diff --git a/.clang-tidy b/.clang-tidy index 68fc9e75fc..02e90d9148 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -75,6 +75,8 @@ Checks: "-*, # readability-static-accessed-through-instance, # this check is probably unnecessary. It makes the code less readable # --- +FormatStyle: file + CheckOptions: bugprone-unsafe-functions.ReportMoreUnsafeFunctions: true bugprone-unused-return-value.CheckedReturnTypes: ::std::error_code;::std::error_condition;::std::errc @@ -83,6 +85,8 @@ CheckOptions: readability-braces-around-statements.ShortStatementLines: 2 readability-identifier-naming.MacroDefinitionCase: UPPER_CASE + readability-identifier-naming.NamespaceCase: lower_case + readability-identifier-naming.InlineNamespaceCase: lower_case readability-identifier-naming.ClassCase: CamelCase readability-identifier-naming.StructCase: CamelCase readability-identifier-naming.UnionCase: CamelCase diff --git a/.cspell.config.yaml b/.cspell.config.yaml index e2383c78d9..23227aa6ef 100644 --- a/.cspell.config.yaml +++ b/.cspell.config.yaml @@ -176,7 +176,6 @@ words: - MPTAMM - MPTDEX - Merkle - - Metafuncton - misprediction - missingok - mptbalance diff --git a/.envrc b/.envrc index 3550a30f2d..cecf4b4767 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,3 @@ +watch_file nix/*.nix + use flake diff --git a/.github/dependabot.yml b/.github/dependabot.yml index d930f3c006..1ece579328 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -15,6 +15,10 @@ updates: commit-message: prefix: "ci: [DEPENDABOT] " target-branch: develop + groups: + github-actions: + patterns: + - "*" - package-ecosystem: cargo directory: /crates diff --git a/.github/scripts/levelization/results/ordering.txt b/.github/scripts/levelization/results/ordering.txt index 709ba4d6d4..5577c363fd 100644 --- a/.github/scripts/levelization/results/ordering.txt +++ b/.github/scripts/levelization/results/ordering.txt @@ -130,12 +130,9 @@ test.ledger > xrpl.json test.ledger > xrpl.ledger test.ledger > xrpl.protocol test.nodestore > test.jtx -test.nodestore > test.unit_test test.nodestore > xrpl.basics -test.nodestore > xrpl.config test.nodestore > xrpld.core test.nodestore > xrpl.nodestore -test.nodestore > xrpl.protocol test.nodestore > xrpl.rdb test.overlay > test.jtx test.overlay > test.unit_test diff --git a/.github/scripts/rename/namespace.sh b/.github/scripts/rename/namespace.sh index bb186bc8bc..94a9205f76 100755 --- a/.github/scripts/rename/namespace.sh +++ b/.github/scripts/rename/namespace.sh @@ -46,9 +46,6 @@ for DIRECTORY in "${DIRECTORIES[@]}"; do done done -# Special case for NuDBFactory that has ripple twice in the test suite name. -${SED_COMMAND} -i -E 's/(BEAST_DEFINE_TESTSUITE.+)ripple(.+)/\1xrpl\2/g' src/test/nodestore/NuDBFactory_test.cpp - DIRECTORY=$1 find "${DIRECTORY}" -type f -name "*.md" | while read -r FILE; do echo "Processing file: ${FILE}" diff --git a/.github/scripts/strategy-matrix/linux.json b/.github/scripts/strategy-matrix/linux.json index 992f314686..9510212344 100644 --- a/.github/scripts/strategy-matrix/linux.json +++ b/.github/scripts/strategy-matrix/linux.json @@ -1,13 +1,22 @@ { - "image_tag": "sha-3122de8", + "image_tag": "sha-fecfc0c", "configs": { "ubuntu": [ + { + "compiler": ["gcc"], + "build_type": ["Debug"], + "arch": ["amd64"], + "minimal": true, + "suffix": "coverage", + "extra_cmake_args": "-DUNIT_TEST_REFERENCE_FEE=500 -Dcoverage=ON -Dcoverage_format=xml -DCODE_COVERAGE_VERBOSE=ON -DCMAKE_C_FLAGS=-O0 -DCMAKE_CXX_FLAGS=-O0" + }, { "compiler": ["clang"], "build_type": ["Release"], "arch": ["amd64"], "minimal": true }, + { "compiler": ["gcc"], "build_type": ["Release"], @@ -29,14 +38,6 @@ "sanitizers": ["address", "undefinedbehavior"] }, - { - "compiler": ["gcc"], - "build_type": ["Debug"], - "arch": ["amd64"], - "minimal": true, - "suffix": "coverage", - "extra_cmake_args": "-DUNIT_TEST_REFERENCE_FEE=500 -Dcoverage=ON -Dcoverage_format=xml -DCODE_COVERAGE_VERBOSE=ON -DCMAKE_C_FLAGS=-O0 -DCMAKE_CXX_FLAGS=-O0" - }, { "compiler": ["clang"], "build_type": ["Debug"], diff --git a/.github/workflows/build-nix-images.yml b/.github/workflows/build-nix-images.yml index 8574182a7e..fe2f43fdcc 100644 --- a/.github/workflows/build-nix-images.yml +++ b/.github/workflows/build-nix-images.yml @@ -13,6 +13,7 @@ on: - "!nix/docker/README.md" - "!nix/devshell.nix" - "bin/check-tools.sh" + - "bin/default-loader-path.sh" - "bin/install-sanitizer-libs.sh" pull_request: paths: @@ -24,6 +25,7 @@ on: - "!nix/docker/README.md" - "!nix/devshell.nix" - "bin/check-tools.sh" + - "bin/default-loader-path.sh" - "bin/install-sanitizer-libs.sh" workflow_dispatch: diff --git a/.github/workflows/check-pr-description.yml b/.github/workflows/check-pr-description.yml index 744449f216..f8e7b6cdc4 100644 --- a/.github/workflows/check-pr-description.yml +++ b/.github/workflows/check-pr-description.yml @@ -23,7 +23,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Write PR body to file env: diff --git a/.github/workflows/check-tools.yml b/.github/workflows/check-tools.yml new file mode 100644 index 0000000000..99dddd7d96 --- /dev/null +++ b/.github/workflows/check-tools.yml @@ -0,0 +1,114 @@ +# Verifies the committed snapshots of `bin/check-tools.sh` output for each Nix +# environment (see nix/check-tools/). If the environment changes — a new image +# tag, an updated flake.lock, a different tool list — without the matching +# snapshot being regenerated and committed, this workflow fails so the drift is +# caught in review. +# +# To regenerate the snapshots, see nix/check-tools/README.md. +name: Check tools + +on: + pull_request: + paths: + - ".github/workflows/check-tools.yml" + - ".github/scripts/strategy-matrix/linux.json" + - "bin/check-tools.sh" + - "nix/**" + - "flake.nix" + - "flake.lock" + - "rust-toolchain.toml" + push: + branches: + - "develop" + paths: + - ".github/workflows/check-tools.yml" + - ".github/scripts/strategy-matrix/linux.json" + - "bin/check-tools.sh" + - "nix/**" + - "flake.nix" + - "flake.lock" + - "rust-toolchain.toml" + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +defaults: + run: + shell: bash + +jobs: + # The nix-nixos image tag is pinned alongside the build matrix in linux.json, + # so snapshots are checked against the exact image CI builds against. + linux-image-tag: + runs-on: ubuntu-latest + outputs: + tag: ${{ steps.tag.outputs.tag }} + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Read nix image tag + id: tag + run: echo "tag=$(jq -r .image_tag .github/scripts/strategy-matrix/linux.json)" >>"${GITHUB_OUTPUT}" + + # One job for all environments; they differ only in whether the tools come + # from the nix-nixos container (Linux) or `nix develop` (macOS). + check-tools: + needs: linux-image-tag + strategy: + fail-fast: false + matrix: + include: + - runner: ubuntu-latest + snapshot: nix/check-tools/nix-ubuntu-amd64.txt + nix_develop: false + - runner: ubuntu-24.04-arm + snapshot: nix/check-tools/nix-ubuntu-arm64.txt + nix_develop: false + - runner: macos-26-apple-clang-21 + snapshot: nix/check-tools/macos.txt + nix_develop: true + runs-on: ${{ matrix.runner }} + # Linux runs inside the pinned nix-nixos image; macOS runs natively and uses + # the flake's dev shell instead (see the run step below). + container: ${{ !matrix.nix_develop && format('ghcr.io/xrplf/xrpld/nix-ubuntu:{0}', needs.linux-image-tag.outputs.tag) || null }} + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Prepare runner + uses: XRPLF/actions/prepare-runner@c00c22ada3bd6bcda48fcb0d62fbbab49fec8a0f + with: + enable_ccache: false + + - name: Regenerate snapshot + env: + CHECK_TOOLS_SKIP_CLONE: "1" + # check-tools.sh skips some macOS tools when CI is set; the snapshots + # capture the full `nix develop` environment, so unset it here. + CI: "" + run: | + if [ "${{ matrix.nix_develop }}" = "true" ]; then + # `nix develop` prints the dev-shell greeting first; keep only the + # check-tools.sh output (from the "Detected OS:" line onward). + nix --extra-experimental-features "nix-command flakes" develop \ + -c bash bin/check-tools.sh | sed -n '/^Detected OS:/,$p' >"${{ matrix.snapshot }}" + else + bash bin/check-tools.sh >"${{ matrix.snapshot }}" + fi + + - name: Verify snapshot is up to date + run: | + if ! git diff --exit-code -- "${{ matrix.snapshot }}"; then + echo "::error::${{ matrix.snapshot }} is out of date. Regenerate it (see nix/check-tools/README.md) and commit the result." + exit 1 + fi + + - name: Upload regenerated snapshot + if: failure() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: check-tools-${{ runner.os }}-${{ runner.arch }} + path: ${{ matrix.snapshot }} diff --git a/.github/workflows/on-pr.yml b/.github/workflows/on-pr.yml index 70335ae05e..552909305e 100644 --- a/.github/workflows/on-pr.yml +++ b/.github/workflows/on-pr.yml @@ -52,7 +52,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Determine changed files # This step checks whether any files have changed that should # cause the next jobs to run. We do it this way rather than @@ -91,6 +91,7 @@ jobs: .clang-tidy .codecov.yml bin/check-tools.sh + bin/default-loader-path.sh cfg/** cmake/** conan/** diff --git a/.github/workflows/on-trigger.yml b/.github/workflows/on-trigger.yml index a160478c4f..5c24bbd186 100644 --- a/.github/workflows/on-trigger.yml +++ b/.github/workflows/on-trigger.yml @@ -29,6 +29,7 @@ on: - ".clang-tidy" - ".codecov.yml" - "bin/check-tools.sh" + - "bin/default-loader-path.sh" - "cfg/**" - "cmake/**" - "conan/**" diff --git a/.github/workflows/publish-docs.yml b/.github/workflows/publish-docs.yml index 19c73f93d6..a3e096315c 100644 --- a/.github/workflows/publish-docs.yml +++ b/.github/workflows/publish-docs.yml @@ -41,13 +41,13 @@ env: jobs: build: runs-on: ubuntu-latest - container: ghcr.io/xrplf/xrpld/nix-ubuntu:sha-3122de8 + container: ghcr.io/xrplf/xrpld/nix-ubuntu:sha-fecfc0c steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Prepare runner - uses: XRPLF/actions/prepare-runner@e4b6449d55a61c002d7c3fdfa6c20f721ede0606 + uses: XRPLF/actions/prepare-runner@c00c22ada3bd6bcda48fcb0d62fbbab49fec8a0f with: enable_ccache: false diff --git a/.github/workflows/reusable-build-test-config.yml b/.github/workflows/reusable-build-test-config.yml index 0a12708ce8..9b2f692026 100644 --- a/.github/workflows/reusable-build-test-config.yml +++ b/.github/workflows/reusable-build-test-config.yml @@ -110,10 +110,10 @@ jobs: uses: XRPLF/actions/cleanup-workspace@c7d9ce5ebb03c752a354889ecd870cadfc2b1cd4 - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Prepare runner - uses: XRPLF/actions/prepare-runner@e4b6449d55a61c002d7c3fdfa6c20f721ede0606 + uses: XRPLF/actions/prepare-runner@c00c22ada3bd6bcda48fcb0d62fbbab49fec8a0f with: enable_ccache: ${{ inputs.ccache_enabled }} @@ -266,7 +266,7 @@ jobs: ./xrpld --definitions | python3 -m json.tool >server_definitions.json - name: Upload server definitions - if: ${{ github.event.repository.visibility == 'public' && inputs.config_name == 'debian-gcc-release-amd64' }} + if: ${{ github.event.repository.visibility == 'public' && inputs.config_name == 'ubuntu-gcc-debug-amd64-coverage' }} uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: server-definitions diff --git a/.github/workflows/reusable-check-levelization.yml b/.github/workflows/reusable-check-levelization.yml index 88c95ac3ba..7f547f2ab6 100644 --- a/.github/workflows/reusable-check-levelization.yml +++ b/.github/workflows/reusable-check-levelization.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Check levelization run: python .github/scripts/levelization/generate.py - name: Check for differences diff --git a/.github/workflows/reusable-check-rename.yml b/.github/workflows/reusable-check-rename.yml index 9a91e98ee3..874c8adcde 100644 --- a/.github/workflows/reusable-check-rename.yml +++ b/.github/workflows/reusable-check-rename.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Check definitions run: .github/scripts/rename/definitions.sh . - name: Check copyright notices diff --git a/.github/workflows/reusable-clang-tidy.yml b/.github/workflows/reusable-clang-tidy.yml index c32fe2e29c..983972e8c1 100644 --- a/.github/workflows/reusable-clang-tidy.yml +++ b/.github/workflows/reusable-clang-tidy.yml @@ -34,16 +34,16 @@ jobs: needs: [determine-files] if: ${{ needs.determine-files.outputs.cpp_changed_files != '' || needs.determine-files.outputs.need_full_run == 'true' }} runs-on: ["self-hosted", "Linux", "X64", "heavy"] - container: "ghcr.io/xrplf/xrpld/nix-debian:sha-3122de8" + container: "ghcr.io/xrplf/xrpld/nix-debian:sha-fecfc0c" permissions: contents: read issues: write steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Prepare runner - uses: XRPLF/actions/prepare-runner@e4b6449d55a61c002d7c3fdfa6c20f721ede0606 + uses: XRPLF/actions/prepare-runner@c00c22ada3bd6bcda48fcb0d62fbbab49fec8a0f with: enable_ccache: false @@ -101,7 +101,7 @@ jobs: TARGETS: ${{ needs.determine-files.outputs.need_full_run != 'true' && needs.determine-files.outputs.cpp_changed_files || 'include src tests' }} run: | set -o pipefail - run-clang-tidy -j ${{ steps.nproc.outputs.nproc }} -p "${BUILD_DIR}" -quiet -fix -allow-no-checks ${TARGETS} 2>&1 | tee "${OUTPUT_FILE}" + run-clang-tidy -j ${{ steps.nproc.outputs.nproc }} -p "${BUILD_DIR}" -quiet -fix -format -allow-no-checks ${TARGETS} 2>&1 | tee "${OUTPUT_FILE}" - name: Print filtered clang-tidy errors if: ${{ steps.run_clang_tidy.outcome != 'success' }} diff --git a/.github/workflows/reusable-package.yml b/.github/workflows/reusable-package.yml index 55bc20dc5c..e1c11ac677 100644 --- a/.github/workflows/reusable-package.yml +++ b/.github/workflows/reusable-package.yml @@ -27,7 +27,7 @@ jobs: matrix: ${{ steps.generate.outputs.matrix }} steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Set up Python uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 @@ -54,7 +54,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Download pre-built binary uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 diff --git a/.github/workflows/reusable-strategy-matrix.yml b/.github/workflows/reusable-strategy-matrix.yml index de8d9cfc8e..12f11b0fbe 100644 --- a/.github/workflows/reusable-strategy-matrix.yml +++ b/.github/workflows/reusable-strategy-matrix.yml @@ -23,7 +23,7 @@ jobs: matrix: ${{ steps.generate.outputs.matrix }} steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Set up Python uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 diff --git a/.github/workflows/reusable-upload-recipe.yml b/.github/workflows/reusable-upload-recipe.yml index df4a2d9516..b4ab638dee 100644 --- a/.github/workflows/reusable-upload-recipe.yml +++ b/.github/workflows/reusable-upload-recipe.yml @@ -40,14 +40,14 @@ defaults: jobs: upload: runs-on: ubuntu-latest - container: ghcr.io/xrplf/xrpld/nix-ubuntu:sha-3122de8 + container: ghcr.io/xrplf/xrpld/nix-ubuntu:sha-fecfc0c env: REMOTE_NAME: ${{ inputs.remote_name }} CONAN_LOGIN_USERNAME_XRPLF: ${{ secrets.remote_username }} CONAN_PASSWORD_XRPLF: ${{ secrets.remote_password }} steps: - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Generate build version number id: version diff --git a/.github/workflows/upload-conan-deps.yml b/.github/workflows/upload-conan-deps.yml index 8a02c4c2db..eb58650bdf 100644 --- a/.github/workflows/upload-conan-deps.yml +++ b/.github/workflows/upload-conan-deps.yml @@ -65,10 +65,10 @@ jobs: uses: XRPLF/actions/cleanup-workspace@c7d9ce5ebb03c752a354889ecd870cadfc2b1cd4 - name: Checkout repository - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Prepare runner - uses: XRPLF/actions/prepare-runner@e4b6449d55a61c002d7c3fdfa6c20f721ede0606 + uses: XRPLF/actions/prepare-runner@c00c22ada3bd6bcda48fcb0d62fbbab49fec8a0f with: enable_ccache: false diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7632741e35..fc385cf6ed 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -348,12 +348,14 @@ run-clang-tidy -p build -allow-no-checks src tests ``` This will check all source files in the `src`, `include` and `tests` directories using the compile commands from your `build` directory. -If you wish to automatically fix whatever clang-tidy finds _and_ is capable of fixing, add `-fix` to the above command: +If you wish to automatically fix whatever clang-tidy finds _and_ is capable of fixing, add `-fix -format` to the above command: ``` -run-clang-tidy -p build -quiet -fix -allow-no-checks src tests +run-clang-tidy -p build -quiet -fix -format -allow-no-checks src tests ``` +`-format` reformats the fixed code with [`.clang-format`](./.clang-format); without it the fixes are inserted in LLVM style and the `clang-format` hook rewrites them afterwards. + ## Contracts and instrumentation We are using [Antithesis](https://antithesis.com/) for continuous fuzzing, diff --git a/bin/check-tools.sh b/bin/check-tools.sh index 7886bcf8b0..e230302742 100755 --- a/bin/check-tools.sh +++ b/bin/check-tools.sh @@ -30,8 +30,10 @@ missing=() checked=0 # check [probe-command...] -# Runs the probe (default: " --version") quietly. Records as -# missing if the command is not found or exits non-zero. +# Runs the probe (default: " --version"), capturing both stdout and +# stderr, and prints one aligned line: the status, the name, and the first +# non-blank line of the probe output (its version). Records as missing +# if the command is not found or exits non-zero. check() { local name="$1" shift @@ -40,10 +42,11 @@ check() { probe=("${name}" --version) fi - echo "Checking ${name}..." checked=$((checked + 1)) - if "${probe[@]}" | head -n 1; then - printf ' [ ok ] %s\n' "${name}" + local output version + if output="$("${probe[@]}" 2>&1)"; then + version="$(printf '%s\n' "${output}" | grep -m1 '[^[:space:]]' || true)" + printf ' [ ok ] %-20s %s\n' "${name}" "${version}" else printf ' [MISS] %s\n' "${name}" missing+=("${name}") @@ -85,12 +88,14 @@ if [ "${os}" = "linux" ] || [ "${os}" = "macos" ]; then check file check less check make - check netstat which netstat + # net-tools netstat reports "net-tools X.Y"; macOS ships BSD netstat with no + # version flag, so fall back to a presence marker there. + check netstat sh -c 'command -v netstat >/dev/null && { netstat --version 2>&1 | grep -m1 -oE "net-tools [0-9.]+" || echo present; }' check ninja - check perl + check perl perl -e 'print "$^V\n"' check pkg-config check vim - check zip + check zip bash -c 'zip --version 2>&1 | grep -m1 -oE "Zip [0-9.]+"' # These tools are present in our Linux CI images and in local development # setups, but not in the macOS CI environment. So check them everywhere diff --git a/nix/docker/loader-path.sh b/bin/default-loader-path.sh similarity index 100% rename from nix/docker/loader-path.sh rename to bin/default-loader-path.sh diff --git a/bin/pre-commit/clang_tidy_check.py b/bin/pre-commit/clang_tidy_check.py index cf4808d2ea..118d9619e2 100755 --- a/bin/pre-commit/clang_tidy_check.py +++ b/bin/pre-commit/clang_tidy_check.py @@ -144,7 +144,11 @@ def main(): + files ) canonicalize_fix_paths(Path(fixes_dir)) - applied = subprocess.run([clang_apply_replacements, fixes_dir]) + # `FormatStyle` in .clang-tidy does not reach this path, + # so ask for the repository style here. + applied = subprocess.run( + [clang_apply_replacements, "--format", "--style=file", fixes_dir] + ) return result.returncode or applied.returncode diff --git a/cmake/CompilationEnv.cmake b/cmake/CompilationEnv.cmake index 8e69a4dfdd..471c43d6c6 100644 --- a/cmake/CompilationEnv.cmake +++ b/cmake/CompilationEnv.cmake @@ -29,6 +29,27 @@ if(CMAKE_GENERATOR STREQUAL "Xcode") set(is_xcode TRUE) endif() +# -------------------------------------------------------------------- +# Nix toolchain detection +# -------------------------------------------------------------------- +# True when the C++ compiler resolves into the Nix store. CMAKE_CXX_COMPILER may +# be referenced through a symlink outside the store (a Nix profile, a /usr/bin +# alternative, ...), so resolve the real path before matching. +set(is_nix_compiler FALSE) +get_filename_component(_cxx_real "${CMAKE_CXX_COMPILER}" REALPATH) +if(_cxx_real MATCHES "^/nix/store/") + set(is_nix_compiler TRUE) +endif() +unset(_cxx_real) + +# True inside the Nix CI Docker image, identified by the /nix/ci-env tree it +# ships (see nix/docker/Dockerfile). The dev shell and bare systems don't have +# it, so it distinguishes the CI image from other Nix-compiler environments. +set(is_ci_image FALSE) +if(EXISTS "/nix/ci-env/bin") + set(is_ci_image TRUE) +endif() + # -------------------------------------------------------------------- # Operating system detection # -------------------------------------------------------------------- diff --git a/cmake/PatchNixBinary.cmake b/cmake/PatchNixBinary.cmake index 79ca0b150c..2490416f1f 100644 --- a/cmake/PatchNixBinary.cmake +++ b/cmake/PatchNixBinary.cmake @@ -1,26 +1,37 @@ #[===================================================================[ Patch executables to run in non-Nix environments. - The Nix-based CI image links binaries against an ELF interpreter (loader) - that lives in the Nix store, so the resulting binaries don't run elsewhere - (including once installed from the .deb package). `patch_nix_binary` adds a - POST_BUILD step that resets the interpreter to the system default loader and - drops the rpath. + The Nix toolchain links binaries against an ELF interpreter (loader) + that lives in the Nix store, so the resulting binaries don't run elsewhere. + `patch_nix_binary` adds a POST_BUILD step that resets the interpreter + to the system default loader and drops the rpath. - This is only active inside the Nix-based image, detected by the presence of - /tmp/loader-path.sh (shipped by that image, resolves the default loader). It - is skipped for sanitizer builds, whose runtime libraries are resolved through - the rpath. Everywhere else `patch_nix_binary` is a no-op. + This runs by default for Nix-toolchain builds (determined by whether the compiler resolves under /nix/store/). + Those builds are where binaries get a Nix-store loader. + It is opted out of by setting the XRPLD_NO_PATCH_NIX_BINARY environment variable — + the plain Nix dev shells set it, since their binaries link a newer glibc + and must not be retargeted to the system loader. + + Non-Nix builds (a system compiler, already using the system loader) and sanitizer builds + (runtime libraries resolved through the rpath) are skipped too. + Everywhere else `patch_nix_binary` is a no-op. + + The default loader is resolved by bin/default-loader-path.sh. #]===================================================================] include_guard(GLOBAL) include(CompilationEnv) -# Provided by the Nix-based CI image; prints the system default ELF loader path. -set(_loader_path_script "/tmp/loader-path.sh") +# Resolves the system default ELF loader path for the current architecture. +set(_loader_path_script "${CMAKE_SOURCE_DIR}/bin/default-loader-path.sh") -if(is_linux AND NOT SANITIZERS_ENABLED AND EXISTS "${_loader_path_script}") +if( + is_linux + AND NOT SANITIZERS_ENABLED + AND is_nix_compiler + AND NOT DEFINED ENV{XRPLD_NO_PATCH_NIX_BINARY} +) execute_process( COMMAND "${_loader_path_script}" OUTPUT_VARIABLE DEFAULT_LOADER_PATH diff --git a/cmake/XrplAddBenchmark.cmake b/cmake/XrplAddBenchmark.cmake index 1dd875dd61..921deb0658 100644 --- a/cmake/XrplAddBenchmark.cmake +++ b/cmake/XrplAddBenchmark.cmake @@ -1,3 +1,5 @@ +include_guard() + include(isolate_headers) # Define a benchmark executable for the module `name`. diff --git a/cmake/XrplCompiler.cmake b/cmake/XrplCompiler.cmake index cb4e797137..e262acf1c9 100644 --- a/cmake/XrplCompiler.cmake +++ b/cmake/XrplCompiler.cmake @@ -171,9 +171,8 @@ else() # Clang wrapper supplies those paths itself (via -nostdinc++), so at compile time the # flag is unused -> Clang errors under our -Werror. At link time the flag IS consumed # (it selects the C++ runtime), so we move it there instead of dropping it entirely. - get_filename_component(_cxx_real "${CMAKE_CXX_COMPILER}" REALPATH) if( - _cxx_real MATCHES "^/nix/store/" + is_nix_compiler AND is_linux AND is_clang AND CMAKE_CXX_FLAGS MATCHES "stdlib=libstdc" diff --git a/cmake/XrplSanity.cmake b/cmake/XrplSanity.cmake index a35645ad5c..ba9f7988bc 100644 --- a/cmake/XrplSanity.cmake +++ b/cmake/XrplSanity.cmake @@ -36,6 +36,19 @@ elseif(is_gcc) endif() endif() +# A Nix compiler is only meant to be used from a managed environment: the xrpld +# dev shell (which exports XRPL_DEVSHELL) or the CI image. Using one from a bare +# shell usually means a leaked toolchain (picked up via PATH or a Conan profile) +# and leads to confusing breakage, so fail early with guidance. +if(is_nix_compiler AND NOT is_ci_image AND NOT DEFINED ENV{XRPL_DEVSHELL}) + message( + FATAL_ERROR + "A Nix compiler (${CMAKE_CXX_COMPILER}) is being used outside the xrpld " + "dev shell. Enter it with `nix develop` (see docs/build/nix.md) before " + "configuring the build." + ) +endif() + # check for in-source build and fail if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}") message( diff --git a/docs/build/nix.md b/docs/build/nix.md index b95b82fc42..d0001294e3 100644 --- a/docs/build/nix.md +++ b/docs/build/nix.md @@ -154,6 +154,14 @@ conan install .. --output-folder . --build '*' --settings build_type=Release To update `flake.lock` to the latest revision use `nix flake update` command. +## Tooling snapshots + +The tool versions in each Nix environment are recorded in +[`nix/check-tools/`](../../nix/check-tools) and verified by CI. If you change the +environment (bump the CI image tag, update `flake.lock`, or edit the tool list in +`bin/check-tools.sh`), CI fails until you regenerate and commit the affected +snapshot — see [`nix/check-tools/README.md`](../../nix/check-tools/README.md). + ## Troubleshooting See [Troubleshooting Nix problems](./nix_troubleshooting.md) for common issues, diff --git a/include/xrpl/basics/Resolver.h b/include/xrpl/basics/Resolver.h index 239eb9630e..a159619517 100644 --- a/include/xrpl/basics/Resolver.h +++ b/include/xrpl/basics/Resolver.h @@ -11,7 +11,7 @@ namespace xrpl { class Resolver { public: - using HandlerType = std::function)>; + using HandlerType = std::function)>; virtual ~Resolver() = 0; diff --git a/include/xrpl/basics/Slice.h b/include/xrpl/basics/Slice.h index 36e7615c3a..75c9b8c7bd 100644 --- a/include/xrpl/basics/Slice.h +++ b/include/xrpl/basics/Slice.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -251,4 +252,11 @@ makeSlice(std::basic_string const& s) return Slice(s.data(), s.size()); } +template +Slice +makeSlice(std::basic_string_view s) +{ + return Slice(s.data(), s.size()); +} + } // namespace xrpl diff --git a/include/xrpl/beast/core/LexicalCast.h b/include/xrpl/beast/core/LexicalCast.h index 7cf21892bd..288c5d6673 100644 --- a/include/xrpl/beast/core/LexicalCast.h +++ b/include/xrpl/beast/core/LexicalCast.h @@ -58,7 +58,7 @@ struct LexicalCast "beast::LexicalCast can only be used with integral types"); template - bool + constexpr bool operator()(Integral& out, std::string_view in) const requires(std::is_integral_v && !std::is_same_v) { @@ -110,7 +110,7 @@ struct LexicalCast> { explicit LexicalCast() = default; - bool + constexpr bool operator()(Out& out, boost::core::basic_string_view in) const { return LexicalCast()(out, in); @@ -123,7 +123,7 @@ struct LexicalCast { explicit LexicalCast() = default; - bool + constexpr bool operator()(Out& out, std::string in) const { return LexicalCast()(out, in); @@ -136,7 +136,7 @@ struct LexicalCast { explicit LexicalCast() = default; - bool + constexpr bool operator()(Out& out, char const* in) const { XRPL_ASSERT(in, "beast::detail::LexicalCast(char const*) : non-null input"); @@ -151,7 +151,7 @@ struct LexicalCast { explicit LexicalCast() = default; - bool + constexpr bool operator()(Out& out, char* in) const { XRPL_ASSERT(in, "beast::detail::LexicalCast(char*) : non-null input"); @@ -177,7 +177,7 @@ struct BadLexicalCast : public std::bad_cast * @return `false` if there was a parsing or range error */ template -bool +constexpr bool lexicalCastChecked(Out& out, In in) { return detail::LexicalCast()(out, in); @@ -191,7 +191,7 @@ lexicalCastChecked(Out& out, In in) * @return The new type. */ template -Out +constexpr Out lexicalCastThrow(In in) { if (Out out; lexicalCastChecked(out, in)) @@ -207,7 +207,7 @@ lexicalCastThrow(In in) * @return The new type. */ template -Out +constexpr Out lexicalCast(In in, Out defaultValue = Out()) { if (Out out; lexicalCastChecked(out, in)) diff --git a/include/xrpl/beast/core/SemanticVersion.h b/include/xrpl/beast/core/SemanticVersion.h index 338942c252..c2e395e3f4 100644 --- a/include/xrpl/beast/core/SemanticVersion.h +++ b/include/xrpl/beast/core/SemanticVersion.h @@ -17,14 +17,14 @@ namespace beast { class SemanticVersion { public: - using identifier_list = std::vector; + using IdentifierList = std::vector; int majorVersion; int minorVersion; int patchVersion; - identifier_list preReleaseIdentifiers; - identifier_list metaData; + IdentifierList preReleaseIdentifiers; + IdentifierList metaData; SemanticVersion(); diff --git a/include/xrpl/beast/insight/StatsDCollector.h b/include/xrpl/beast/insight/StatsDCollector.h index e14d3a27ff..0b44f345ba 100644 --- a/include/xrpl/beast/insight/StatsDCollector.h +++ b/include/xrpl/beast/insight/StatsDCollector.h @@ -26,7 +26,7 @@ public: * @param journal Destination for logging output. */ static std::shared_ptr - make(IP::Endpoint const& address, std::string const& prefix, Journal journal); + make(ip::Endpoint const& address, std::string const& prefix, Journal journal); }; } // namespace beast::insight diff --git a/include/xrpl/beast/net/IPAddress.h b/include/xrpl/beast/net/IPAddress.h index 4f4fb189a6..7422778ea2 100644 --- a/include/xrpl/beast/net/IPAddress.h +++ b/include/xrpl/beast/net/IPAddress.h @@ -15,7 +15,7 @@ //------------------------------------------------------------------------------ namespace beast { -namespace IP { +namespace ip { using Address = boost::asio::ip::address; @@ -73,13 +73,13 @@ isPublic(Address const& addr) return (addr.is_v4()) ? isPublic(addr.to_v4()) : isPublic(addr.to_v6()); } -} // namespace IP +} // namespace ip //------------------------------------------------------------------------------ template void -hash_append(Hasher& h, beast::IP::Address const& addr) noexcept +hash_append(Hasher& h, beast::ip::Address const& addr) noexcept { using beast::hash_append; if (addr.is_v4()) @@ -101,12 +101,12 @@ hash_append(Hasher& h, beast::IP::Address const& addr) noexcept namespace boost { template <> -struct hash<::beast::IP::Address> +struct hash<::beast::ip::Address> { explicit hash() = default; std::size_t - operator()(::beast::IP::Address const& addr) const + operator()(::beast::ip::Address const& addr) const { return ::beast::Uhash<>{}(addr); } diff --git a/include/xrpl/beast/net/IPAddressConversion.h b/include/xrpl/beast/net/IPAddressConversion.h index 73777cf841..e2d485642f 100644 --- a/include/xrpl/beast/net/IPAddressConversion.h +++ b/include/xrpl/beast/net/IPAddressConversion.h @@ -4,7 +4,7 @@ #include -namespace beast::IP { +namespace beast::ip { /** * Convert to Endpoint. @@ -32,7 +32,7 @@ toAsioAddress(Endpoint const& endpoint); boost::asio::ip::tcp::endpoint toAsioEndpoint(Endpoint const& endpoint); -} // namespace beast::IP +} // namespace beast::ip namespace beast { @@ -41,25 +41,25 @@ struct IPAddressConversion { explicit IPAddressConversion() = default; - static IP::Endpoint + static ip::Endpoint fromAsio(boost::asio::ip::address const& address) { - return IP::fromAsio(address); + return ip::fromAsio(address); } - static IP::Endpoint + static ip::Endpoint fromAsio(boost::asio::ip::tcp::endpoint const& endpoint) { - return IP::fromAsio(endpoint); + return ip::fromAsio(endpoint); } static boost::asio::ip::address - toAsioAddress(IP::Endpoint const& address) + toAsioAddress(ip::Endpoint const& address) { - return IP::toAsioAddress(address); + return ip::toAsioAddress(address); } static boost::asio::ip::tcp::endpoint - toAsioEndpoint(IP::Endpoint const& address) + toAsioEndpoint(ip::Endpoint const& address) { - return IP::toAsioEndpoint(address); + return ip::toAsioEndpoint(address); } }; diff --git a/include/xrpl/beast/net/IPAddressV4.h b/include/xrpl/beast/net/IPAddressV4.h index 94943af3ea..280c2c791c 100644 --- a/include/xrpl/beast/net/IPAddressV4.h +++ b/include/xrpl/beast/net/IPAddressV4.h @@ -2,7 +2,7 @@ #include -namespace beast::IP { +namespace beast::ip { using AddressV4 = boost::asio::ip::address_v4; @@ -25,4 +25,4 @@ isPublic(AddressV4 const& addr); char getClass(AddressV4 const& address); -} // namespace beast::IP +} // namespace beast::ip diff --git a/include/xrpl/beast/net/IPAddressV6.h b/include/xrpl/beast/net/IPAddressV6.h index b51cb62532..0659e7e405 100644 --- a/include/xrpl/beast/net/IPAddressV6.h +++ b/include/xrpl/beast/net/IPAddressV6.h @@ -2,7 +2,7 @@ #include -namespace beast::IP { +namespace beast::ip { using AddressV6 = boost::asio::ip::address_v6; @@ -18,4 +18,4 @@ isPrivate(AddressV6 const& addr); bool isPublic(AddressV6 const& addr); -} // namespace beast::IP +} // namespace beast::ip diff --git a/include/xrpl/beast/net/IPEndpoint.h b/include/xrpl/beast/net/IPEndpoint.h index c4b269e9c3..d4d3b2ab12 100644 --- a/include/xrpl/beast/net/IPEndpoint.h +++ b/include/xrpl/beast/net/IPEndpoint.h @@ -13,7 +13,7 @@ #include #include -namespace beast::IP { +namespace beast::ip { using Port = std::uint16_t; @@ -223,7 +223,7 @@ operator<<(OutputStream& os, Endpoint const& endpoint) std::istream& operator>>(std::istream& is, Endpoint& endpoint); -} // namespace beast::IP +} // namespace beast::ip //------------------------------------------------------------------------------ @@ -232,12 +232,12 @@ namespace std { * std::hash support. */ template <> -struct hash<::beast::IP::Endpoint> +struct hash<::beast::ip::Endpoint> { hash() = default; std::size_t - operator()(::beast::IP::Endpoint const& endpoint) const + operator()(::beast::ip::Endpoint const& endpoint) const { return ::beast::Uhash<>{}(endpoint); } @@ -249,12 +249,12 @@ namespace boost { * boost::hash support. */ template <> -struct hash<::beast::IP::Endpoint> +struct hash<::beast::ip::Endpoint> { hash() = default; std::size_t - operator()(::beast::IP::Endpoint const& endpoint) const + operator()(::beast::ip::Endpoint const& endpoint) const { return ::beast::Uhash<>{}(endpoint); } diff --git a/include/xrpl/core/ServiceRegistry.h b/include/xrpl/core/ServiceRegistry.h index 592964134b..000bdaa7fa 100644 --- a/include/xrpl/core/ServiceRegistry.h +++ b/include/xrpl/core/ServiceRegistry.h @@ -15,12 +15,12 @@ namespace xrpl { // Forward declarations -namespace NodeStore { +namespace node_store { class Database; -} // namespace NodeStore -namespace Resource { +} // namespace node_store +namespace resource { class Manager; -} // namespace Resource +} // namespace resource namespace perf { class PerfLog; } // namespace perf @@ -160,11 +160,11 @@ public: virtual PeerReservationTable& getPeerReservations() = 0; - virtual Resource::Manager& + virtual resource::Manager& getResourceManager() = 0; // Storage services - virtual NodeStore::Database& + virtual node_store::Database& getNodeStore() = 0; virtual SHAMapStore& diff --git a/include/xrpl/json/json_value.h b/include/xrpl/json/json_value.h index 47ad3ac1e0..260917face 100644 --- a/include/xrpl/json/json_value.h +++ b/include/xrpl/json/json_value.h @@ -623,6 +623,7 @@ class ValueConstIterator : public ValueIteratorBase public: using size_t = unsigned int; using difference_type = int; + using value_type = Value const; using reference = Value const&; using pointer = Value const*; using SelfType = ValueConstIterator; @@ -687,6 +688,7 @@ class ValueIterator : public ValueIteratorBase public: using size_t = unsigned int; using difference_type = int; + using value_type = Value; using reference = Value&; using pointer = Value*; using SelfType = ValueIterator; diff --git a/include/xrpl/ledger/helpers/LendingHelpers.h b/include/xrpl/ledger/helpers/LendingHelpers.h index 8e0d11cccb..c69efff964 100644 --- a/include/xrpl/ledger/helpers/LendingHelpers.h +++ b/include/xrpl/ledger/helpers/LendingHelpers.h @@ -286,6 +286,77 @@ computeFullPaymentInterest( std::uint32_t startDate, TenthBips32 closeInterestRate); +// Deltas applied to Vault.AssetsTotal and LoanBroker.DebtTotal at a single +// accounting touch point (origination, payment, impair/unimpair/default). +struct AccountingDeltas +{ + Number assetsTotalDelta; + Number debtTotalDelta; +}; + +// Whole-life (pre-LendingProtocolV1_1) recognition model: interest is +// recognized into AssetsTotal/DebtTotal up front, at origination. +namespace accrual { + +// LoanSet origination: what's added to Vault.AssetsTotal and LoanBroker.DebtTotal +AccountingDeltas +loanOriginationDeltas(Number const& principalRequested, Number const& interestDue); + +// LoanSet origination: would recognizing this loan's interest push +// Vault.AssetsTotal past Vault.AssetsMaximum? +bool +loanOriginationExceedsVaultMaximum( + Number const& vaultMaximum, + Number const& vaultTotal, + Number const& interestDue); + +// LoanManage impair/unimpair/default: the vault's exposure to this loan +Number +loanVaultExposure(SLE::const_ref loanSle); + +// LoanPay: what's added to Vault.AssetsTotal and subtracted from LoanBroker.DebtTotal for a payment +AccountingDeltas +loanPaymentDeltas(LoanPaymentParts const& parts); + +} // namespace accrual + +// Cash-basis (LendingProtocolV1_1) recognition model: AssetsTotal/DebtTotal +// are principal-only, interest is recognized only as it's actually paid. +namespace cash_basis { + +AccountingDeltas +loanOriginationDeltas(Number const& principalRequested); + +Number +loanVaultExposure(SLE::const_ref loanSle); + +AccountingDeltas +loanPaymentDeltas(LoanPaymentParts const& parts); + +} // namespace cash_basis + +// Public dispatchers: pick cash_basis:: if featureLendingProtocolV1_1 is +// enabled AND the Vault's LEVersion (VaultHelpers::getVaultVersion) is +// VaultVersion::CashBasis, else accrual::. These are the only entry points +// transactors call. +AccountingDeltas +loanOriginationDeltas( + SLE::const_ref vaultSle, + Number const& principalRequested, + Number const& interestDue); + +bool +loanOriginationExceedsVaultMaximum( + SLE::const_ref vaultSle, + Number const& vaultTotal, + Number const& interestDue); + +Number +loanVaultExposure(SLE::const_ref vaultSle, SLE::const_ref loanSle); + +AccountingDeltas +loanPaymentDeltas(SLE::const_ref vaultSle, LoanPaymentParts const& parts); + namespace detail { // These classes and functions should only be accessed by LendingHelper // functions and unit tests diff --git a/include/xrpl/ledger/helpers/VaultHelpers.h b/include/xrpl/ledger/helpers/VaultHelpers.h index 1bd1663314..5681cc57e8 100644 --- a/include/xrpl/ledger/helpers/VaultHelpers.h +++ b/include/xrpl/ledger/helpers/VaultHelpers.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -107,4 +108,19 @@ sharesToAssetsWithdraw( [[nodiscard]] bool isSoleShareholder(ReadView const& view, AccountID const& account, SLE::const_ref issuance); +/** + * Resolves a Vault's LEVersion, the single point every accounting touch + * point should call to determine which recognition model (accrual vs. + * cash-basis) a Vault uses. Vaults created before featureLendingProtocolV1_1 + * activated never have sfLEVersion set, which resolves here to + * VaultVersion::Legacy. + * + * @param vault The vault SLE. + * + * @return The Vault's LEVersion, or VaultVersion::Legacy if the field is + * absent. + */ +[[nodiscard]] VaultVersion +getVaultVersion(SLE::const_ref vault); + } // namespace xrpl diff --git a/include/xrpl/net/AutoSocket.h b/include/xrpl/net/AutoSocket.h index b98885959d..d090247388 100644 --- a/include/xrpl/net/AutoSocket.h +++ b/include/xrpl/net/AutoSocket.h @@ -67,16 +67,16 @@ public: return socket_->next_layer(); } - beast::IP::Endpoint + beast::ip::Endpoint localEndpoint() { - return beast::IP::fromAsio(lowestLayer().local_endpoint()); + return beast::ip::fromAsio(lowestLayer().local_endpoint()); } - beast::IP::Endpoint + beast::ip::Endpoint remoteEndpoint() { - return beast::IP::fromAsio(lowestLayer().remote_endpoint()); + return beast::ip::fromAsio(lowestLayer().remote_endpoint()); } lowest_layer_type& diff --git a/include/xrpl/nodestore/Backend.h b/include/xrpl/nodestore/Backend.h index 564a874c5e..85d076bcfb 100644 --- a/include/xrpl/nodestore/Backend.h +++ b/include/xrpl/nodestore/Backend.h @@ -13,7 +13,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * A backend used for the NodeStore. @@ -163,4 +163,4 @@ public: fdRequired() const = 0; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/Database.h b/include/xrpl/nodestore/Database.h index 96ba91bd76..902cfc9e03 100644 --- a/include/xrpl/nodestore/Database.h +++ b/include/xrpl/nodestore/Database.h @@ -25,7 +25,7 @@ namespace xrpl { class Section; } // namespace xrpl -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Persistency layer for NodeObject @@ -248,7 +248,7 @@ protected: void storeStats(std::uint64_t count, std::uint64_t sz) { - XRPL_ASSERT(count <= sz, "xrpl::NodeStore::Database::storeStats : valid inputs"); + XRPL_ASSERT(count <= sz, "xrpl::node_store::Database::storeStats : valid inputs"); storeCount_ += count; storeSz_ += sz; } @@ -308,4 +308,4 @@ private: threadEntry(); }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/DatabaseRotating.h b/include/xrpl/nodestore/DatabaseRotating.h index 5381b5c435..21b8b422c7 100644 --- a/include/xrpl/nodestore/DatabaseRotating.h +++ b/include/xrpl/nodestore/DatabaseRotating.h @@ -9,7 +9,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { /* This class has two key-value store Backend objects for persisting SHAMap * records. This facilitates online deletion of data. New backends are @@ -38,7 +38,7 @@ public: */ virtual void rotate( - std::unique_ptr&& newBackend, + std::unique_ptr&& newBackend, std::function const& f) = 0; @@ -56,4 +56,4 @@ public: setRotationInFlight(bool inFlight) = 0; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/DummyScheduler.h b/include/xrpl/nodestore/DummyScheduler.h index 49b0d37462..fc7a040b5a 100644 --- a/include/xrpl/nodestore/DummyScheduler.h +++ b/include/xrpl/nodestore/DummyScheduler.h @@ -3,7 +3,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Simple NodeStore Scheduler that just performs the tasks synchronously. @@ -21,4 +21,4 @@ public: onBatchWrite(BatchWriteReport const& report) override; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/Factory.h b/include/xrpl/nodestore/Factory.h index a18023a8a8..568e9b0712 100644 --- a/include/xrpl/nodestore/Factory.h +++ b/include/xrpl/nodestore/Factory.h @@ -14,7 +14,7 @@ namespace xrpl { class Section; } // namespace xrpl -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Base class for backend factories. @@ -70,4 +70,4 @@ public: } }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/Manager.h b/include/xrpl/nodestore/Manager.h index 54d99fe94b..1b869e4bca 100644 --- a/include/xrpl/nodestore/Manager.h +++ b/include/xrpl/nodestore/Manager.h @@ -10,7 +10,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Singleton for managing NodeStore factories and back ends. @@ -98,4 +98,4 @@ public: beast::Journal journal) = 0; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/NodeObject.h b/include/xrpl/nodestore/NodeObject.h index b96d65fa12..db139b9aa8 100644 --- a/include/xrpl/nodestore/NodeObject.h +++ b/include/xrpl/nodestore/NodeObject.h @@ -8,7 +8,7 @@ #include #include -// VFALCO NOTE Intentionally not in the NodeStore namespace +// VFALCO NOTE Intentionally not in the node_store namespace namespace xrpl { diff --git a/include/xrpl/nodestore/Scheduler.h b/include/xrpl/nodestore/Scheduler.h index 5d93a80eaa..40c36ce8ab 100644 --- a/include/xrpl/nodestore/Scheduler.h +++ b/include/xrpl/nodestore/Scheduler.h @@ -4,7 +4,7 @@ #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { enum class FetchType { Synchronous, Async }; @@ -71,4 +71,4 @@ public: onBatchWrite(BatchWriteReport const& report) = 0; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/Task.h b/include/xrpl/nodestore/Task.h index 59fe648476..22cec62eae 100644 --- a/include/xrpl/nodestore/Task.h +++ b/include/xrpl/nodestore/Task.h @@ -1,6 +1,6 @@ #pragma once -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Derived classes perform scheduled tasks. @@ -17,4 +17,4 @@ struct Task performScheduledTask() = 0; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/Types.h b/include/xrpl/nodestore/Types.h index 872d948a36..21af6fa68b 100644 --- a/include/xrpl/nodestore/Types.h +++ b/include/xrpl/nodestore/Types.h @@ -5,7 +5,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { // This is only used to pre-allocate the array for // batch objects and does not affect the amount written. @@ -36,4 +36,4 @@ enum class Status { */ using Batch = std::vector>; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/BatchWriter.h b/include/xrpl/nodestore/detail/BatchWriter.h index b89df0da14..6ac3428752 100644 --- a/include/xrpl/nodestore/detail/BatchWriter.h +++ b/include/xrpl/nodestore/detail/BatchWriter.h @@ -9,7 +9,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Batch-writing assist logic. @@ -86,4 +86,4 @@ private: Batch writeSet_; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/DatabaseNodeImp.h b/include/xrpl/nodestore/detail/DatabaseNodeImp.h index 6f2fca682f..33a2e27939 100644 --- a/include/xrpl/nodestore/detail/DatabaseNodeImp.h +++ b/include/xrpl/nodestore/detail/DatabaseNodeImp.h @@ -22,7 +22,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { class DatabaseNodeImp : public Database { @@ -68,7 +68,7 @@ public: XRPL_ASSERT( backend_, - "xrpl::NodeStore::DatabaseNodeImp::DatabaseNodeImp : non-null " + "xrpl::node_store::DatabaseNodeImp::DatabaseNodeImp : non-null " "backend"); } @@ -138,4 +138,4 @@ private: } }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/DatabaseRotatingImp.h b/include/xrpl/nodestore/detail/DatabaseRotatingImp.h index ecbe9a513d..9b566195ef 100644 --- a/include/xrpl/nodestore/detail/DatabaseRotatingImp.h +++ b/include/xrpl/nodestore/detail/DatabaseRotatingImp.h @@ -16,7 +16,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { class DatabaseRotatingImp : public DatabaseRotating { @@ -41,7 +41,7 @@ public: void rotate( - std::unique_ptr&& newBackend, + std::unique_ptr&& newBackend, std::function const& f) override; @@ -94,4 +94,4 @@ private: forEach(std::function)> f) override; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/DecodedBlob.h b/include/xrpl/nodestore/detail/DecodedBlob.h index d0cc5e3404..bd90ff2f1b 100644 --- a/include/xrpl/nodestore/detail/DecodedBlob.h +++ b/include/xrpl/nodestore/detail/DecodedBlob.h @@ -4,7 +4,7 @@ #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Parsed key/value blob into NodeObject components. @@ -49,4 +49,4 @@ private: int dataBytes_; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/EncodedBlob.h b/include/xrpl/nodestore/detail/EncodedBlob.h index d668cdccd8..d171df3cc9 100644 --- a/include/xrpl/nodestore/detail/EncodedBlob.h +++ b/include/xrpl/nodestore/detail/EncodedBlob.h @@ -12,7 +12,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { /** * Convert a NodeObject from in-memory to database format. @@ -68,7 +68,7 @@ class EncodedBlob public: explicit EncodedBlob(std::shared_ptr const& obj) : size_([&obj]() { - XRPL_ASSERT(obj, "xrpl::NodeStore::EncodedBlob::EncodedBlob : non-null input"); + XRPL_ASSERT(obj, "xrpl::node_store::EncodedBlob::EncodedBlob : non-null input"); if (!obj) throw std::runtime_error("EncodedBlob: unseated std::shared_ptr used."); @@ -88,7 +88,7 @@ public: XRPL_ASSERT( ((ptr_ == payload_.data()) && (size_ <= payload_.size())) || ((ptr_ != payload_.data()) && (size_ > payload_.size())), - "xrpl::NodeStore::EncodedBlob::~EncodedBlob : valid payload " + "xrpl::node_store::EncodedBlob::~EncodedBlob : valid payload " "pointer"); if (ptr_ != payload_.data()) @@ -114,4 +114,4 @@ public: } }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/ManagerImp.h b/include/xrpl/nodestore/detail/ManagerImp.h index fc84b0aa57..f1653b45dc 100644 --- a/include/xrpl/nodestore/detail/ManagerImp.h +++ b/include/xrpl/nodestore/detail/ManagerImp.h @@ -13,7 +13,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { class ManagerImp : public Manager { @@ -57,4 +57,4 @@ public: beast::Journal journal) override; }; -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/codec.h b/include/xrpl/nodestore/detail/codec.h index 2f69d532be..47ad2da50a 100644 --- a/include/xrpl/nodestore/detail/codec.h +++ b/include/xrpl/nodestore/detail/codec.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -21,7 +21,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { template std::pair @@ -59,7 +59,7 @@ lz4Compress(void const* in, std::size_t inSize, BufferFactory&& bf) using std::runtime_error; using namespace nudb::detail; std::pair result; - std::array::kMax> vi{}; + std::array::kMax> vi{}; auto const n = writeVarint(vi.data(), inSize); auto const outMax = LZ4_compressBound(inSize); auto* out = reinterpret_cast(bf(n + outMax)); @@ -240,7 +240,7 @@ nodeobjectCompress(void const* in, std::size_t inSize, BufferFactory&& bf) auto* out = reinterpret_cast(bf(result.second)); result.first = out; ostream os(out, result.second); - write(os, type); + write(os, type); write(os, mask); write(os, vh.data(), n * 32); return result; @@ -252,13 +252,13 @@ nodeobjectCompress(void const* in, std::size_t inSize, BufferFactory&& bf) auto* out = reinterpret_cast(bf(result.second)); result.first = out; ostream os(out, result.second); - write(os, type); + write(os, type); write(os, vh.data(), n * 32); return result; } } - std::array::kMax> vi{}; + std::array::kMax> vi{}; static constexpr std::size_t kCodecType = 1; auto const vn = writeVarint(vi.data(), kCodecType); @@ -269,7 +269,7 @@ nodeobjectCompress(void const* in, std::size_t inSize, BufferFactory&& bf) case 1: // lz4 { std::uint8_t* p = nullptr; - auto const lzr = NodeStore::lz4Compress(in, inSize, [&p, &vn, &bf](std::size_t n) { + auto const lzr = node_store::lz4Compress(in, inSize, [&p, &vn, &bf](std::size_t n) { p = reinterpret_cast(bf(vn + n)); return p + vn; }); @@ -316,4 +316,4 @@ filterInner(void* in, std::size_t inSize) } } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/include/xrpl/nodestore/detail/varint.h b/include/xrpl/nodestore/detail/varint.h deleted file mode 100644 index 5a65545d3a..0000000000 --- a/include/xrpl/nodestore/detail/varint.h +++ /dev/null @@ -1,126 +0,0 @@ -#pragma once - -#include - -#include -#include -#include - -namespace xrpl::NodeStore { - -// This is a variant of the base128 varint format from -// google protocol buffers: -// https://developers.google.com/protocol-buffers/docs/encoding#varints - -// field tag -struct varint; - -// Metafuncton to return largest -// possible size of T represented as varint. -// T must be unsigned -template > -struct varint_traits; - -template -struct varint_traits -{ - explicit varint_traits() = default; - - static constexpr std::size_t kMax = ((8 * sizeof(T)) + 6) / 7; -}; - -// Returns: Number of bytes consumed or 0 on error, -// if the buffer was too small or t overflowed. -// -template -std::size_t -readVarint(void const* buf, std::size_t buflen, std::size_t& t) -{ - if (buflen == 0) - return 0; - t = 0; - auto const* p = reinterpret_cast(buf); - std::size_t n = 0; - while (p[n] & 0x80) - { - if (++n >= buflen) - return 0; - } - if (++n > buflen) - return 0; - // Special case for 0 - if (n == 1 && *p == 0) - { - t = 0; - return 1; - } - auto const used = n; - while (n > 0) - { - --n; - auto const d = p[n]; - auto const t0 = t; - t *= 127; - t += d & 0x7f; - if (t <= t0) - return 0; // overflow - } - return used; -} - -template -std::size_t -sizeVarint(T v) - requires(std::is_unsigned_v) -{ - std::size_t n = 0; - do - { - v /= 127; - ++n; - } while (v != 0); - return n; -} - -template -std::size_t -writeVarint(void* p0, std::size_t v) -{ - // NOLINTNEXTLINE(misc-const-correctness) - auto* p = reinterpret_cast(p0); - do - { - std::uint8_t d = v % 127; - v /= 127; - if (v != 0) - d |= 0x80; - *p++ = d; - } while (v != 0); - return p - reinterpret_cast(p0); -} - -// input stream - -template -void -read(nudb::detail::istream& is, std::size_t& u) - requires(std::is_same_v) -{ - auto p0 = is(1); - auto p1 = p0; - while (*p1++ & 0x80) - is(1); - readVarint(p0, p1 - p0, u); -} - -// output stream - -template -void -write(nudb::detail::ostream& os, std::size_t t) - requires(std::is_same_v) -{ - writeVarint(os.data(sizeVarint(t)), t); -} - -} // namespace xrpl::NodeStore diff --git a/include/xrpl/peerfinder/Config.h b/include/xrpl/peerfinder/Config.h index 9ff0d342c3..3326ae8a97 100644 --- a/include/xrpl/peerfinder/Config.h +++ b/include/xrpl/peerfinder/Config.h @@ -9,7 +9,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { struct PeerLimitConfig { @@ -28,7 +28,7 @@ struct Config * This includes both inbound and outbound, but does not include * fixed peers. */ - std::size_t maxPeers{Tuning::kDefaultMaxPeers}; + std::size_t maxPeers{tuning::kDefaultMaxPeers}; /** * The number of automatic outbound connections to maintain. @@ -100,7 +100,7 @@ struct Config onWrite(beast::PropertyStream::Map& map) const; /** - * Make PeerFinder::Config from peer limit and server mode parameters. + * Make peer_finder::Config from peer limit and server mode parameters. */ static Config makeConfig( @@ -160,4 +160,4 @@ to_string(Result result) noexcept return "unknown"; } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/PeerfinderManager.h b/include/xrpl/peerfinder/PeerfinderManager.h index ed683520c1..bb03d85537 100644 --- a/include/xrpl/peerfinder/PeerfinderManager.h +++ b/include/xrpl/peerfinder/PeerfinderManager.h @@ -15,7 +15,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Maintains a set of IP addresses used for getting into the network. @@ -68,17 +68,17 @@ public: * file, along with the set of corresponding IP addresses. */ virtual void - addFixedPeer(std::string_view name, std::vector const& addresses) = 0; + addFixedPeer(std::string_view name, std::vector const& addresses) = 0; /** - * Add a set of strings as fallback IP::Endpoint sources. + * Add a set of strings as fallback ip::Endpoint sources. * @param name A label used for diagnostics. */ virtual void addFallbackStrings(std::string const& name, std::vector const& strings) = 0; /** - * Add a URL as a fallback location to obtain IP::Endpoint sources. + * Add a URL as a fallback location to obtain ip::Endpoint sources. * @param name A label used for diagnostics. */ /* VFALCO NOTE Unimplemented @@ -95,8 +95,8 @@ public: */ virtual std::pair, Result> newInboundSlot( - beast::IP::Endpoint const& localEndpoint, - beast::IP::Endpoint const& remoteEndpoint) = 0; + beast::ip::Endpoint const& localEndpoint, + beast::ip::Endpoint const& remoteEndpoint) = 0; /** * Create a new outbound slot with the specified remote endpoint. @@ -104,7 +104,7 @@ public: * Usually this is because of a duplicate connection. */ virtual std::pair, Result> - newOutboundSlot(beast::IP::Endpoint const& remoteEndpoint) = 0; + newOutboundSlot(beast::ip::Endpoint const& remoteEndpoint) = 0; /** * Called when mtENDPOINTS is received. @@ -145,7 +145,7 @@ public: * @return `true` if the connection should be kept */ virtual bool - onConnected(std::shared_ptr const& slot, beast::IP::Endpoint const& localEndpoint) = 0; + onConnected(std::shared_ptr const& slot, beast::ip::Endpoint const& localEndpoint) = 0; /** * Request an active slot type. @@ -162,7 +162,7 @@ public: /** * Return a set of addresses we should connect to. */ - virtual std::vector + virtual std::vector autoconnect() = 0; virtual std::vector, std::vector>> @@ -176,4 +176,4 @@ public: oncePerSecond() = 0; }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/Slot.h b/include/xrpl/peerfinder/Slot.h index 9db39ac94c..58e094afc4 100644 --- a/include/xrpl/peerfinder/Slot.h +++ b/include/xrpl/peerfinder/Slot.h @@ -7,7 +7,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Properties and state associated with a peer to peer overlay connection. @@ -52,13 +52,13 @@ public: /** * The remote endpoint of socket. */ - [[nodiscard]] virtual beast::IP::Endpoint const& + [[nodiscard]] virtual beast::ip::Endpoint const& remoteEndpoint() const = 0; /** * The local endpoint of the socket, when known. */ - [[nodiscard]] virtual std::optional const& + [[nodiscard]] virtual std::optional const& localEndpoint() const = 0; [[nodiscard]] virtual std::optional @@ -72,4 +72,4 @@ public: publicKey() const = 0; }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/Types.h b/include/xrpl/peerfinder/Types.h index 9e82d9d65c..1327f2564f 100644 --- a/include/xrpl/peerfinder/Types.h +++ b/include/xrpl/peerfinder/Types.h @@ -8,14 +8,14 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { using clock_type = beast::AbstractClock; /** * Represents a set of addresses. */ -using IPAddresses = std::vector; +using IPAddresses = std::vector; //------------------------------------------------------------------------------ @@ -26,10 +26,10 @@ struct Endpoint { Endpoint() = default; - Endpoint(beast::IP::Endpoint ep, std::uint32_t hops); + Endpoint(beast::ip::Endpoint ep, std::uint32_t hops); std::uint32_t hops = 0; - beast::IP::Endpoint address; + beast::ip::Endpoint address; }; inline bool @@ -43,4 +43,4 @@ operator<(Endpoint const& lhs, Endpoint const& rhs) */ using Endpoints = std::vector; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Bootcache.h b/include/xrpl/peerfinder/detail/Bootcache.h index 2141a374fa..453d9c22d2 100644 --- a/include/xrpl/peerfinder/detail/Bootcache.h +++ b/include/xrpl/peerfinder/detail/Bootcache.h @@ -14,7 +14,7 @@ #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Stores IP addresses useful for gaining initial connections. @@ -65,7 +65,7 @@ private: }; using left_t = boost::bimaps:: - unordered_set_of, std::equal_to<>>; + unordered_set_of, std::equal_to<>>; using right_t = boost::bimaps::multiset_of>; using map_type = boost::bimap; using value_type = map_type::value_type; @@ -73,11 +73,11 @@ private: struct Transform { using first_argument_type = map_type::right_map::const_iterator::value_type const&; - using result_type = beast::IP::Endpoint const&; + using result_type = beast::ip::Endpoint const&; explicit Transform() = default; - beast::IP::Endpoint const& + beast::ip::Endpoint const& operator()(map_type::right_map::const_iterator::value_type const& v) const { return v.get_left(); @@ -121,7 +121,7 @@ public: size() const; /** - * IP::Endpoint iterators that traverse in decreasing valence. + * ip::Endpoint iterators that traverse in decreasing valence. */ /** @{ */ [[nodiscard]] const_iterator @@ -146,25 +146,25 @@ public: * Add a newly-learned address to the cache. */ bool - insert(beast::IP::Endpoint const& endpoint); + insert(beast::ip::Endpoint const& endpoint); /** * Add a staticallyconfigured address to the cache. */ bool - insertStatic(beast::IP::Endpoint const& endpoint); + insertStatic(beast::ip::Endpoint const& endpoint); /** * Called when an outbound connection handshake completes. */ void - onSuccess(beast::IP::Endpoint const& endpoint); + onSuccess(beast::ip::Endpoint const& endpoint); /** * Called when an outbound connection attempt fails to handshake. */ void - onFailure(beast::IP::Endpoint const& endpoint); + onFailure(beast::ip::Endpoint const& endpoint); /** * Stores the cache in the persistent database on a timer. @@ -189,4 +189,4 @@ private: flagForUpdate(); }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Checker.h b/include/xrpl/peerfinder/detail/Checker.h index 28ec83adb1..e1ac1d44e0 100644 --- a/include/xrpl/peerfinder/detail/Checker.h +++ b/include/xrpl/peerfinder/detail/Checker.h @@ -11,7 +11,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Tests remote listening sockets to make sure they are connectable. @@ -104,7 +104,7 @@ public: */ template void - asyncConnect(beast::IP::Endpoint const& endpoint, Handler&& handler); + asyncConnect(beast::ip::Endpoint const& endpoint, Handler&& handler); private: void @@ -179,7 +179,7 @@ Checker::wait() template template void -Checker::asyncConnect(beast::IP::Endpoint const& endpoint, Handler&& handler) +Checker::asyncConnect(beast::ip::Endpoint const& endpoint, Handler&& handler) { auto const op = std::make_shared>(*this, ioContext_, std::forward(handler)); @@ -202,4 +202,4 @@ Checker::remove(BasicAsyncOp& op) cond_.notify_all(); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Counts.h b/include/xrpl/peerfinder/detail/Counts.h index ce78eadce4..035103463e 100644 --- a/include/xrpl/peerfinder/detail/Counts.h +++ b/include/xrpl/peerfinder/detail/Counts.h @@ -10,7 +10,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Direction of a slot count adjustment. @@ -50,7 +50,7 @@ public: // Must be handshaked and in the right state XRPL_ASSERT( s.state() == Slot::State::Connected || s.state() == Slot::State::Accept, - "xrpl::PeerFinder::Counts::can_activate : valid input state"); + "xrpl::peer_finder::Counts::can_activate : valid input state"); if (s.fixed() || s.reserved()) return true; @@ -67,9 +67,9 @@ public: [[nodiscard]] std::size_t attemptsNeeded() const { - if (attempts_ >= Tuning::kMaxConnectAttempts) + if (attempts_ >= tuning::kMaxConnectAttempts) return 0; - return Tuning::kMaxConnectAttempts - attempts_; + return tuning::kMaxConnectAttempts - attempts_; } /** @@ -295,7 +295,7 @@ private: switch (s.state()) { case Slot::State::Accept: - XRPL_ASSERT(s.inbound(), "xrpl::PeerFinder::Counts::adjust : input is inbound"); + XRPL_ASSERT(s.inbound(), "xrpl::peer_finder::Counts::adjust : input is inbound"); acceptCount_ += n; break; @@ -303,7 +303,7 @@ private: case Slot::State::Connected: XRPL_ASSERT( !s.inbound(), - "xrpl::PeerFinder::Counts::adjust : input is not " + "xrpl::peer_finder::Counts::adjust : input is not " "inbound"); attempts_ += n; break; @@ -331,7 +331,7 @@ private: // LCOV_EXCL_START default: - UNREACHABLE("xrpl::PeerFinder::Counts::adjust : invalid input state"); + UNREACHABLE("xrpl::peer_finder::Counts::adjust : invalid input state"); break; // LCOV_EXCL_STOP }; @@ -391,4 +391,4 @@ private: int closingCount_{0}; }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Fixed.h b/include/xrpl/peerfinder/detail/Fixed.h index 6754ec6dbd..5a52fd7c3e 100644 --- a/include/xrpl/peerfinder/detail/Fixed.h +++ b/include/xrpl/peerfinder/detail/Fixed.h @@ -7,7 +7,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Metadata for a Fixed slot. @@ -36,8 +36,8 @@ public: void failure(clock_type::time_point const& now) { - failures_ = std::min(failures_ + 1, Tuning::kConnectionBackoff.size() - 1); - when_ = now + std::chrono::minutes(Tuning::kConnectionBackoff[failures_]); + failures_ = std::min(failures_ + 1, tuning::kConnectionBackoff.size() - 1); + when_ = now + std::chrono::minutes(tuning::kConnectionBackoff[failures_]); } /** @@ -55,4 +55,4 @@ private: std::size_t failures_{0}; }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Handouts.h b/include/xrpl/peerfinder/detail/Handouts.h index cb5fd7f850..c20d4b2139 100644 --- a/include/xrpl/peerfinder/detail/Handouts.h +++ b/include/xrpl/peerfinder/detail/Handouts.h @@ -12,7 +12,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { namespace detail { @@ -28,7 +28,7 @@ template std::size_t handoutOne(Target& t, HopContainer& h) { - XRPL_ASSERT(!t.full(), "xrpl::PeerFinder::detail::handoutOne : target is not full"); + XRPL_ASSERT(!t.full(), "xrpl::peer_finder::detail::handoutOne : target is not full"); for (auto it = h.begin(); it != h.end(); ++it) { auto const& e = *it; @@ -95,7 +95,7 @@ public: [[nodiscard]] bool full() const { - return list_.size() >= Tuning::kRedirectEndpointCount; + return list_.size() >= tuning::kRedirectEndpointCount; } [[nodiscard]] SlotImp::ptr const& @@ -124,7 +124,7 @@ private: template RedirectHandouts::RedirectHandouts(SlotImp::ptr slot) : slot_(std::move(slot)) { - list_.reserve(Tuning::kRedirectEndpointCount); + list_.reserve(tuning::kRedirectEndpointCount); } template @@ -138,7 +138,7 @@ RedirectHandouts::tryInsert(Endpoint const& ep) // addresses in a peer HTTP handshake instead of // the tmENDPOINTS message. // - if (ep.hops > Tuning::kMaxHops) + if (ep.hops > tuning::kMaxHops) return false; // Don't send them our address @@ -181,7 +181,7 @@ public: [[nodiscard]] bool full() const { - return list_.size() >= Tuning::kNumberOfEndpoints; + return list_.size() >= tuning::kNumberOfEndpoints; } void @@ -210,7 +210,7 @@ private: template SlotHandouts::SlotHandouts(SlotImp::ptr slot) : slot_(std::move(slot)) { - list_.reserve(Tuning::kNumberOfEndpoints); + list_.reserve(tuning::kNumberOfEndpoints); } template @@ -220,7 +220,7 @@ SlotHandouts::tryInsert(Endpoint const& ep) if (full()) return false; - if (ep.hops > Tuning::kMaxHops) + if (ep.hops > tuning::kMaxHops) return false; if (slot_->recent.filter(ep.address, ep.hops)) @@ -259,9 +259,9 @@ class ConnectHandouts public: // Keeps track of addresses we have made outgoing connections // to, for the purposes of not connecting to them too frequently. - using Squelches = beast::aged_set; + using Squelches = beast::aged_set; - using list_type = std::vector; + using list_type = std::vector; private: std::size_t needed_; @@ -274,7 +274,7 @@ public: template bool - tryInsert(beast::IP::Endpoint const& endpoint); + tryInsert(beast::ip::Endpoint const& endpoint); [[nodiscard]] bool empty() const @@ -316,13 +316,13 @@ ConnectHandouts::ConnectHandouts(std::size_t needed, Squelches& squelches) template bool -ConnectHandouts::tryInsert(beast::IP::Endpoint const& endpoint) +ConnectHandouts::tryInsert(beast::ip::Endpoint const& endpoint) { if (full()) return false; // Make sure the address isn't already in our list - if (std::ranges::any_of(list_, [&endpoint](beast::IP::Endpoint const& other) { + if (std::ranges::any_of(list_, [&endpoint](beast::ip::Endpoint const& other) { // Ignore port for security reasons return other.address() == endpoint.address(); })) @@ -341,4 +341,4 @@ ConnectHandouts::tryInsert(beast::IP::Endpoint const& endpoint) return true; } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Livecache.h b/include/xrpl/peerfinder/detail/Livecache.h index cac284d1cc..ec797065e5 100644 --- a/include/xrpl/peerfinder/detail/Livecache.h +++ b/include/xrpl/peerfinder/detail/Livecache.h @@ -29,7 +29,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { template class Livecache; @@ -188,10 +188,10 @@ class Livecache : protected detail::LivecacheBase { private: using cache_type = beast::aged_map< - beast::IP::Endpoint, + beast::ip::Endpoint, Element, std::chrono::steady_clock, - std::less, + std::less, Allocator>; beast::Journal journal_; @@ -220,8 +220,8 @@ public: // but not given out (since they would exceed maxHops). They // are used for automatic connection attempts. // - using Histogram = std::array; - using lists_type = std::array; + using Histogram = std::array; + using lists_type = std::array; template struct Transform @@ -400,7 +400,7 @@ Livecache::expire() { std::size_t n(0); typename cache_type::time_point const expired( - cache_.clock().now() - Tuning::kLiveCacheSecondsToLive); + cache_.clock().now() - tuning::kLiveCacheSecondsToLive); for (auto iter(cache_.chronological.begin()); iter != cache_.chronological.end() && iter.when() <= expired;) { @@ -427,8 +427,8 @@ Livecache::insert(Endpoint const& ep) // when redirecting. // XRPL_ASSERT( - ep.hops <= (Tuning::kMaxHops + 1), - "xrpl::PeerFinder::Livecache::insert : maximum input hops"); + ep.hops <= (tuning::kMaxHops + 1), + "xrpl::peer_finder::Livecache::insert : maximum input hops"); auto result = cache_.emplace(ep.address, ep); Element& e(result.first->second); if (result.second) @@ -468,7 +468,7 @@ void Livecache::onWrite(beast::PropertyStream::Map& map) { typename cache_type::time_point const expired( - cache_.clock().now() - Tuning::kLiveCacheSecondsToLive); + cache_.clock().now() - tuning::kLiveCacheSecondsToLive); map["size"] = size(); map["hist"] = hops.histogram(); beast::PropertyStream::Set set("entries", map); @@ -527,8 +527,8 @@ void Livecache::HopsT::insert(Element& e) { XRPL_ASSERT( - e.endpoint.hops <= Tuning::kMaxHops + 1, - "xrpl::PeerFinder::Livecache::HopsT::insert : maximum input hops"); + e.endpoint.hops <= tuning::kMaxHops + 1, + "xrpl::peer_finder::Livecache::HopsT::insert : maximum input hops"); // This has security implications without a shuffle lists_[e.endpoint.hops].push_front(e); ++hist_[e.endpoint.hops]; @@ -539,8 +539,8 @@ void Livecache::HopsT::reinsert(Element& e, std::uint32_t numHops) { XRPL_ASSERT( - numHops <= Tuning::kMaxHops + 1, - "xrpl::PeerFinder::Livecache::HopsT::reinsert : maximum hops input"); + numHops <= tuning::kMaxHops + 1, + "xrpl::peer_finder::Livecache::HopsT::reinsert : maximum hops input"); auto& list = lists_[e.endpoint.hops]; list.erase(list.iterator_to(e)); @@ -561,4 +561,4 @@ Livecache::HopsT::remove(Element& e) list.erase(list.iterator_to(e)); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Logic.h b/include/xrpl/peerfinder/detail/Logic.h index c623263884..4821054280 100644 --- a/include/xrpl/peerfinder/detail/Logic.h +++ b/include/xrpl/peerfinder/detail/Logic.h @@ -43,7 +43,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * The Logic for maintaining the list of Slot addresses. @@ -57,7 +57,7 @@ public: // Maps remote endpoints to slots. Since a slot has a // remote endpoint upon construction, this holds all counts_. // - using Slots = std::map>; + using Slots = std::map>; beast::Journal journal; clock_type& clock; @@ -81,7 +81,7 @@ private: Counts counts_; // A list of slots that should always be connected - std::map fixed_; + std::map fixed_; public: // Live livecache from mtENDPOINTS messages @@ -96,7 +96,7 @@ public: // The addresses (but not port) we are connected to. This includes // outgoing connection attempts. Note that this set can contain // duplicates (since the port is not set) - std::multiset connectedAddresses; + std::multiset connectedAddresses; // Set of public keys belonging to active peers std::set keys; @@ -170,13 +170,13 @@ public: } void - addFixedPeer(std::string_view name, beast::IP::Endpoint const& ep) + addFixedPeer(std::string_view name, beast::ip::Endpoint const& ep) { - addFixedPeer(name, std::vector{ep}); + addFixedPeer(name, std::vector{ep}); } void - addFixedPeer(std::string_view name, std::vector const& addresses) + addFixedPeer(std::string_view name, std::vector const& addresses) { std::scoped_lock const _(lock); @@ -213,8 +213,8 @@ public: // Called when the Checker completes a connectivity test void checkComplete( - beast::IP::Endpoint const& remoteAddress, - beast::IP::Endpoint const& checkedAddress, + beast::ip::Endpoint const& remoteAddress, + beast::ip::Endpoint const& checkedAddress, boost::system::error_code ec) { if (ec == boost::asio::error::operation_aborted) @@ -256,8 +256,8 @@ public: std::pair newInboundSlot( - beast::IP::Endpoint const& localEndpoint, - beast::IP::Endpoint const& remoteEndpoint) + beast::ip::Endpoint const& localEndpoint, + beast::ip::Endpoint const& remoteEndpoint) { JLOG(journal.debug()) << std::left << std::setw(18) << "Logic accept" << remoteEndpoint << " on local " << localEndpoint; @@ -293,7 +293,7 @@ public: // Remote address must not already exist XRPL_ASSERT( result.second, - "xrpl::PeerFinder::Logic::new_inbound_slot : remote endpoint " + "xrpl::peer_finder::Logic::new_inbound_slot : remote endpoint " "inserted"); // Add to the connected address list connectedAddresses.emplace(remoteEndpoint.address()); @@ -306,7 +306,7 @@ public: // Can't check for self-connect because we don't know the local endpoint std::pair - newOutboundSlot(beast::IP::Endpoint const& remoteEndpoint) + newOutboundSlot(beast::ip::Endpoint const& remoteEndpoint) { JLOG(journal.debug()) << std::left << std::setw(18) << "Logic connect " << remoteEndpoint; @@ -329,7 +329,7 @@ public: // Remote address must not already exist XRPL_ASSERT( result.second, - "xrpl::PeerFinder::Logic::new_outbound_slot : remote endpoint " + "xrpl::peer_finder::Logic::new_outbound_slot : remote endpoint " "inserted"); // Add to the connected address list @@ -342,7 +342,7 @@ public: } bool - onConnected(SlotImp::ptr const& slot, beast::IP::Endpoint const& localEndpoint) + onConnected(SlotImp::ptr const& slot, beast::ip::Endpoint const& localEndpoint) { beast::WrappedSink sink{journal.sink(), slot->prefix()}; beast::Journal const journal{sink}; @@ -354,7 +354,7 @@ public: // The object must exist in our table XRPL_ASSERT( slots.contains(slot->remoteEndpoint()), - "xrpl::PeerFinder::Logic::onConnected : valid slot input"); + "xrpl::peer_finder::Logic::onConnected : valid slot input"); // Assign the local endpoint now that it's known slot->localEndpoint(localEndpoint); @@ -365,7 +365,7 @@ public: { XRPL_ASSERT( iter->second->localEndpoint() == slot->remoteEndpoint(), - "xrpl::PeerFinder::Logic::onConnected : local and remote " + "xrpl::peer_finder::Logic::onConnected : local and remote " "endpoints do match"); JLOG(journal.warn()) << "Logic dropping as self connect"; return false; @@ -393,11 +393,11 @@ public: // The object must exist in our table XRPL_ASSERT( slots.contains(slot->remoteEndpoint()), - "xrpl::PeerFinder::Logic::activate : valid slot input"); + "xrpl::peer_finder::Logic::activate : valid slot input"); // Must be accepted or connected XRPL_ASSERT( slot->state() == Slot::State::Accept || slot->state() == Slot::State::Connected, - "xrpl::PeerFinder::Logic::activate : valid slot state"); + "xrpl::peer_finder::Logic::activate : valid slot state"); // Check for duplicate connection by key if (keys.contains(key)) @@ -425,7 +425,7 @@ public: { [[maybe_unused]] bool const inserted = keys.insert(key).second; // Public key must not already exist - XRPL_ASSERT(inserted, "xrpl::PeerFinder::Logic::activate : public key inserted"); + XRPL_ASSERT(inserted, "xrpl::peer_finder::Logic::activate : public key inserted"); } // Change state and update counts @@ -443,7 +443,7 @@ public: if (iter == fixed_.end()) { logicError( - "PeerFinder::Logic::activate(): remote_endpoint " + "peer_finder::Logic::activate(): remote_endpoint " "missing from fixed_"); } @@ -476,10 +476,10 @@ public: // VFALCO TODO This should add the returned addresses to the // squelch list in one go once the list is built, // rather than having each module add to the squelch list. - std::vector + std::vector autoconnect() { - std::vector none; + std::vector none; std::scoped_lock const _(lock); @@ -635,7 +635,7 @@ public: // either. ipv6 has a slightly more compact string // representation of 0, so use that for self entries. ep.address = - beast::IP::Endpoint(beast::IP::AddressV6()).atPort(config_.listeningPort); + beast::ip::Endpoint(beast::ip::AddressV6()).atPort(config_.listeningPort); for (auto& t : targets) t.insert(ep); } @@ -656,7 +656,7 @@ public: result.emplace_back(slot, list); } - whenBroadcast = now + Tuning::kSecondsPerMessage; + whenBroadcast = now + tuning::kSecondsPerMessage; } return result; @@ -675,7 +675,7 @@ public: entry.second->expire(); // Expire the recent attempts table - beast::expire(squelches, Tuning::kRecentAttemptDuration); + beast::expire(squelches, tuning::kRecentAttemptDuration); bootcache.periodicActivity(); } @@ -692,7 +692,7 @@ public: Endpoint& ep(*iter); // Enforce hop limit - if (ep.hops > Tuning::kMaxHops) + if (ep.hops > tuning::kMaxHops) { JLOG(journal.debug()) << std::left << std::setw(18) << "Endpoints drop " << ep.address << " for excess hops " << ep.hops; @@ -754,10 +754,10 @@ public: beast::Journal const journal{sink}; // If we're sent too many endpoints, sample them at random: - if (list.size() > Tuning::kNumberOfEndpointsMax) + if (list.size() > tuning::kNumberOfEndpointsMax) { std::shuffle(list.begin(), list.end(), defaultPrng()); - list.resize(Tuning::kNumberOfEndpointsMax); + list.resize(tuning::kNumberOfEndpointsMax); } JLOG(journal.trace()) << "Endpoints contained " << list.size() @@ -768,12 +768,12 @@ public: // The object must exist in our table XRPL_ASSERT( slots.contains(slot->remoteEndpoint()), - "xrpl::PeerFinder::Logic::onEndpoints : valid slot input"); + "xrpl::peer_finder::Logic::onEndpoints : valid slot input"); // Must be handshaked! XRPL_ASSERT( slot->state() == Slot::State::Active, - "xrpl::PeerFinder::Logic::onEndpoints : valid slot state"); + "xrpl::peer_finder::Logic::onEndpoints : valid slot state"); clock_type::time_point const now(clock.now()); @@ -785,7 +785,7 @@ public: for (auto const& ep : list) { - XRPL_ASSERT(ep.hops, "xrpl::PeerFinder::Logic::onEndpoints : nonzero hops"); + XRPL_ASSERT(ep.hops, "xrpl::peer_finder::Logic::onEndpoints : nonzero hops"); slot->recent.insert(ep.address, ep.hops); @@ -837,7 +837,7 @@ public: bootcache.insert(ep.address); } - slot->whenAcceptEndpoints = now + Tuning::kSecondsPerMessage; + slot->whenAcceptEndpoints = now + tuning::kSecondsPerMessage; } //-------------------------------------------------------------------------- @@ -851,7 +851,7 @@ public: if (iter == slots.end()) { logicError( - "PeerFinder::Logic::remove(): remote_endpoint " + "peer_finder::Logic::remove(): remote_endpoint " "missing from slots_"); } @@ -866,7 +866,7 @@ public: if (iter == keys.end()) { logicError( - "PeerFinder::Logic::remove(): public_key missing " + "peer_finder::Logic::remove(): public_key missing " "from keys_"); } @@ -879,7 +879,7 @@ public: if (iter == connectedAddresses.end()) { logicError( - "PeerFinder::Logic::remove(): remote_endpoint " + "peer_finder::Logic::remove(): remote_endpoint " "address missing from connectedAddresses_"); } @@ -907,7 +907,7 @@ public: if (iter == fixed_.end()) { logicError( - "PeerFinder::Logic::on_closed(): remote_endpoint " + "peer_finder::Logic::on_closed(): remote_endpoint " "missing from fixed_"); } @@ -943,7 +943,7 @@ public: // LCOV_EXCL_START default: UNREACHABLE( - "xrpl::PeerFinder::Logic::on_closed : invalid slot " + "xrpl::peer_finder::Logic::on_closed : invalid slot " "state"); break; // LCOV_EXCL_STOP @@ -968,17 +968,17 @@ public: // Returns `true` if the address matches a fixed slot address // Must have the lock held bool - fixed(beast::IP::Endpoint const& endpoint) const + fixed(beast::ip::Endpoint const& endpoint) const { return std::ranges::any_of( fixed_, [&endpoint](auto const& entry) { return entry.first == endpoint; }); } // Returns `true` if the address matches a fixed slot address - // Note that this does not use the port information in the IP::Endpoint + // Note that this does not use the port information in the ip::Endpoint // Must have the lock held bool - fixed(beast::IP::Address const& address) const + fixed(beast::ip::Address const& address) const { return std::ranges::any_of( fixed_, [&address](auto const& entry) { return entry.first.address() == address; }); @@ -1097,9 +1097,9 @@ public: // //-------------------------------------------------------------------------- - // Returns true if the IP::Endpoint contains no invalid data. + // Returns true if the ip::Endpoint contains no invalid data. bool - isValidAddress(beast::IP::Endpoint const& address) + isValidAddress(beast::ip::Endpoint const& address) { if (isUnspecified(address)) return false; @@ -1220,7 +1220,7 @@ Logic::onRedirects( { std::scoped_lock const _(lock); std::size_t n = 0; - for (; first != last && n < Tuning::kMaxRedirects; ++first, ++n) + for (; first != last && n < tuning::kMaxRedirects; ++first, ++n) bootcache.insert(beast::IPAddressConversion::fromAsio(*first)); if (n > 0) { @@ -1229,4 +1229,4 @@ Logic::onRedirects( } } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/SlotImp.h b/include/xrpl/peerfinder/detail/SlotImp.h index 35c61b13cf..db86183f64 100644 --- a/include/xrpl/peerfinder/detail/SlotImp.h +++ b/include/xrpl/peerfinder/detail/SlotImp.h @@ -12,7 +12,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { class SlotImp : public Slot { @@ -21,13 +21,13 @@ public: // inbound SlotImp( - beast::IP::Endpoint const& localEndpoint, - beast::IP::Endpoint remoteEndpoint, + beast::ip::Endpoint const& localEndpoint, + beast::ip::Endpoint remoteEndpoint, bool fixed, clock_type& clock); // outbound - SlotImp(beast::IP::Endpoint remoteEndpoint, bool fixed, clock_type& clock); + SlotImp(beast::ip::Endpoint remoteEndpoint, bool fixed, clock_type& clock); bool inbound() const override @@ -53,13 +53,13 @@ public: return state_; } - beast::IP::Endpoint const& + beast::ip::Endpoint const& remoteEndpoint() const override { return remoteEndpoint_; } - std::optional const& + std::optional const& localEndpoint() const override { return localEndpoint_; @@ -93,13 +93,13 @@ public: } void - localEndpoint(beast::IP::Endpoint const& endpoint) + localEndpoint(beast::ip::Endpoint const& endpoint) { localEndpoint_ = endpoint; } void - remoteEndpoint(beast::IP::Endpoint const& endpoint) + remoteEndpoint(beast::ip::Endpoint const& endpoint) { remoteEndpoint_ = endpoint; } @@ -140,20 +140,20 @@ public: * sending a slot the same address too frequently. */ void - insert(beast::IP::Endpoint const& ep, std::uint32_t hops); + insert(beast::ip::Endpoint const& ep, std::uint32_t hops); /** * Returns `true` if we should not send endpoint to the slot. */ bool - filter(beast::IP::Endpoint const& ep, std::uint32_t hops); + filter(beast::ip::Endpoint const& ep, std::uint32_t hops); private: void expire(); friend class SlotImp; - beast::aged_unordered_map cache_; + beast::aged_unordered_map cache_; } recent; void @@ -167,8 +167,8 @@ private: bool const fixed_; bool reserved_; State state_; - beast::IP::Endpoint remoteEndpoint_; - std::optional localEndpoint_; + beast::ip::Endpoint remoteEndpoint_; + std::optional localEndpoint_; std::optional publicKey_; static std::int32_t constexpr kUnknownPort = -1; @@ -196,4 +196,4 @@ public: clock_type::time_point whenAcceptEndpoints; }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Source.h b/include/xrpl/peerfinder/detail/Source.h index 5cdb535bdd..09aa4e216a 100644 --- a/include/xrpl/peerfinder/detail/Source.h +++ b/include/xrpl/peerfinder/detail/Source.h @@ -7,7 +7,7 @@ #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * A static or dynamic source of peer addresses. @@ -46,4 +46,4 @@ public: fetch(Results& results, beast::Journal journal) = 0; }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/SourceStrings.h b/include/xrpl/peerfinder/detail/SourceStrings.h index 325a024764..e9783c775f 100644 --- a/include/xrpl/peerfinder/detail/SourceStrings.h +++ b/include/xrpl/peerfinder/detail/SourceStrings.h @@ -6,7 +6,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Provides addresses from a static set of strings. @@ -22,4 +22,4 @@ public: make(std::string const& name, Strings const& strings); }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Store.h b/include/xrpl/peerfinder/detail/Store.h index 9393ef6c2b..1f9352c6ec 100644 --- a/include/xrpl/peerfinder/detail/Store.h +++ b/include/xrpl/peerfinder/detail/Store.h @@ -6,7 +6,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Abstract persistence for PeerFinder data. @@ -17,7 +17,7 @@ public: virtual ~Store() = default; // load the bootstrap cache - using load_callback = std::function; + using load_callback = std::function; virtual std::size_t load(load_callback const& cb) = 0; @@ -26,11 +26,11 @@ public: { explicit Entry() = default; - beast::IP::Endpoint endpoint; + beast::ip::Endpoint endpoint; int valence{}; }; virtual void save(std::vector const& v) = 0; }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/peerfinder/detail/Tuning.h b/include/xrpl/peerfinder/detail/Tuning.h index ea4637dd9d..b4ccfae167 100644 --- a/include/xrpl/peerfinder/detail/Tuning.h +++ b/include/xrpl/peerfinder/detail/Tuning.h @@ -9,7 +9,7 @@ * Heuristically tuned constants. */ /** @{ */ -namespace xrpl::PeerFinder::Tuning { +namespace xrpl::peer_finder::tuning { //--------------------------------------------------------- // @@ -111,5 +111,5 @@ constexpr std::chrono::seconds kLiveCacheSecondsToLive(30); // Note that we ignore the port for purposes of comparison. constexpr std::chrono::seconds kRecentAttemptDuration(60); -} // namespace xrpl::PeerFinder::Tuning +} // namespace xrpl::peer_finder::tuning /** @} */ diff --git a/include/xrpl/peerfinder/make_Manager.h b/include/xrpl/peerfinder/make_Manager.h index 5da372e588..e514734d8b 100644 --- a/include/xrpl/peerfinder/make_Manager.h +++ b/include/xrpl/peerfinder/make_Manager.h @@ -10,7 +10,7 @@ #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * @brief Create a new Manager. @@ -33,4 +33,4 @@ makeManager( Store& store, beast::insight::Collector::ptr const& collector); -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/include/xrpl/proto/xrpl.proto b/include/xrpl/proto/xrpl.proto index d49920201e..bef5ec1d76 100644 --- a/include/xrpl/proto/xrpl.proto +++ b/include/xrpl/proto/xrpl.proto @@ -246,7 +246,15 @@ message TMGetObjectByHash { message TMLedgerNode { required bytes nodedata = 1; - optional bytes nodeid = 2; // missing for ledger base data + + // Used when protocol version <2.3. Not set for ledger base data. + optional bytes nodeid = 2; + + // Used when protocol version >=2.3. Neither value is set for ledger base data. + oneof reference { + bytes id = 3; // Set for inner nodes. + uint32 depth = 4; // Set for leaf nodes. + } } enum TMLedgerInfoType { diff --git a/include/xrpl/protocol/ApiVersion.h b/include/xrpl/protocol/ApiVersion.h index c3292e6074..b52e705b38 100644 --- a/include/xrpl/protocol/ApiVersion.h +++ b/include/xrpl/protocol/ApiVersion.h @@ -33,7 +33,7 @@ namespace xrpl { * Command line Requests use apiCommandLineVersion. */ -namespace RPC { +namespace rpc { template static constexpr std::integral_constant kApiVersion = {}; @@ -60,7 +60,7 @@ static_assert(kApiMaximumValidVersion >= kApiMaximumSupportedVersion); inline void setVersion(json::Value& parent, unsigned int apiVersion, bool betaEnabled) { - XRPL_ASSERT(apiVersion != kApiInvalidVersion, "xrpl::RPC::setVersion : input is valid"); + XRPL_ASSERT(apiVersion != kApiInvalidVersion, "xrpl::rpc::setVersion : input is valid"); auto& retObj = parent[jss::version] = json::ValueType::Object; @@ -99,12 +99,12 @@ setVersion(json::Value& parent, unsigned int apiVersion, bool betaEnabled) inline unsigned int getAPIVersionNumber(json::Value const& jv, bool betaEnabled) { - static json::Value const kMinVersion(RPC::kApiMinimumSupportedVersion); + static json::Value const kMinVersion(rpc::kApiMinimumSupportedVersion); json::Value const maxVersion( - betaEnabled ? RPC::kApiBetaVersion : RPC::kApiMaximumSupportedVersion); + betaEnabled ? rpc::kApiBetaVersion : rpc::kApiMaximumSupportedVersion); if (!jv.isObject() || !jv.isMember(jss::api_version)) - return RPC::kApiVersionIfUnspecified; + return rpc::kApiVersionIfUnspecified; try { @@ -113,33 +113,33 @@ getAPIVersionNumber(json::Value const& jv, bool betaEnabled) { case json::ValueType::Int: if (rawVersion.asInt() < 0) - return RPC::kApiInvalidVersion; + return rpc::kApiInvalidVersion; [[fallthrough]]; case json::ValueType::UInt: { auto const apiVersion = rawVersion.asUInt(); if (apiVersion < kMinVersion || apiVersion > maxVersion) - return RPC::kApiInvalidVersion; + return rpc::kApiInvalidVersion; return apiVersion; } default: - return RPC::kApiInvalidVersion; + return rpc::kApiInvalidVersion; } } catch (...) { - return RPC::kApiInvalidVersion; + return rpc::kApiInvalidVersion; } } -} // namespace RPC +} // namespace rpc template void forApiVersions(Fn const& fn, Args&&... args) requires // (MaxVer >= MinVer) && // - (MinVer >= RPC::kApiMinimumSupportedVersion) && // - (RPC::kApiMaximumValidVersion >= MaxVer) && requires { + (MinVer >= rpc::kApiMinimumSupportedVersion) && // + (rpc::kApiMaximumValidVersion >= MaxVer) && requires { fn(std::integral_constant{}, std::forward(args)...); fn(std::integral_constant{}, std::forward(args)...); } @@ -158,11 +158,11 @@ template void forAllApiVersions(Fn const& fn, Args&&... args) requires requires { - forApiVersions( + forApiVersions( fn, std::forward(args)...); } { - forApiVersions( + forApiVersions( fn, std::forward(args)...); } diff --git a/include/xrpl/protocol/BuildInfo.h b/include/xrpl/protocol/BuildInfo.h index 18ba20f23c..c3f90d8f9f 100644 --- a/include/xrpl/protocol/BuildInfo.h +++ b/include/xrpl/protocol/BuildInfo.h @@ -8,7 +8,7 @@ * Versioning information for this build. */ // VFALCO The namespace is deprecated -namespace xrpl::BuildInfo { +namespace xrpl::build_info { /** * Server version. @@ -84,4 +84,4 @@ isXrpldVersion(std::uint64_t version); bool isNewerVersion(std::uint64_t version); -} // namespace xrpl::BuildInfo +} // namespace xrpl::build_info diff --git a/include/xrpl/protocol/ErrorCodes.h b/include/xrpl/protocol/ErrorCodes.h index 8ac7c8c58f..465b6d711f 100644 --- a/include/xrpl/protocol/ErrorCodes.h +++ b/include/xrpl/protocol/ErrorCodes.h @@ -167,7 +167,7 @@ enum WarningCodeI { // VFALCO NOTE these should probably not be in the RPC namespace. -namespace RPC { +namespace rpc { /** * Maps an rpc error code to its token, default message, and HTTP status. @@ -337,7 +337,7 @@ containsError(json::Value const& json); int errorCodeHttpStatus(ErrorCodeI code); -} // namespace RPC +} // namespace rpc /** * Returns a single string with the contents of an RPC error. diff --git a/include/xrpl/protocol/MultiApiJson.h b/include/xrpl/protocol/MultiApiJson.h index 9a4882ec55..a0029fa491 100644 --- a/include/xrpl/protocol/MultiApiJson.h +++ b/include/xrpl/protocol/MultiApiJson.h @@ -188,6 +188,6 @@ struct MultiApiJson // Wrapper for Json for all supported API versions. using MultiApiJson = - detail::MultiApiJson; + detail::MultiApiJson; } // namespace xrpl diff --git a/include/xrpl/protocol/NFTSyntheticSerializer.h b/include/xrpl/protocol/NFTSyntheticSerializer.h index bef05b9a8f..df4fedb707 100644 --- a/include/xrpl/protocol/NFTSyntheticSerializer.h +++ b/include/xrpl/protocol/NFTSyntheticSerializer.h @@ -6,7 +6,7 @@ #include -namespace xrpl::RPC { +namespace xrpl::rpc { /** * Adds common synthetic fields to transaction-related JSON responses @@ -16,4 +16,4 @@ void insertNFTSyntheticInJson(json::Value&, std::shared_ptr const&, TxMeta const&); /** @} */ -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/include/xrpl/protocol/Protocol.h b/include/xrpl/protocol/Protocol.h index fe44b38bb8..fbfa75b988 100644 --- a/include/xrpl/protocol/Protocol.h +++ b/include/xrpl/protocol/Protocol.h @@ -139,7 +139,7 @@ tenthBipsOfValue(T value, TenthBips bips) return value * bips.value() / kTenthBipsPerUnity.value(); } -namespace Lending { +namespace lending { /** * The maximum management fee rate allowed by a loan broker in 1/10 bips. * @@ -236,7 +236,7 @@ static constexpr int kLoanPaymentsPerFeeIncrement = 5; * without an amendment */ static constexpr int kLoanMaximumPaymentsPerTransaction = 100; -} // namespace Lending +} // namespace lending /** * The maximum length of a URI inside an NFT @@ -316,6 +316,17 @@ constexpr std::uint8_t kVaultDefaultIouScale = 6; */ constexpr std::uint8_t kVaultMaximumIouScale = 18; +/** + * Vault ledger-entry schema versions. Assigned to newly created + * Vaults once featureLendingProtocolV1_1 is enabled. Vaults created before + * activation are left without LEVersion (implicit legacy version 0, + * accrual-basis accounting). + */ +enum class VaultVersion : uint8_t { + Legacy = 0, + CashBasis, +}; + /** * Maximum recursion depth for vault shares being put as an asset inside * another vault; counted from 0 diff --git a/include/xrpl/protocol/PublicKey.h b/include/xrpl/protocol/PublicKey.h index 98301af487..833078d741 100644 --- a/include/xrpl/protocol/PublicKey.h +++ b/include/xrpl/protocol/PublicKey.h @@ -260,7 +260,7 @@ calcAccountID(PublicKey const& pk); inline std::string getFingerprint( - beast::IP::Endpoint const& address, + beast::ip::Endpoint const& address, std::optional const& publicKey = std::nullopt, std::optional const& id = std::nullopt) { diff --git a/include/xrpl/protocol/XChainAttestations.h b/include/xrpl/protocol/XChainAttestations.h index 8f1c7a4ce3..ed8ffeb88e 100644 --- a/include/xrpl/protocol/XChainAttestations.h +++ b/include/xrpl/protocol/XChainAttestations.h @@ -20,7 +20,7 @@ namespace xrpl { -namespace Attestations { +namespace attestations { struct AttestationBase { @@ -227,7 +227,7 @@ struct CmpByCreateCount } }; -}; // namespace Attestations +}; // namespace attestations // Result when checking when two attestation match. enum class AttestationMatch { @@ -241,7 +241,7 @@ enum class AttestationMatch { struct XChainClaimAttestation { - using TSignedAttestation = Attestations::AttestationClaim; + using TSignedAttestation = attestations::AttestationClaim; static SField const& arrayFieldName; AccountID keyAccount; @@ -297,7 +297,7 @@ struct XChainClaimAttestation struct XChainCreateAccountAttestation { - using TSignedAttestation = Attestations::AttestationCreateAccount; + using TSignedAttestation = attestations::AttestationCreateAccount; static SField const& arrayFieldName; AccountID keyAccount; diff --git a/include/xrpl/protocol/detail/ledger_entries.macro b/include/xrpl/protocol/detail/ledger_entries.macro index 90810e06d2..b6408581a9 100644 --- a/include/xrpl/protocol/detail/ledger_entries.macro +++ b/include/xrpl/protocol/detail/ledger_entries.macro @@ -505,6 +505,7 @@ LEDGER_ENTRY(ltVAULT, 0x0084, Vault, vault, ({ {sfShareMPTID, SoeRequired}, {sfWithdrawalPolicy, SoeRequired}, {sfScale, SoeDefault}, + {sfLEVersion, SoeDefault}, // no SharesTotal ever (use MPTIssuance.sfOutstandingAmount) // no PermissionedDomainID ever (use MPTIssuance.sfDomainID) })) diff --git a/include/xrpl/protocol/detail/sfields.macro b/include/xrpl/protocol/detail/sfields.macro index 4ef76c8b75..16defe3ba3 100644 --- a/include/xrpl/protocol/detail/sfields.macro +++ b/include/xrpl/protocol/detail/sfields.macro @@ -18,6 +18,7 @@ TYPED_SFIELD(sfMethod, UINT8, 2) TYPED_SFIELD(sfTransactionResult, UINT8, 3) TYPED_SFIELD(sfScale, UINT8, 4) TYPED_SFIELD(sfAssetScale, UINT8, 5) +TYPED_SFIELD(sfLEVersion, UINT8, 6) // 8-bit integers (uncommon) TYPED_SFIELD(sfTickSize, UINT8, 16) diff --git a/include/xrpl/protocol_autogen/ledger_entries/Vault.h b/include/xrpl/protocol_autogen/ledger_entries/Vault.h index 2bf92b4f5d..a6ab54cb0a 100644 --- a/include/xrpl/protocol_autogen/ledger_entries/Vault.h +++ b/include/xrpl/protocol_autogen/ledger_entries/Vault.h @@ -287,6 +287,30 @@ public: { return this->sle_->isFieldPresent(sfScale); } + + /** + * @brief Get sfLEVersion (SoeDefault) + * @return The field value, or std::nullopt if not present. + */ + [[nodiscard]] + protocol_autogen::Optional + getLEVersion() const + { + if (hasLEVersion()) + return this->sle_->at(sfLEVersion); + return std::nullopt; + } + + /** + * @brief Check if sfLEVersion is present. + * @return True if the field is present, false otherwise. + */ + [[nodiscard]] + bool + hasLEVersion() const + { + return this->sle_->isFieldPresent(sfLEVersion); + } }; /** @@ -508,6 +532,17 @@ public: return *this; } + /** + * @brief Set sfLEVersion (SoeDefault) + * @return Reference to this builder for method chaining. + */ + VaultBuilder& + setLEVersion(std::decay_t const& value) + { + object_[sfLEVersion] = value; + return *this; + } + /** * @brief Build and return the completed Vault wrapper. * @param index The ledger entry index. diff --git a/include/xrpl/resource/Charge.h b/include/xrpl/resource/Charge.h index 12ea548fd2..b5bb8dd52e 100644 --- a/include/xrpl/resource/Charge.h +++ b/include/xrpl/resource/Charge.h @@ -4,7 +4,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { /** * A consumption charge. @@ -32,7 +32,7 @@ public: label() const; /** - * Return the cost of the charge in Resource::Manager units. + * Return the cost of the charge in resource::Manager units. */ [[nodiscard]] value_type cost() const; @@ -60,4 +60,4 @@ private: std::ostream& operator<<(std::ostream& os, Charge const& v); -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/Consumer.h b/include/xrpl/resource/Consumer.h index 9abcbffc82..01539e3a39 100644 --- a/include/xrpl/resource/Consumer.h +++ b/include/xrpl/resource/Consumer.h @@ -8,7 +8,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { struct Entry; class Logic; @@ -96,4 +96,4 @@ private: std::ostream& operator<<(std::ostream& os, Consumer const& v); -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/Disposition.h b/include/xrpl/resource/Disposition.h index cd5bceafa5..28dd4edf62 100644 --- a/include/xrpl/resource/Disposition.h +++ b/include/xrpl/resource/Disposition.h @@ -1,6 +1,6 @@ #pragma once -namespace xrpl::Resource { +namespace xrpl::resource { /** * The disposition of a consumer after applying a load charge. @@ -24,4 +24,4 @@ enum class Disposition { Drop }; -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/Fees.h b/include/xrpl/resource/Fees.h index 5001b504d6..411169253d 100644 --- a/include/xrpl/resource/Fees.h +++ b/include/xrpl/resource/Fees.h @@ -2,7 +2,7 @@ #include -namespace xrpl::Resource { +namespace xrpl::resource { /** * Schedule of fees charged for imposing load on the server. @@ -31,4 +31,4 @@ extern Charge const kFeeWarning; // The cost of receiving a warning. extern Charge const kFeeDrop; // The cost of being dropped for excess load. /** @} */ -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/Gossip.h b/include/xrpl/resource/Gossip.h index 4ad5852de0..0d8ccb100c 100644 --- a/include/xrpl/resource/Gossip.h +++ b/include/xrpl/resource/Gossip.h @@ -4,7 +4,7 @@ #include -namespace xrpl::Resource { +namespace xrpl::resource { /** * Data format for exchanging consumption information across peers. @@ -21,10 +21,10 @@ struct Gossip explicit Item() = default; int balance{}; - beast::IP::Endpoint address; + beast::ip::Endpoint address; }; std::vector items; }; -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/README.md b/include/xrpl/resource/README.md index 545d4e9ca0..96b6c3d603 100644 --- a/include/xrpl/resource/README.md +++ b/include/xrpl/resource/README.md @@ -1,4 +1,4 @@ -# Resource::Manager +# resource::Manager The ResourceManager module has these responsibilities: @@ -36,7 +36,7 @@ to the general public. ## Consumer Types Consumers are placed into three classifications (as identified by the -Resource::Kind enumeration): +resource::Kind enumeration): - InBound, - OutBound, and @@ -72,6 +72,6 @@ drop connections to those IP addresses that occur commonly in the gossip. ## Access -In xrpld, the Application holds a unique instance of Resource::Manager, +In xrpld, the Application holds a unique instance of resource::Manager, which may be retrieved by calling the method `Application::getResourceManager()`. diff --git a/include/xrpl/resource/ResourceManager.h b/include/xrpl/resource/ResourceManager.h index 03aab60c75..267cfb16e3 100644 --- a/include/xrpl/resource/ResourceManager.h +++ b/include/xrpl/resource/ResourceManager.h @@ -14,7 +14,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { /** * Tracks load and resource consumption. @@ -32,10 +32,10 @@ public: * IP if proxied. */ virtual Consumer - newInboundEndpoint(beast::IP::Endpoint const& address) = 0; + newInboundEndpoint(beast::ip::Endpoint const& address) = 0; virtual Consumer newInboundEndpoint( - beast::IP::Endpoint const& address, + beast::ip::Endpoint const& address, bool const proxy, std::string_view forwardedFor) = 0; @@ -43,13 +43,13 @@ public: * Create a new endpoint keyed by outbound IP address and port. */ virtual Consumer - newOutboundEndpoint(beast::IP::Endpoint const& address) = 0; + newOutboundEndpoint(beast::ip::Endpoint const& address) = 0; /** * Create a new unlimited endpoint keyed by forwarded IP. */ virtual Consumer - newUnlimitedEndpoint(beast::IP::Endpoint const& address) = 0; + newUnlimitedEndpoint(beast::ip::Endpoint const& address) = 0; /** * Extract packaged consumer information for export. @@ -78,4 +78,4 @@ public: std::unique_ptr makeManager(beast::insight::Collector::ptr const& collector, beast::Journal journal); -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/detail/Entry.h b/include/xrpl/resource/detail/Entry.h index 1336bda6ab..ec5a328b8b 100644 --- a/include/xrpl/resource/detail/Entry.h +++ b/include/xrpl/resource/detail/Entry.h @@ -12,7 +12,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { using clock_type = beast::AbstractClock; @@ -91,4 +91,4 @@ operator<<(std::ostream& os, Entry const& v) return os; } -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/detail/Import.h b/include/xrpl/resource/detail/Import.h index b19dbc4d1a..c5366146c1 100644 --- a/include/xrpl/resource/detail/Import.h +++ b/include/xrpl/resource/detail/Import.h @@ -5,7 +5,7 @@ #include -namespace xrpl::Resource { +namespace xrpl::resource { /** * A set of imported consumer data from a gossip origin. @@ -32,4 +32,4 @@ struct Import std::vector items; }; -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/detail/Key.h b/include/xrpl/resource/detail/Key.h index a0f11422a7..180e868319 100644 --- a/include/xrpl/resource/detail/Key.h +++ b/include/xrpl/resource/detail/Key.h @@ -7,17 +7,17 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { // The consumer key struct Key { Kind kind; - beast::IP::Endpoint address; + beast::ip::Endpoint address; Key() = delete; - Key(Kind k, beast::IP::Endpoint addr) : kind(k), address(std::move(addr)) + Key(Kind k, beast::ip::Endpoint addr) : kind(k), address(std::move(addr)) { } @@ -47,4 +47,4 @@ struct Key }; }; -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/detail/Kind.h b/include/xrpl/resource/detail/Kind.h index ce2e0773cf..9af760d252 100644 --- a/include/xrpl/resource/detail/Kind.h +++ b/include/xrpl/resource/detail/Kind.h @@ -1,6 +1,6 @@ #pragma once -namespace xrpl::Resource { +namespace xrpl::resource { /** * Kind of consumer. @@ -12,4 +12,4 @@ namespace xrpl::Resource { */ enum class Kind { Inbound, Outbound, Unlimited }; -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/detail/Logic.h b/include/xrpl/resource/detail/Logic.h index 3f36ad84a3..aaaeb4fdd1 100644 --- a/include/xrpl/resource/detail/Logic.h +++ b/include/xrpl/resource/detail/Logic.h @@ -24,7 +24,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { class Logic { @@ -96,7 +96,7 @@ public: } Consumer - newInboundEndpoint(beast::IP::Endpoint const& address) + newInboundEndpoint(beast::ip::Endpoint const& address) { Entry* entry(nullptr); @@ -126,7 +126,7 @@ public: } Consumer - newOutboundEndpoint(beast::IP::Endpoint const& address) + newOutboundEndpoint(beast::ip::Endpoint const& address) { Entry* entry(nullptr); @@ -159,7 +159,7 @@ public: * enabled. */ Consumer - newUnlimitedEndpoint(beast::IP::Endpoint const& address) + newUnlimitedEndpoint(beast::ip::Endpoint const& address) { Entry* entry(nullptr); @@ -387,7 +387,7 @@ public: { std::scoped_lock const _(lock_); Entry& entry(iter->second); - XRPL_ASSERT(entry.refcount == 0, "xrpl::Resource::Logic::erase : entry not used"); + XRPL_ASSERT(entry.refcount == 0, "xrpl::resource::Logic::erase : entry not used"); inactive_.erase(inactive_.iteratorTo(entry)); table_.erase(iter); } @@ -421,7 +421,7 @@ public: default: // LCOV_EXCL_START UNREACHABLE( - "xrpl::Resource::Logic::release : invalid entry " + "xrpl::resource::Logic::release : invalid entry " "kind"); break; // LCOV_EXCL_STOP @@ -440,7 +440,7 @@ public: static_assert( kFeeLogAsWarn > kFeeLogAsInfo && kFeeLogAsInfo > kFeeLogAsDebug && kFeeLogAsDebug > 10); - static auto kGetStream = [](Resource::Charge::value_type cost, beast::Journal& journal) { + static auto kGetStream = [](resource::Charge::value_type cost, beast::Journal& journal) { if (cost >= kFeeLogAsWarn) return journal.warn(); if (cost >= kFeeLogAsInfo) @@ -564,4 +564,4 @@ public: } }; -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/resource/detail/Tuning.h b/include/xrpl/resource/detail/Tuning.h index 62f7fa3f9d..d631aaddba 100644 --- a/include/xrpl/resource/detail/Tuning.h +++ b/include/xrpl/resource/detail/Tuning.h @@ -2,7 +2,7 @@ #include -namespace xrpl::Resource { +namespace xrpl::resource { /** * Tunable constants. @@ -26,4 +26,4 @@ static constexpr std::chrono::seconds kSecondsUntilExpiration{300}; // Number of seconds until imported gossip expires static constexpr std::chrono::seconds kGossipExpirationSeconds{30}; -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/include/xrpl/server/InfoSub.h b/include/xrpl/server/InfoSub.h index 2e9bd857c7..4bf88cd53b 100644 --- a/include/xrpl/server/InfoSub.h +++ b/include/xrpl/server/InfoSub.h @@ -62,7 +62,7 @@ public: using ref = std::shared_ptr const&; - using Consumer = Resource::Consumer; + using Consumer = resource::Consumer; public: /** diff --git a/include/xrpl/server/Session.h b/include/xrpl/server/Session.h index be8d9a497c..03ac767c25 100644 --- a/include/xrpl/server/Session.h +++ b/include/xrpl/server/Session.h @@ -52,7 +52,7 @@ public: /** * Returns the remote address of the connection. */ - virtual beast::IP::Endpoint + virtual beast::ip::Endpoint remoteAddress() = 0; /** diff --git a/include/xrpl/server/detail/BaseHTTPPeer.h b/include/xrpl/server/detail/BaseHTTPPeer.h index c7553c1da3..6020d3cc65 100644 --- a/include/xrpl/server/detail/BaseHTTPPeer.h +++ b/include/xrpl/server/detail/BaseHTTPPeer.h @@ -157,7 +157,7 @@ protected: return port_; } - beast::IP::Endpoint + beast::ip::Endpoint remoteAddress() override { return beast::IPAddressConversion::fromAsio(remoteAddress_); diff --git a/include/xrpl/server/detail/BaseWSPeer.h b/include/xrpl/server/detail/BaseWSPeer.h index 59a866ab8c..b1670865bd 100644 --- a/include/xrpl/server/detail/BaseWSPeer.h +++ b/include/xrpl/server/detail/BaseWSPeer.h @@ -196,7 +196,7 @@ BaseWSPeer::run() startTimer(); closeOnTimer_ = true; impl().ws_.set_option(boost::beast::websocket::stream_base::decorator([](auto& res) { - res.set(boost::beast::http::field::server, BuildInfo::getFullVersionString()); + res.set(boost::beast::http::field::server, build_info::getFullVersionString()); })); impl().ws_.async_accept( request_, bind_executor(strand_, [self = impl().shared_from_this()](error_code const& ec) { diff --git a/include/xrpl/shamap/Family.h b/include/xrpl/shamap/Family.h index 7624b3e600..467d41e4cc 100644 --- a/include/xrpl/shamap/Family.h +++ b/include/xrpl/shamap/Family.h @@ -26,10 +26,10 @@ public: explicit Family() = default; virtual ~Family() = default; - virtual NodeStore::Database& + virtual node_store::Database& db() = 0; - [[nodiscard]] virtual NodeStore::Database const& + [[nodiscard]] virtual node_store::Database const& db() const = 0; virtual beast::Journal const& diff --git a/include/xrpl/shamap/SHAMap.h b/include/xrpl/shamap/SHAMap.h index a1194ccfd3..e198c472fa 100644 --- a/include/xrpl/shamap/SHAMap.h +++ b/include/xrpl/shamap/SHAMap.h @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -95,6 +94,21 @@ enum class SHAMapState { * * See https://en.wikipedia.org/wiki/Merkle_tree */ + +/** + * Holds a SHAMap node's identity, leaf status, and serialized data. Used by + * getNodeFat to return node data for peer synchronization. + */ +struct SHAMapNodeData +{ + SHAMapNodeID nodeID; + // The `data` field (a Blob, 8-byte aligned) needs 4 bytes of padding after the `nodeID` field + // (36 bytes, 4-byte aligned) regardless of what comes between them, so `isLeaf` costs nothing + // extra here. Moving it after `data` would add 8 bytes to the size of this struct instead. + bool isLeaf; + Blob data; +}; + class SHAMap { private: @@ -289,10 +303,10 @@ public: std::vector> getMissingNodes(int maxNodes, SHAMapSyncFilter const* filter); - bool + [[nodiscard]] bool getNodeFat( SHAMapNodeID const& wanted, - std::vector>& data, + std::vector& data, bool fatLeaves, std::uint32_t depth) const; @@ -321,10 +335,45 @@ public: void serializeRoot(Serializer& s) const; + /** + * Add a root node to the SHAMap during synchronization. + * + * This function is used when receiving the root node of a SHAMap from a peer during ledger + * synchronization. The node must already have been deserialized. + * + * @param hash The expected hash of the root node. + * @param rootNode A deserialized root node to add. + * @param filter Optional sync filter to track received nodes. + * @return Status indicating whether the node was useful, duplicate, or invalid. + * + * @note This function expects the rootNode to be a valid, deserialized SHAMapTreeNode. The + * caller is responsible for deserialization and basic validation before calling this + * function. + */ SHAMapAddNode - addRootNode(SHAMapHash const& hash, Slice const& rootNode, SHAMapSyncFilter const* filter); + addRootNode(SHAMapHash const& hash, SHAMapTreeNodePtr rootNode, SHAMapSyncFilter const* filter); + + /** + * Add a known node at a specific position in the SHAMap during synchronization. + * + * This function is used when receiving nodes from peers during ledger synchronization. The node + * is inserted at the position specified by nodeID. The node must already have been + * deserialized. + * + * @param nodeID The position in the tree where this node belongs. + * @param treeNode A deserialized tree node to add. + * @param filter Optional sync filter to track received nodes. + * @return Status indicating whether the node was useful, duplicate, or invalid. + * + * @note This function expects the treeNode to be a valid, deserialized SHAMapTreeNode. The + * caller is responsible for deserialization and basic validation before calling this + * function. This also means that the nodeID must be consistent with the node's content. + */ SHAMapAddNode - addKnownNode(SHAMapNodeID const& nodeID, Slice const& rawNode, SHAMapSyncFilter const* filter); + addKnownNode( + SHAMapNodeID const& nodeID, + SHAMapTreeNodePtr treeNode, + SHAMapSyncFilter const* filter); // status functions void diff --git a/include/xrpl/shamap/SHAMapLeafNode.h b/include/xrpl/shamap/SHAMapLeafNode.h index 26cfde9fe8..ab5bd574ed 100644 --- a/include/xrpl/shamap/SHAMapLeafNode.h +++ b/include/xrpl/shamap/SHAMapLeafNode.h @@ -1,6 +1,9 @@ #pragma once #include +#include +#include +#include #include #include #include @@ -60,4 +63,16 @@ public: getString(SHAMapNodeID const&) const final; }; +/** + * Return the key of the item held by a SHAMap leaf node. + * + * @param node a node known to be a leaf (see SHAMapTreeNode::isLeaf). + */ +inline uint256 const& +leafKey(SHAMapTreeNode const& node) +{ + XRPL_ASSERT(node.isLeaf(), "xrpl::leafKey : node is a leaf"); + return safeDowncast(node).peekItem()->key(); +} + } // namespace xrpl diff --git a/nix/check-tools/README.md b/nix/check-tools/README.md new file mode 100644 index 0000000000..5b7538f2ca --- /dev/null +++ b/nix/check-tools/README.md @@ -0,0 +1,49 @@ +# check-tools snapshots + +These files capture the output of [`bin/check-tools.sh`](../../bin/check-tools.sh) +— the versions of the development tooling — in each Nix environment: + +| File | Environment | +| ---------------------- | ------------------------------------ | +| `nix-ubuntu-amd64.txt` | `nix-ubuntu` CI image, `linux/amd64` | +| `nix-ubuntu-arm64.txt` | `nix-ubuntu` CI image, `linux/arm64` | +| `macos.txt` | macOS, inside `nix develop` | + +The [`check-tools`](../../.github/workflows/check-tools.yml) workflow regenerates +each snapshot in its environment and fails if it differs from the committed file. +So if you change the environment (bump the image tag in +[`linux.json`](../../.github/scripts/strategy-matrix/linux.json), update +`flake.lock`, change the tool list in `check-tools.sh`, …) you must regenerate +and commit the affected snapshots. + +Each snapshot is `check-tools.sh` stdout with the git-clone connectivity check +skipped (`CHECK_TOOLS_SKIP_CLONE=1`), so it contains only deterministic version +data. On macOS the dev-shell greeting that `nix develop` prints first is dropped +with `sed -n '/^Detected OS:/,$p'`. + +## Regenerating + +The two Linux snapshots come from the `nix-ubuntu` image (Docker or a compatible +runtime such as Apple `container`). The image tag is pinned in `linux.json`: + +```bash +img="ghcr.io/xrplf/xrpld/nix-ubuntu:$(jq -r .image_tag .github/scripts/strategy-matrix/linux.json)" + +for arch in amd64 arm64; do + container run --rm -i -e CHECK_TOOLS_SKIP_CLONE=1 -a "${arch}" --entrypoint bash "${img}" -s \ + "nix/check-tools/nix-ubuntu-${arch}.txt" +done +``` + +(With Docker, replace `container run … -a "${arch}"` with +`docker run … --platform "linux/${arch}"`.) + +The macOS snapshot is generated locally. `CI=` is unset so `check-tools.sh` +checks the full dev-shell tool set (it otherwise skips some tools when `CI` is +set): + +```bash +CI= nix develop -c bash -c 'CHECK_TOOLS_SKIP_CLONE=1 bash bin/check-tools.sh' | + sed -n '/^Detected OS:/,$p' \ + >nix/check-tools/macos.txt +``` diff --git a/nix/check-tools/macos.txt b/nix/check-tools/macos.txt new file mode 100644 index 0000000000..93cc926181 --- /dev/null +++ b/nix/check-tools/macos.txt @@ -0,0 +1,47 @@ +Detected OS: macos (Darwin arm64) + +Core build tools: + [ ok ] cmake cmake version 4.1.2 + [ ok ] conan Conan version 2.28.1 + [ ok ] git git version 2.54.0 + [ ok ] python3 Python 3.13.13 + +Development tooling: + [ ok ] ccache ccache version 4.13.6 + [ ok ] clang clang version 22.1.7 + [ ok ] clang++ clang version 22.1.7 + [ ok ] ClangBuildAnalyzer ClangBuildAnalyzer 1.6.0 + [ ok ] curl curl 8.20.0 (aarch64-apple-darwin25.3.0) libcurl/8.20.0 OpenSSL/3.6.2 zlib/1.3.2 libssh2/1.11.1 nghttp2/1.69.0 mit-krb5/1.22.1 + [ ok ] file file-5.47 + [ ok ] less less 692 (PCRE2 regular expressions) + [ ok ] make GNU Make 4.4.1 + [ ok ] netstat present + [ ok ] ninja 1.13.2 + [ ok ] perl v5.42.0 + [ ok ] pkg-config 0.29.2 + [ ok ] vim VIM - Vi IMproved 9.2 (2026 Feb 14, compiled Jan 01 1980 00:00:00) + [ ok ] zip Zip 3.0 + [ ok ] clang-format clang-format version 22.1.7 + [ ok ] dot dot - graphviz version 12.2.1 (0) + [ ok ] doxygen 1.16.1 + [ ok ] gcovr gcovr 8.4 + [ ok ] gh gh version 2.94.0 (nixpkgs) + [ ok ] git-cliff git-cliff 2.13.1 + [ ok ] git-lfs git-lfs/3.7.1 (3.7.1; darwin arm64; go 1.26.3) + [ ok ] gpg gpg (GnuPG) 2.4.9 + [ ok ] pre-commit pre-commit 4.5.1 + [ ok ] run-clang-tidy usage: run-clang-tidy [-h] [-allow-enabling-alpha-checkers] + +Rust toolchain: + [ ok ] cargo cargo 1.95.0 (f2d3ce0bd 2026-03-21) + [ ok ] cargo-audit cargo-audit-audit 0.22.1 + [ ok ] cargo-llvm-cov cargo-llvm-cov 0.8.5 + [ ok ] cargo-nextest cargo-nextest 0.9.137 + [ ok ] clippy clippy 0.1.95 (59807616e1 2026-04-14) + [ ok ] rust-analyzer rust-analyzer 1.95.0 (59807616 2026-04-14) + [ ok ] rustc rustc 1.95.0 (59807616e 2026-04-14) + [ ok ] rustfmt rustfmt 1.9.0-stable (59807616e1 2026-04-14) + +Skipping git-over-HTTPS check (CHECK_TOOLS_SKIP_CLONE is set). + +All 36 checked tools are present and runnable. diff --git a/nix/check-tools/nix-ubuntu-amd64.txt b/nix/check-tools/nix-ubuntu-amd64.txt new file mode 100644 index 0000000000..b922cca4a8 --- /dev/null +++ b/nix/check-tools/nix-ubuntu-amd64.txt @@ -0,0 +1,55 @@ +Detected OS: linux (Linux x86_64) + +Core build tools: + [ ok ] cmake cmake version 4.1.2 + [ ok ] conan Conan version 2.28.1 + [ ok ] git git version 2.54.0 + [ ok ] python3 Python 3.13.13 + +Development tooling: + [ ok ] ccache ccache version 4.13.6 + [ ok ] clang clang version 22.1.7 + [ ok ] clang++ clang version 22.1.7 + [ ok ] ClangBuildAnalyzer ClangBuildAnalyzer 1.6.0 + [ ok ] curl curl 8.20.0 (x86_64-pc-linux-gnu) libcurl/8.20.0 OpenSSL/3.6.2 zlib/1.3.2 libssh2/1.11.1 nghttp2/1.69.0 mit-krb5/1.22.1 + [ ok ] file file-5.47 + [ ok ] less less 692 (PCRE2 regular expressions) + [ ok ] make GNU Make 4.4.1 + [ ok ] netstat net-tools 2.10 + [ ok ] ninja 1.13.2 + [ ok ] perl v5.42.0 + [ ok ] pkg-config 0.29.2 + [ ok ] vim VIM - Vi IMproved 9.2 (2026 Feb 14, compiled Jan 01 1980 00:00:00) + [ ok ] zip Zip 3.0 + [ ok ] clang-format clang-format version 22.1.7 + [ ok ] dot dot - graphviz version 12.2.1 (0) + [ ok ] doxygen 1.16.1 + [ ok ] gcovr gcovr 8.4 + [ ok ] gh gh version 2.94.0 (nixpkgs) + [ ok ] git-cliff git-cliff 2.13.1 + [ ok ] git-lfs git-lfs/3.7.1 (3.7.1; linux amd64; go 1.26.3) + [ ok ] gpg gpg (GnuPG) 2.4.9 + [ ok ] pre-commit pre-commit 4.5.1 + [ ok ] run-clang-tidy usage: run-clang-tidy [-h] [-allow-enabling-alpha-checkers] + +Rust toolchain: + [ ok ] cargo cargo 1.95.0 (f2d3ce0bd 2026-03-21) + [ ok ] cargo-audit cargo-audit-audit 0.22.1 + [ ok ] cargo-llvm-cov cargo-llvm-cov 0.8.5 + [ ok ] cargo-nextest cargo-nextest 0.9.137 + [ ok ] clippy clippy 0.1.95 (59807616e1 2026-04-14) + [ ok ] rust-analyzer rust-analyzer 1.95.0 (5980761 2026-04-14) + [ ok ] rustc rustc 1.95.0 (59807616e 2026-04-14) + [ ok ] rustfmt rustfmt 1.9.0-stable (59807616e1 2026-04-14) + +GCC toolchain: + [ ok ] gcc gcc (GCC) 15.2.0 + [ ok ] g++ g++ (GCC) 15.2.0 + [ ok ] gcov gcov (GCC) 15.2.0 + +Mold: + [ ok ] mold mold 2.41.0 (compatible with GNU ld) + +Skipping git-over-HTTPS check (CHECK_TOOLS_SKIP_CLONE is set). + +All 40 checked tools are present and runnable. diff --git a/nix/check-tools/nix-ubuntu-arm64.txt b/nix/check-tools/nix-ubuntu-arm64.txt new file mode 100644 index 0000000000..5267839682 --- /dev/null +++ b/nix/check-tools/nix-ubuntu-arm64.txt @@ -0,0 +1,55 @@ +Detected OS: linux (Linux aarch64) + +Core build tools: + [ ok ] cmake cmake version 4.1.2 + [ ok ] conan Conan version 2.28.1 + [ ok ] git git version 2.54.0 + [ ok ] python3 Python 3.13.13 + +Development tooling: + [ ok ] ccache ccache version 4.13.6 + [ ok ] clang clang version 22.1.7 + [ ok ] clang++ clang version 22.1.7 + [ ok ] ClangBuildAnalyzer ClangBuildAnalyzer 1.6.0 + [ ok ] curl curl 8.20.0 (aarch64-unknown-linux-gnu) libcurl/8.20.0 OpenSSL/3.6.2 zlib/1.3.2 libssh2/1.11.1 nghttp2/1.69.0 mit-krb5/1.22.1 + [ ok ] file file-5.47 + [ ok ] less less 692 (PCRE2 regular expressions) + [ ok ] make GNU Make 4.4.1 + [ ok ] netstat net-tools 2.10 + [ ok ] ninja 1.13.2 + [ ok ] perl v5.42.0 + [ ok ] pkg-config 0.29.2 + [ ok ] vim VIM - Vi IMproved 9.2 (2026 Feb 14, compiled Jan 01 1980 00:00:00) + [ ok ] zip Zip 3.0 + [ ok ] clang-format clang-format version 22.1.7 + [ ok ] dot dot - graphviz version 12.2.1 (0) + [ ok ] doxygen 1.16.1 + [ ok ] gcovr gcovr 8.4 + [ ok ] gh gh version 2.94.0 (nixpkgs) + [ ok ] git-cliff git-cliff 2.13.1 + [ ok ] git-lfs git-lfs/3.7.1 (3.7.1; linux arm64; go 1.26.3) + [ ok ] gpg gpg (GnuPG) 2.4.9 + [ ok ] pre-commit pre-commit 4.5.1 + [ ok ] run-clang-tidy usage: run-clang-tidy [-h] [-allow-enabling-alpha-checkers] + +Rust toolchain: + [ ok ] cargo cargo 1.95.0 (f2d3ce0bd 2026-03-21) + [ ok ] cargo-audit cargo-audit-audit 0.22.1 + [ ok ] cargo-llvm-cov cargo-llvm-cov 0.8.5 + [ ok ] cargo-nextest cargo-nextest 0.9.137 + [ ok ] clippy clippy 0.1.95 (59807616e1 2026-04-14) + [ ok ] rust-analyzer rust-analyzer 1.95.0 (5980761 2026-04-14) + [ ok ] rustc rustc 1.95.0 (59807616e 2026-04-14) + [ ok ] rustfmt rustfmt 1.9.0-stable (59807616e1 2026-04-14) + +GCC toolchain: + [ ok ] gcc gcc (GCC) 15.2.0 + [ ok ] g++ g++ (GCC) 15.2.0 + [ ok ] gcov gcov (GCC) 15.2.0 + +Mold: + [ ok ] mold mold 2.41.0 (compatible with GNU ld) + +Skipping git-over-HTTPS check (CHECK_TOOLS_SKIP_CLONE is set). + +All 40 checked tools are present and runnable. diff --git a/nix/devshell.nix b/nix/devshell.nix index 9b453ddef8..cb4a99c76a 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -50,12 +50,17 @@ let # compilerName is the command used to print the version, or null for none. makeShell = { + shellName, stdenv, compilerName, version ? null, versionedTools ? [ ], extraPackages ? [ ], warningHook ? "", + # Opt out of PatchNixBinary.cmake retargeting binaries to the system + # loader. The plain toolchain links a newer glibc, so it must not be + # patched; the custom toolchain patches by default. + noPatchNixBinary ? false, }: let compilerVersionHook = @@ -73,14 +78,20 @@ let tools = versionedTools; }); in - (pkgs.mkShell.override { inherit stdenv; }) { - packages = commonPackages ++ versionedLinks ++ extraPackages; - shellHook = '' - echo "Welcome to xrpld development shell"; - ${compilerVersionHook} - ${warningHook} - ''; - }; + (pkgs.mkShell.override { inherit stdenv; }) ( + { + packages = commonPackages ++ versionedLinks ++ extraPackages; + # Marks a managed dev shell, so the build (XrplSanity.cmake) can tell an + # intentional Nix toolchain from one leaked into a bare shell. + XRPL_DEVSHELL = shellName; + shellHook = '' + echo "Welcome to xrpld development shell"; + ${compilerVersionHook} + ${warningHook} + ''; + } + // pkgs.lib.optionalAttrs noPatchNixBinary { XRPLD_NO_PATCH_NIX_BINARY = "1"; } + ); in rec { # macOS: Nix Clang. Linux: Nix GCC. @@ -89,6 +100,7 @@ rec { # gcc/clang use the custom-glibc toolchain, matching CI. On darwin there is no # custom glibc, so they fall back to the plain nixpkgs toolchain. gcc = makeShell { + shellName = "gcc"; stdenv = customGccStdenv; compilerName = "gcc"; version = gccVersion; @@ -97,6 +109,7 @@ rec { }; clang = makeShell { + shellName = "clang"; stdenv = customClangStdenv; compilerName = "clang"; version = llvmVersion; @@ -105,6 +118,7 @@ rec { # Nix provides no compiler; use the one from your system (e.g. Apple Clang). no-compiler = makeShell { + shellName = "no-compiler"; stdenv = pkgs.stdenvNoCC; compilerName = null; }; @@ -115,19 +129,23 @@ rec { # makes `nix develop .#gcc-plain` fail there rather than silently aliasing gcc. // pkgs.lib.optionalAttrs pkgs.stdenv.isLinux { gcc-plain = makeShell { + shellName = "gcc-plain"; stdenv = plainGccStdenv; compilerName = "gcc"; version = gccVersion; versionedTools = gccVersionedTools; extraPackages = [ plainGcov ]; warningHook = plainWarningHook; + noPatchNixBinary = true; }; clang-plain = makeShell { + shellName = "clang-plain"; stdenv = plainClangStdenv; compilerName = "clang"; version = llvmVersion; versionedTools = clangVersionedTools; warningHook = plainWarningHook; + noPatchNixBinary = true; }; } diff --git a/nix/docker/Dockerfile b/nix/docker/Dockerfile index 53b646ac7c..74c630cb61 100644 --- a/nix/docker/Dockerfile +++ b/nix/docker/Dockerfile @@ -58,7 +58,7 @@ ENV GIT_SSL_CAINFO="/nix/ci-env/etc/ssl/certs/ca-bundle.crt" # Externally-built dynamically-linked ELF binaries hard-code the loader path # (e.g. /lib64/ld-linux-x86-64.so.2) in their PT_INTERP header. Install it # from the Nix store when the base image doesn't already provide one. -COPY nix/docker/loader-path.sh /tmp/loader-path.sh +COPY bin/default-loader-path.sh /tmp/loader-path.sh RUN < #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { namespace { -constexpr std::size_t kPoolSizes[] = {1000, 10000, 100000}; -constexpr int kThreadCounts[] = {1, 4, 8}; +constexpr auto kPoolSizes = std::to_array({1000, 10000, 100000}); +constexpr auto kThreadCounts = std::to_array({1, 4, 8}); constexpr std::size_t kBatchSize = 256; +constexpr std::size_t kMissRatio = 5; constexpr std::string_view kNamePrefix = "BM_Backend_"; constexpr std::string_view kNameSeparator = "/"; struct RunState { - std::unique_ptr harness; - Batch present; // prefix-1 objects, eligible to be stored - Batch recent; // prefix-1 objects in the "future" key space - std::vector missing; // prefix-2 keys that are never stored - std::vector shuffle; // [0, poolSize) permutation for random-like access - std::size_t avgPayload = 0; // mean getData().size() over `present` + std::unique_ptr harness; ///< backend under test, rebuilt per run + Batch present; ///< prefix-1 objects, eligible to be stored + Batch recent; ///< prefix-1 objects in the "future" key space + std::vector missing; ///< prefix-2 keys that are never stored + std::vector shuffle; ///< [0, poolSize) permutation for random-like access + std::size_t avgPayload = 0; ///< mean getData().size() over `present` void release() @@ -85,7 +86,7 @@ Workload const kInsert{ }, .iterate = [](IterateContext const& ctx) { - auto& [rs, backend, index, poolSize] = ctx; + auto const& [rs, backend, index, poolSize] = ctx; backend.store(rs.present[index % poolSize]); }, .reportBytes = true, @@ -104,7 +105,7 @@ Workload const kFetch{ }, .iterate = [](IterateContext const& ctx) { - auto& [rs, backend, index, poolSize] = ctx; + auto const& [rs, backend, index, poolSize] = ctx; std::shared_ptr result; backend.fetch(rs.present[index % poolSize]->getHash(), &result); benchmark::DoNotOptimize(result); @@ -118,7 +119,7 @@ Workload const kMissing{ .setup = [](SetupContext const& ctx) { ctx.rs.missing = makeMissingKeys(ctx.poolSize); }, .iterate = [](IterateContext const& ctx) { - auto& [rs, backend, index, poolSize] = ctx; + auto const& [rs, backend, index, poolSize] = ctx; std::shared_ptr result; backend.fetch(rs.missing[index % poolSize], &result); benchmark::DoNotOptimize(result); @@ -139,10 +140,10 @@ Workload const kMixed{ }, .iterate = [](IterateContext const& ctx) { - auto& [rs, backend, index, poolSize] = ctx; + auto const& [rs, backend, index, poolSize] = ctx; std::shared_ptr result; auto const pick = rs.shuffle[index % poolSize]; - if (index % 5 == 0) + if (index % kMissRatio == 0) { backend.fetch(rs.missing[pick], &result); } @@ -170,7 +171,7 @@ Workload const kWork{ }, .iterate = [](IterateContext const& ctx) { - auto& [rs, backend, index, poolSize] = ctx; + auto const& [rs, backend, index, poolSize] = ctx; auto const slot = index % poolSize; auto const pick = rs.shuffle[slot]; @@ -239,7 +240,7 @@ registerWorkload(BackendConfig const& bc, Workload const& w) { auto rs = std::make_shared(); auto* b = benchmark::RegisterBenchmark(name, makeRunner(w, cfg, rs)); - b->RangeMultiplier(10)->Range(kPoolSizes[0], kPoolSizes[std::size(kPoolSizes) - 1]); + b->RangeMultiplier(10)->Range(kPoolSizes.front(), kPoolSizes.back()); b->Threads(1)->Threads(4)->Threads(8)->UseRealTime(); return; @@ -249,14 +250,14 @@ registerWorkload(BackendConfig const& bc, Workload const& w) { for (auto const threads : kThreadCounts) { - if (poolSize % static_cast(threads) != 0) + if (poolSize % threads != 0) continue; auto rs = std::make_shared(); benchmark::RegisterBenchmark(name, makeRunner(w, cfg, rs)) ->Arg(poolSize) - ->Iterations(poolSize / static_cast(threads)) - ->Threads(threads) + ->Iterations(poolSize / threads) + ->Threads(static_cast(threads)) ->UseRealTime(); } } @@ -289,7 +290,7 @@ registerStoreBatch(BackendConfig const& bc) rs->harness = std::make_unique(cfg); rs->present = makePool(1, poolSize); rs->avgPayload = averagePayload(rs->present); - std::vector const batches = sliceBatches(rs->present, kBatchSize); + std::vector const batches = sliceFixedBatches(rs->present, kBatchSize); if (batches.empty()) { state.SkipWithError("pool smaller than one batch"); @@ -326,4 +327,4 @@ registerStoreBatch(BackendConfig const& bc) }(); } // namespace -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/benchmarks/libxrpl/nodestore/Database.cpp b/src/benchmarks/libxrpl/nodestore/Database.cpp index 2303075ab9..cd4337b603 100644 --- a/src/benchmarks/libxrpl/nodestore/Database.cpp +++ b/src/benchmarks/libxrpl/nodestore/Database.cpp @@ -18,7 +18,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { namespace { // Number of distinct objects pre-generated per run. @@ -240,4 +240,4 @@ registerWorkload(BackendConfig const& bc, Workload const& w) }(); } // namespace -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/benchmarks/libxrpl/nodestore/NodeStoreBench.h b/src/benchmarks/libxrpl/nodestore/NodeStoreBench.h index fe6c2a350e..debdc5d47a 100644 --- a/src/benchmarks/libxrpl/nodestore/NodeStoreBench.h +++ b/src/benchmarks/libxrpl/nodestore/NodeStoreBench.h @@ -26,13 +26,14 @@ #include #include #include +#include #include #include #include // Shared helpers for the NodeStore benchmarks. // -namespace xrpl::NodeStore { +namespace xrpl::node_store { // Fill `bytes` of memory at `buffer` with random bits drawn from `g`. template @@ -40,18 +41,13 @@ inline void rngcpy(void* buffer, std::size_t bytes, Generator& g) { using result_type = typename Generator::result_type; - while (bytes >= sizeof(result_type)) + while (bytes > 0) { auto const v = g(); - std::memcpy(buffer, &v, sizeof(v)); - buffer = reinterpret_cast(buffer) + sizeof(v); - bytes -= sizeof(v); - } - - if (bytes > 0) - { - auto const v = g(); - std::memcpy(buffer, &v, bytes); + auto const chunk = std::min(bytes, sizeof(result_type)); + std::memcpy(buffer, &v, chunk); + buffer = reinterpret_cast(buffer) + chunk; + bytes -= chunk; } } @@ -145,7 +141,7 @@ makePool(std::uint8_t prefix, std::size_t count, std::size_t start = 0) Sequence seq(prefix); Batch pool; pool.reserve(count); - for (std::size_t i = 0; i < count; ++i) + for (auto i = 0uz; i < count; ++i) pool.push_back(seq.obj(start + i)); return pool; } @@ -158,7 +154,7 @@ makeMissingKeys(std::size_t count) Sequence seq(2); std::vector keys; keys.reserve(count); - for (std::size_t i = 0; i < count; ++i) + for (auto i = 0uz; i < count; ++i) keys.push_back(seq.key(i)); return keys; } @@ -206,16 +202,16 @@ inline std::vector makeShuffle(std::size_t size, std::uint64_t seed) { std::vector v(size); - std::iota(v.begin(), v.end(), std::size_t{0}); + std::ranges::iota(v, 0uz); beast::xor_shift_engine gen(seed); - std::shuffle(v.begin(), v.end(), gen); + std::ranges::shuffle(v, gen); return v; } // Partition a pool into fixed-size batches. Any trailing remainder shorter than // `batchSize` is dropped, so every returned batch has exactly `batchSize`. inline std::vector -sliceBatches(Batch const& pool, std::size_t batchSize) +sliceFixedBatches(Batch const& pool, std::size_t batchSize) { std::vector batches; if (batchSize == 0) @@ -228,13 +224,10 @@ sliceBatches(Batch const& pool, std::size_t batchSize) /** * @brief RAII owner of a NodeStore Backend opened on a private temporary directory. - * - * Member declaration order matters: `tempDir` is declared first so it is - * destroyed last, after the backend has closed and released its files. */ struct BackendHarness { - beast::TempDir tempDir; + beast::TempDir tempDir; ///< Declared first so it is destroyed last DummyScheduler scheduler; beast::Journal journal{beast::Journal::getNullSink()}; std::unique_ptr backend; @@ -315,4 +308,4 @@ backendConfigs() return kConfigs; } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/basics/ResolverAsio.cpp b/src/libxrpl/basics/ResolverAsio.cpp index 25e95b7fc5..53739fed8a 100644 --- a/src/libxrpl/basics/ResolverAsio.cpp +++ b/src/libxrpl/basics/ResolverAsio.cpp @@ -255,7 +255,7 @@ public: if (ec == boost::asio::error::operation_aborted) return; - std::vector addresses; + std::vector addresses; auto iter = results.begin(); // If we get an error message back, we don't return any @@ -283,7 +283,7 @@ public: // first attempt to parse as an endpoint (IP addr + port). // If that doesn't succeed, fall back to generic name + port parsing - if (auto const result = beast::IP::Endpoint::fromStringChecked(str)) + if (auto const result = beast::ip::Endpoint::fromStringChecked(str)) { return make_pair(result->address().to_string(), std::to_string(result->port())); } diff --git a/src/libxrpl/basics/StringUtilities.cpp b/src/libxrpl/basics/StringUtilities.cpp index f4edaf5aca..2b7deecb8e 100644 --- a/src/libxrpl/basics/StringUtilities.cpp +++ b/src/libxrpl/basics/StringUtilities.cpp @@ -74,7 +74,7 @@ parseUrl(ParsedUrl& pUrl, std::string const& strUrl) // We need to use Endpoint to parse the domain to // strip surrounding brackets from IPv6 addresses, // e.g. [::1] => ::1. - auto const result = beast::IP::Endpoint::fromStringChecked(domain); + auto const result = beast::ip::Endpoint::fromStringChecked(domain); pUrl.domain = result ? result->address().to_string() : domain; std::string const port = smMatch[5]; if (!port.empty()) diff --git a/src/libxrpl/beast/core/SemanticVersion.cpp b/src/libxrpl/beast/core/SemanticVersion.cpp index a99437f8f2..f902a14b07 100644 --- a/src/libxrpl/beast/core/SemanticVersion.cpp +++ b/src/libxrpl/beast/core/SemanticVersion.cpp @@ -15,7 +15,7 @@ namespace beast { std::string -printIdentifiers(SemanticVersion::identifier_list const& list) +printIdentifiers(SemanticVersion::IdentifierList const& list) { std::string ret; @@ -115,7 +115,7 @@ extractIdentifier(std::string& value, bool allowLeadingZeroes, std::string& inpu bool extractIdentifiers( - SemanticVersion::identifier_list& identifiers, + SemanticVersion::IdentifierList& identifiers, bool allowLeadingZeroes, std::string& input) { diff --git a/src/libxrpl/beast/insight/StatsDCollector.cpp b/src/libxrpl/beast/insight/StatsDCollector.cpp index 3cff5d93b5..72fe6189a5 100644 --- a/src/libxrpl/beast/insight/StatsDCollector.cpp +++ b/src/libxrpl/beast/insight/StatsDCollector.cpp @@ -207,7 +207,7 @@ private: static constexpr auto kMaxPacketSize = 1472; Journal journal_; - IP::Endpoint address_; + ip::Endpoint address_; std::string prefix_; boost::asio::io_context ioContext_; std::optional> work_; @@ -222,13 +222,13 @@ private: std::thread thread_; static boost::asio::ip::udp::endpoint - toEndpoint(IP::Endpoint const& ep) + toEndpoint(ip::Endpoint const& ep) { return boost::asio::ip::udp::endpoint(ep.address(), ep.port()); } public: - StatsDCollectorImp(IP::Endpoint address, std::string prefix, Journal journal) + StatsDCollectorImp(ip::Endpoint address, std::string prefix, Journal journal) : journal_(journal) , address_(std::move(address)) , prefix_(std::move(prefix)) @@ -707,7 +707,7 @@ StatsDMeterImpl::doProcess() //------------------------------------------------------------------------------ std::shared_ptr -StatsDCollector::make(IP::Endpoint const& address, std::string const& prefix, Journal journal) +StatsDCollector::make(ip::Endpoint const& address, std::string const& prefix, Journal journal) { return std::make_shared(address, prefix, journal); } diff --git a/src/libxrpl/beast/net/IPAddressConversion.cpp b/src/libxrpl/beast/net/IPAddressConversion.cpp index c0a37d234e..bf24ef75c1 100644 --- a/src/libxrpl/beast/net/IPAddressConversion.cpp +++ b/src/libxrpl/beast/net/IPAddressConversion.cpp @@ -5,7 +5,7 @@ #include #include -namespace beast::IP { +namespace beast::ip { Endpoint fromAsio(boost::asio::ip::address const& address) @@ -31,4 +31,4 @@ toAsioEndpoint(Endpoint const& endpoint) return boost::asio::ip::tcp::endpoint{endpoint.address(), endpoint.port()}; } -} // namespace beast::IP +} // namespace beast::ip diff --git a/src/libxrpl/beast/net/IPAddressV4.cpp b/src/libxrpl/beast/net/IPAddressV4.cpp index f9b0c96022..2a59fe1cc4 100644 --- a/src/libxrpl/beast/net/IPAddressV4.cpp +++ b/src/libxrpl/beast/net/IPAddressV4.cpp @@ -1,6 +1,6 @@ #include -namespace beast::IP { +namespace beast::ip { bool isPrivate(AddressV4 const& addr) @@ -62,4 +62,4 @@ getClass(AddressV4 const& addr) return kTable[(addr.to_uint() & 0xE0000000) >> 29]; } -} // namespace beast::IP +} // namespace beast::ip diff --git a/src/libxrpl/beast/net/IPAddressV6.cpp b/src/libxrpl/beast/net/IPAddressV6.cpp index c75ccaf1cc..e5ef55065f 100644 --- a/src/libxrpl/beast/net/IPAddressV6.cpp +++ b/src/libxrpl/beast/net/IPAddressV6.cpp @@ -4,7 +4,7 @@ #include -namespace beast::IP { +namespace beast::ip { bool isPrivate(AddressV6 const& addr) @@ -58,4 +58,4 @@ isPublic(AddressV6 const& addr) return true; } -} // namespace beast::IP +} // namespace beast::ip diff --git a/src/libxrpl/beast/net/IPEndpoint.cpp b/src/libxrpl/beast/net/IPEndpoint.cpp index 5877151187..02ed5e37c5 100644 --- a/src/libxrpl/beast/net/IPEndpoint.cpp +++ b/src/libxrpl/beast/net/IPEndpoint.cpp @@ -14,7 +14,7 @@ #include #include -namespace beast::IP { +namespace beast::ip { Endpoint::Endpoint() : port_(0) { @@ -176,4 +176,4 @@ operator>>(std::istream& is, Endpoint& endpoint) return is; } -} // namespace beast::IP +} // namespace beast::ip diff --git a/src/libxrpl/ledger/helpers/LendingHelpers.cpp b/src/libxrpl/ledger/helpers/LendingHelpers.cpp index e6c3d632c1..89b03a03a7 100644 --- a/src/libxrpl/ledger/helpers/LendingHelpers.cpp +++ b/src/libxrpl/ledger/helpers/LendingHelpers.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -130,6 +131,127 @@ isRounded(Asset const& asset, Number const& value, std::int32_t scale) roundToAsset(asset, value, scale, Number::RoundingMode::Upward); } +namespace accrual { + +AccountingDeltas +loanOriginationDeltas(Number const& principalRequested, Number const& interestDue) +{ + return {.assetsTotalDelta = interestDue, .debtTotalDelta = principalRequested + interestDue}; +} + +bool +loanOriginationExceedsVaultMaximum( + Number const& vaultMaximum, + Number const& vaultTotal, + Number const& interestDue) +{ + return vaultMaximum != 0 && interestDue > vaultMaximum - vaultTotal; +} + +/* +XLS-66 section 3.2.3.2, defines the default amount as + +DefaultAmount = (Loan.PrincipalOutstanding + Loan.InterestOutstanding) + +Which is equivalent to (Loan.TotalValueOutstanding - Loan.ManagementFeeOutstanding) +*/ +Number +loanVaultExposure(SLE::const_ref loanSle) +{ + return loanSle->at(sfTotalValueOutstanding) - loanSle->at(sfManagementFeeOutstanding); +} + +AccountingDeltas +loanPaymentDeltas(LoanPaymentParts const& parts) +{ + return { + .assetsTotalDelta = parts.valueChange, + .debtTotalDelta = (parts.principalPaid + parts.interestPaid) - parts.valueChange}; +} + +} // namespace accrual + +namespace cash_basis { + +AccountingDeltas +loanOriginationDeltas(Number const& principalRequested) +{ + return {.assetsTotalDelta = kNumZero, .debtTotalDelta = principalRequested}; +} + +/* + * Under CashBasis accounting, Loan default amount is: + * + * DefaultAmount = Loan.PrincipalOutstanding + */ +Number +loanVaultExposure(SLE::const_ref loanSle) +{ + return loanSle->at(sfPrincipalOutstanding); +} + +AccountingDeltas +loanPaymentDeltas(LoanPaymentParts const& parts) +{ + return {.assetsTotalDelta = parts.interestPaid, .debtTotalDelta = parts.principalPaid}; +} + +} // namespace cash_basis + +namespace { + +// Cash-basis accounting applies only when featureLendingProtocolV1_1 is +// enabled AND the specific Vault was created under it (LEVersion == +// VaultVersion::CashBasis). Vaults created before activation keep accrual-basis +// accounting forever, even after the amendment later turns on. +bool +cashBasisEnabled(SLE::const_ref vaultSle) +{ + return getVaultVersion(vaultSle) == VaultVersion::CashBasis; +} + +} // namespace + +AccountingDeltas +loanOriginationDeltas( + SLE::const_ref vaultSle, + Number const& principalRequested, + Number const& interestDue) +{ + return cashBasisEnabled(vaultSle) + ? cash_basis::loanOriginationDeltas(principalRequested) + : accrual::loanOriginationDeltas(principalRequested, interestDue); +} + +bool +loanOriginationExceedsVaultMaximum( + SLE::const_ref vaultSle, + Number const& vaultTotal, + Number const& interestDue) +{ + // Cash-basis origination doesn't recognize interest into AssetsTotal, so + // interest due can never push the vault past AssetsMaximum at origination. + if (cashBasisEnabled(vaultSle)) + return false; + + auto const vaultMaximum = vaultSle->at(sfAssetsMaximum); + return accrual::loanOriginationExceedsVaultMaximum(vaultMaximum, vaultTotal, interestDue); +} + +Number +loanVaultExposure(SLE::const_ref vaultSle, SLE::const_ref loanSle) +{ + return cashBasisEnabled(vaultSle) ? cash_basis::loanVaultExposure(loanSle) + : accrual::loanVaultExposure(loanSle); +} + +AccountingDeltas +loanPaymentDeltas(SLE::const_ref vaultSle, LoanPaymentParts const& parts) +{ + return cashBasisEnabled(vaultSle) ? cash_basis::loanPaymentDeltas(parts) + : accrual::loanPaymentDeltas(parts); +} + namespace detail { void @@ -1495,7 +1617,7 @@ makeRegularPayment( LoanPaymentType const paymentType, beast::Journal j) { - using namespace Lending; + using namespace lending; XRPL_ASSERT_PARTS( paymentType == LoanPaymentType::Regular || paymentType == LoanPaymentType::Overpayment, diff --git a/src/libxrpl/ledger/helpers/VaultHelpers.cpp b/src/libxrpl/ledger/helpers/VaultHelpers.cpp index b5b076d1cb..78f64d2077 100644 --- a/src/libxrpl/ledger/helpers/VaultHelpers.cpp +++ b/src/libxrpl/ledger/helpers/VaultHelpers.cpp @@ -6,6 +6,7 @@ #include #include #include // IWYU pragma: keep +#include #include #include #include @@ -13,6 +14,7 @@ #include #include +#include namespace xrpl { @@ -137,4 +139,22 @@ isSoleShareholder(ReadView const& view, AccountID const& account, SLE::const_ref return sleToken->getFieldU64(sfMPTAmount) == outstanding; } +[[nodiscard]] VaultVersion +getVaultVersion(SLE::const_ref vault) +{ + XRPL_ASSERT(vault && vault->getType() == ltVAULT, "xrpl::getVaultVersion : valid Vault sle"); + if (!vault->isFieldPresent(sfLEVersion)) + return VaultVersion::Legacy; + + auto const version = vault->at(sfLEVersion); + if (version > std::to_underlying(VaultVersion::CashBasis)) + { + // LCOV_EXCL_START + UNREACHABLE("xrpl::getVaultVersion : invalid vault version"); + return VaultVersion::Legacy; + // LCOV_EXCL_STOP + } + return static_cast(version); +} + } // namespace xrpl diff --git a/src/libxrpl/nodestore/BatchWriter.cpp b/src/libxrpl/nodestore/BatchWriter.cpp index e0a1fbf20c..92dfa09e0c 100644 --- a/src/libxrpl/nodestore/BatchWriter.cpp +++ b/src/libxrpl/nodestore/BatchWriter.cpp @@ -11,7 +11,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { BatchWriter::BatchWriter(Callback& callback, Scheduler& scheduler) : callback_(callback), scheduler_(scheduler) @@ -72,7 +72,7 @@ BatchWriter::writeBatch() writeSet_.swap(set); XRPL_ASSERT( - writeSet_.empty(), "xrpl::NodeStore::BatchWriter::writeBatch : writes not set"); + writeSet_.empty(), "xrpl::node_store::BatchWriter::writeBatch : writes not set"); writeLoad_ = set.size(); if (set.empty()) @@ -107,4 +107,4 @@ BatchWriter::waitForWriting() writeCondition_.wait(sl); } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/Database.cpp b/src/libxrpl/nodestore/Database.cpp index ac51dbfb2c..f9de660042 100644 --- a/src/libxrpl/nodestore/Database.cpp +++ b/src/libxrpl/nodestore/Database.cpp @@ -30,7 +30,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { Database::Database( Scheduler& scheduler, @@ -43,7 +43,7 @@ Database::Database( , requestBundle_(get(config, Keys::kRqBundle, 4)) , readThreads_(std::max(1, readThreads)) { - XRPL_ASSERT(readThreads, "xrpl::NodeStore::Database::Database : nonzero threads input"); + XRPL_ASSERT(readThreads, "xrpl::node_store::Database::Database : nonzero threads input"); if (earliestLedgerSeq_ < 1) Throw("Invalid earliest_seq"); @@ -89,7 +89,7 @@ Database::Database( { XRPL_ASSERT( !it->second.empty(), - "xrpl::NodeStore::Database::Database : non-empty " + "xrpl::node_store::Database::Database : non-empty " "data"); auto const& hash = it->first; @@ -164,7 +164,7 @@ Database::stop() { XRPL_ASSERT( steady_clock::now() - start < 30s, - "xrpl::NodeStore::Database::stop : maximum stop duration"); + "xrpl::node_store::Database::stop : maximum stop duration"); std::this_thread::yield(); } @@ -213,7 +213,7 @@ Database::importInternal(Backend& dstBackend, Database& srcDB) }; srcDB.forEach([&](std::shared_ptr nodeObject) { - XRPL_ASSERT(nodeObject, "xrpl::NodeStore::Database::importInternal : non-null node"); + XRPL_ASSERT(nodeObject, "xrpl::node_store::Database::importInternal : non-null node"); if (!nodeObject) // This should never happen return; @@ -257,7 +257,7 @@ Database::fetchNodeObject( void Database::getCountsJson(json::Value& obj) { - XRPL_ASSERT(obj.isObject(), "xrpl::NodeStore::Database::getCountsJson : valid input type"); + XRPL_ASSERT(obj.isObject(), "xrpl::node_store::Database::getCountsJson : valid input type"); { std::unique_lock const lock(readLock_); @@ -276,4 +276,4 @@ Database::getCountsJson(json::Value& obj) obj[jss::node_reads_duration_us] = std::to_string(fetchDurationUs_); } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/DatabaseNodeImp.cpp b/src/libxrpl/nodestore/DatabaseNodeImp.cpp index 9323d69131..1b880ac658 100644 --- a/src/libxrpl/nodestore/DatabaseNodeImp.cpp +++ b/src/libxrpl/nodestore/DatabaseNodeImp.cpp @@ -15,7 +15,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { void DatabaseNodeImp::store(NodeObjectType type, Blob&& data, uint256 const& hash, std::uint32_t) @@ -125,4 +125,4 @@ DatabaseNodeImp::fetchNodeObject( return nodeObject; } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/DatabaseRotatingImp.cpp b/src/libxrpl/nodestore/DatabaseRotatingImp.cpp index 23a48a3bf3..81b1d6b297 100644 --- a/src/libxrpl/nodestore/DatabaseRotatingImp.cpp +++ b/src/libxrpl/nodestore/DatabaseRotatingImp.cpp @@ -22,7 +22,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { DatabaseRotatingImp::DatabaseRotatingImp( Scheduler& scheduler, @@ -43,7 +43,7 @@ DatabaseRotatingImp::DatabaseRotatingImp( void DatabaseRotatingImp::rotate( - std::unique_ptr&& newBackend, + std::unique_ptr&& newBackend, std::function const& f) { // Pass these two names to the callback function @@ -52,7 +52,7 @@ DatabaseRotatingImp::rotate( // Hold on to current archive backend pointer until after the // callback finishes. Only then will the archive directory be // deleted. - std::shared_ptr oldArchiveBackend; + std::shared_ptr oldArchiveBackend; std::uint64_t copyForwards = 0; { std::scoped_lock const lock(mutex_); @@ -232,4 +232,4 @@ DatabaseRotatingImp::forEach(std::function)> f) archive->forEach(f); } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/DecodedBlob.cpp b/src/libxrpl/nodestore/DecodedBlob.cpp index 9740462ae8..321089d40f 100644 --- a/src/libxrpl/nodestore/DecodedBlob.cpp +++ b/src/libxrpl/nodestore/DecodedBlob.cpp @@ -10,7 +10,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { DecodedBlob::DecodedBlob(void const* key, void const* value, int valueBytes) : key_(key) { @@ -55,7 +55,7 @@ DecodedBlob::DecodedBlob(void const* key, void const* value, int valueBytes) : k std::shared_ptr DecodedBlob::createObject() { - XRPL_ASSERT(success_, "xrpl::NodeStore::DecodedBlob::createObject : valid object type"); + XRPL_ASSERT(success_, "xrpl::node_store::DecodedBlob::createObject : valid object type"); std::shared_ptr object; @@ -69,4 +69,4 @@ DecodedBlob::createObject() return object; } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/DummyScheduler.cpp b/src/libxrpl/nodestore/DummyScheduler.cpp index 1f93ed3d0f..32cd14cbdc 100644 --- a/src/libxrpl/nodestore/DummyScheduler.cpp +++ b/src/libxrpl/nodestore/DummyScheduler.cpp @@ -3,7 +3,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { void DummyScheduler::scheduleTask(Task& task) @@ -22,4 +22,4 @@ DummyScheduler::onBatchWrite(BatchWriteReport const& report) { } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/ManagerImp.cpp b/src/libxrpl/nodestore/ManagerImp.cpp index a3db22ce74..c78ccd5761 100644 --- a/src/libxrpl/nodestore/ManagerImp.cpp +++ b/src/libxrpl/nodestore/ManagerImp.cpp @@ -22,7 +22,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { ManagerImp& ManagerImp::instance() @@ -112,7 +112,7 @@ ManagerImp::erase(Factory& factory) std::scoped_lock const _(mutex_); auto const iter = std::ranges::find_if(list_, [&factory](Factory* other) { return other == &factory; }); - XRPL_ASSERT(iter != list_.end(), "xrpl::NodeStore::ManagerImp::erase : valid input"); + XRPL_ASSERT(iter != list_.end(), "xrpl::node_store::ManagerImp::erase : valid input"); list_.erase(iter); } @@ -135,4 +135,4 @@ Manager::instance() return ManagerImp::instance(); } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/backend/MemoryFactory.cpp b/src/libxrpl/nodestore/backend/MemoryFactory.cpp index 22557d652e..39d2123bc9 100644 --- a/src/libxrpl/nodestore/backend/MemoryFactory.cpp +++ b/src/libxrpl/nodestore/backend/MemoryFactory.cpp @@ -24,7 +24,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { struct MemoryDB { @@ -132,7 +132,7 @@ public: Status fetch(uint256 const& hash, std::shared_ptr* pObject) override { - XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::fetch : non-null database"); + XRPL_ASSERT(db_, "xrpl::node_store::MemoryBackend::fetch : non-null database"); std::scoped_lock const _(db_->mutex); @@ -149,7 +149,7 @@ public: void store(std::shared_ptr const& object) override { - XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::store : non-null database"); + XRPL_ASSERT(db_, "xrpl::node_store::MemoryBackend::store : non-null database"); std::scoped_lock const _(db_->mutex); db_->table.emplace(object->getHash(), object); } @@ -169,7 +169,7 @@ public: void forEach(std::function)> f) override { - XRPL_ASSERT(db_, "xrpl::NodeStore::MemoryBackend::forEach : non-null database"); + XRPL_ASSERT(db_, "xrpl::node_store::MemoryBackend::forEach : non-null database"); for (auto const& e : db_->table) f(e.second); } @@ -216,4 +216,4 @@ MemoryFactory::createInstance( return std::make_unique(keyBytes, keyValues, journal); } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/backend/NuDBFactory.cpp b/src/libxrpl/nodestore/backend/NuDBFactory.cpp index 38ea34258f..bbf37f3edf 100644 --- a/src/libxrpl/nodestore/backend/NuDBFactory.cpp +++ b/src/libxrpl/nodestore/backend/NuDBFactory.cpp @@ -44,7 +44,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { class NuDBBackend : public Backend { @@ -136,7 +136,7 @@ public: { // LCOV_EXCL_START UNREACHABLE( - "xrpl::NodeStore::NuDBBackend::open : database is already " + "xrpl::node_store::NuDBBackend::open : database is already " "open"); JLOG(j.error()) << "database is already open"; return; @@ -441,4 +441,4 @@ registerNuDBFactory(Manager& manager) static NuDBFactory const kInstance{manager}; } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/backend/NullFactory.cpp b/src/libxrpl/nodestore/backend/NullFactory.cpp index 0c76cb9938..feef3d37d0 100644 --- a/src/libxrpl/nodestore/backend/NullFactory.cpp +++ b/src/libxrpl/nodestore/backend/NullFactory.cpp @@ -13,7 +13,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { class NullBackend : public Backend { @@ -125,4 +125,4 @@ registerNullFactory(Manager& manager) static NullFactory const kInstance{manager}; } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/libxrpl/nodestore/backend/RocksDBFactory.cpp b/src/libxrpl/nodestore/backend/RocksDBFactory.cpp index 673b0daae0..4b7a1171fe 100644 --- a/src/libxrpl/nodestore/backend/RocksDBFactory.cpp +++ b/src/libxrpl/nodestore/backend/RocksDBFactory.cpp @@ -42,7 +42,7 @@ #include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { class RocksDBEnv : public rocksdb::EnvWrapper { @@ -231,7 +231,7 @@ public: { // LCOV_EXCL_START UNREACHABLE( - "xrpl::NodeStore::RocksDBBackend::open : database is already " + "xrpl::node_store::RocksDBBackend::open : database is already " "open"); JLOG(journal.error()) << "database is already open"; return; @@ -279,7 +279,7 @@ public: Status fetch(uint256 const& hash, std::shared_ptr* pObject) override { - XRPL_ASSERT(db, "xrpl::NodeStore::RocksDBBackend::fetch : non-null database"); + XRPL_ASSERT(db, "xrpl::node_store::RocksDBBackend::fetch : non-null database"); pObject->reset(); Status status = Status::Ok; @@ -339,7 +339,7 @@ public: { XRPL_ASSERT( db, - "xrpl::NodeStore::RocksDBBackend::storeBatch : non-null " + "xrpl::node_store::RocksDBBackend::storeBatch : non-null " "database"); rocksdb::WriteBatch wb; @@ -369,7 +369,7 @@ public: void forEach(std::function)> f) override { - XRPL_ASSERT(db, "xrpl::NodeStore::RocksDBBackend::forEach : non-null database"); + XRPL_ASSERT(db, "xrpl::node_store::RocksDBBackend::forEach : non-null database"); rocksdb::ReadOptions const options; std::unique_ptr it(db->NewIterator(options)); @@ -468,6 +468,6 @@ registerRocksDBFactory(Manager& manager) static RocksDBFactory const kInstance{manager}; } -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store #endif diff --git a/src/libxrpl/peerfinder/Bootcache.cpp b/src/libxrpl/peerfinder/Bootcache.cpp index a2a56b4d01..4f9b5fc816 100644 --- a/src/libxrpl/peerfinder/Bootcache.cpp +++ b/src/libxrpl/peerfinder/Bootcache.cpp @@ -16,7 +16,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { Bootcache::Bootcache(Store& store, clock_type& clock, beast::Journal journal) : store_(store), clock_(clock), journal_(journal), whenUpdate_(clock_.now()) @@ -78,7 +78,7 @@ void Bootcache::load() { clear(); - auto const n(store_.load([this](beast::IP::Endpoint const& endpoint, int valence) { + auto const n(store_.load([this](beast::ip::Endpoint const& endpoint, int valence) { auto const result(this->map_.insert(value_type(endpoint, valence))); if (!result.second) { @@ -96,7 +96,7 @@ Bootcache::load() } bool -Bootcache::insert(beast::IP::Endpoint const& endpoint) +Bootcache::insert(beast::ip::Endpoint const& endpoint) { auto const result(map_.insert(value_type(endpoint, 0))); if (result.second) @@ -109,7 +109,7 @@ Bootcache::insert(beast::IP::Endpoint const& endpoint) } bool -Bootcache::insertStatic(beast::IP::Endpoint const& endpoint) +Bootcache::insertStatic(beast::ip::Endpoint const& endpoint) { auto result(map_.insert(value_type(endpoint, kStaticValence))); @@ -130,7 +130,7 @@ Bootcache::insertStatic(beast::IP::Endpoint const& endpoint) } void -Bootcache::onSuccess(beast::IP::Endpoint const& endpoint) +Bootcache::onSuccess(beast::ip::Endpoint const& endpoint) { auto result(map_.insert(value_type(endpoint, 1))); if (result.second) @@ -144,7 +144,7 @@ Bootcache::onSuccess(beast::IP::Endpoint const& endpoint) ++entry.valence(); map_.erase(result.first); result = map_.insert(value_type(endpoint, entry)); - XRPL_ASSERT(result.second, "xrpl::PeerFinder::Bootcache::onSuccess : endpoint inserted"); + XRPL_ASSERT(result.second, "xrpl::peer_finder::Bootcache::onSuccess : endpoint inserted"); } Entry const& entry(result.first->right); JLOG(journal_.info()) << std::left << std::setw(18) << "Bootcache connect " << endpoint @@ -154,7 +154,7 @@ Bootcache::onSuccess(beast::IP::Endpoint const& endpoint) } void -Bootcache::onFailure(beast::IP::Endpoint const& endpoint) +Bootcache::onFailure(beast::ip::Endpoint const& endpoint) { auto result(map_.insert(value_type(endpoint, -1))); if (result.second) @@ -168,7 +168,7 @@ Bootcache::onFailure(beast::IP::Endpoint const& endpoint) --entry.valence(); map_.erase(result.first); result = map_.insert(value_type(endpoint, entry)); - XRPL_ASSERT(result.second, "xrpl::PeerFinder::Bootcache::onFailure : endpoint inserted"); + XRPL_ASSERT(result.second, "xrpl::peer_finder::Bootcache::onFailure : endpoint inserted"); } Entry const& entry(result.first->right); auto const n(std::abs(entry.valence())); @@ -201,11 +201,11 @@ Bootcache::onWrite(beast::PropertyStream::Map& map) void Bootcache::prune() { - if (size() <= Tuning::kBootcacheSize) + if (size() <= tuning::kBootcacheSize) return; // Calculate the amount to remove - auto count((size() * Tuning::kBootcachePrunePercent) / 100); + auto count((size() * tuning::kBootcachePrunePercent) / 100); decltype(count) pruned(0); // Work backwards because bimap doesn't handle @@ -215,7 +215,7 @@ Bootcache::prune() { --count; --iter; - beast::IP::Endpoint const& endpoint(iter->get_left()); + beast::ip::Endpoint const& endpoint(iter->get_left()); Entry const& entry(iter->get_right()); JLOG(journal_.trace()) << std::left << std::setw(18) << "Bootcache pruned" << endpoint << " at valence " << entry.valence(); @@ -244,7 +244,7 @@ Bootcache::update() store_.save(list); // Reset the flag and cooldown timer needsUpdate_ = false; - whenUpdate_ = clock_.now() + Tuning::kBootcacheCooldownTime; + whenUpdate_ = clock_.now() + tuning::kBootcacheCooldownTime; } // Checks the clock and calls update if we are off the cooldown. @@ -263,4 +263,4 @@ Bootcache::flagForUpdate() checkUpdate(); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/libxrpl/peerfinder/Config.cpp b/src/libxrpl/peerfinder/Config.cpp index 3e2f74f42b..60ac0ca547 100644 --- a/src/libxrpl/peerfinder/Config.cpp +++ b/src/libxrpl/peerfinder/Config.cpp @@ -7,13 +7,13 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { std::size_t Config::calcOutPeers() const { return std::max( - ((maxPeers * Tuning::kOutPercent) + 50) / 100, std::size_t(Tuning::kMinOutCount)); + ((maxPeers * tuning::kOutPercent) + 50) / 100, std::size_t(tuning::kMinOutCount)); } void @@ -26,8 +26,8 @@ Config::applyTuning() // IP addresses. ipLimit = 2; - if (inPeers > Tuning::kDefaultMaxPeers) - ipLimit += std::min(5, static_cast(inPeers / Tuning::kDefaultMaxPeers)); + if (inPeers > tuning::kDefaultMaxPeers) + ipLimit += std::min(5, static_cast(inPeers / tuning::kDefaultMaxPeers)); } // We don't allow a single IP to consume all incoming slots, @@ -58,7 +58,7 @@ Config::makeConfig( int ipLimit, bool verifyEndpoints) { - PeerFinder::Config config; + peer_finder::Config config; if (!limits.maxPeers) { @@ -85,7 +85,7 @@ Config::makeConfig( if (limits.maxPeers && *limits.maxPeers != 0) config.maxPeers = *limits.maxPeers; - config.maxPeers = std::max(config.maxPeers, Tuning::kMinOutCount); + config.maxPeers = std::max(config.maxPeers, tuning::kMinOutCount); config.outPeers = config.calcOutPeers(); // Calculate the number of outbound peers we want. If we dont want @@ -132,4 +132,4 @@ Config::makeConfig( return config; } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/libxrpl/peerfinder/Endpoint.cpp b/src/libxrpl/peerfinder/Endpoint.cpp index 12f2725ea5..6f3e2289f9 100644 --- a/src/libxrpl/peerfinder/Endpoint.cpp +++ b/src/libxrpl/peerfinder/Endpoint.cpp @@ -5,11 +5,11 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { -Endpoint::Endpoint(beast::IP::Endpoint ep, std::uint32_t hops) - : hops(std::min(hops, Tuning::kMaxHops + 1)), address(std::move(ep)) +Endpoint::Endpoint(beast::ip::Endpoint ep, std::uint32_t hops) + : hops(std::min(hops, tuning::kMaxHops + 1)), address(std::move(ep)) { } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/libxrpl/peerfinder/PeerfinderManager.cpp b/src/libxrpl/peerfinder/PeerfinderManager.cpp index 2219627f09..0cce2389ec 100644 --- a/src/libxrpl/peerfinder/PeerfinderManager.cpp +++ b/src/libxrpl/peerfinder/PeerfinderManager.cpp @@ -29,7 +29,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { class ManagerImp : public Manager { @@ -98,7 +98,7 @@ public: } void - addFixedPeer(std::string_view name, std::vector const& addresses) override + addFixedPeer(std::string_view name, std::vector const& addresses) override { logic_.addFixedPeer(name, addresses); } @@ -119,14 +119,14 @@ public: std::pair, Result> newInboundSlot( - beast::IP::Endpoint const& localEndpoint, - beast::IP::Endpoint const& remoteEndpoint) override + beast::ip::Endpoint const& localEndpoint, + beast::ip::Endpoint const& remoteEndpoint) override { return logic_.newInboundSlot(localEndpoint, remoteEndpoint); } std::pair, Result> - newOutboundSlot(beast::IP::Endpoint const& remoteEndpoint) override + newOutboundSlot(beast::ip::Endpoint const& remoteEndpoint) override { return logic_.newOutboundSlot(remoteEndpoint); } @@ -163,7 +163,7 @@ public: //-------------------------------------------------------------------------- bool - onConnected(std::shared_ptr const& slot, beast::IP::Endpoint const& localEndpoint) + onConnected(std::shared_ptr const& slot, beast::ip::Endpoint const& localEndpoint) override { SlotImp::ptr const impl(std::dynamic_pointer_cast(slot)); @@ -184,7 +184,7 @@ public: return logic_.redirect(impl); } - std::vector + std::vector autoconnect() override { return logic_.autoconnect(); @@ -265,4 +265,4 @@ makeManager( return std::make_unique(ioContext, clock, journal, store, collector); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/libxrpl/peerfinder/SlotImp.cpp b/src/libxrpl/peerfinder/SlotImp.cpp index 0a4f32fd62..5209bd51ab 100644 --- a/src/libxrpl/peerfinder/SlotImp.cpp +++ b/src/libxrpl/peerfinder/SlotImp.cpp @@ -9,11 +9,11 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { SlotImp::SlotImp( - beast::IP::Endpoint const& localEndpoint, - beast::IP::Endpoint remoteEndpoint, + beast::ip::Endpoint const& localEndpoint, + beast::ip::Endpoint remoteEndpoint, bool fixed, clock_type& clock) : recent(clock) @@ -30,7 +30,7 @@ SlotImp::SlotImp( { } -SlotImp::SlotImp(beast::IP::Endpoint remoteEndpoint, bool fixed, clock_type& clock) +SlotImp::SlotImp(beast::ip::Endpoint remoteEndpoint, bool fixed, clock_type& clock) : recent(clock) , inbound_(false) , fixed_(fixed) @@ -49,29 +49,29 @@ SlotImp::state(State state) { // Must go through activate() to set active state XRPL_ASSERT( - state != State::Active, "xrpl::PeerFinder::SlotImp::state : input state is not active"); + state != State::Active, "xrpl::peer_finder::SlotImp::state : input state is not active"); // The state must be different XRPL_ASSERT( state_ != state, - "xrpl::PeerFinder::SlotImp::state : input state is different from " + "xrpl::peer_finder::SlotImp::state : input state is different from " "current"); // You can't transition into the initial states XRPL_ASSERT( state != State::Accept && state != State::Connect, - "xrpl::PeerFinder::SlotImp::state : input state is not an initial"); + "xrpl::peer_finder::SlotImp::state : input state is not an initial"); // Can only become connected from outbound connect state XRPL_ASSERT( state != State::Connected || (!inbound_ && state_ == State::Connect), - "xrpl::PeerFinder::SlotImp::state : input state is not connected an " + "xrpl::peer_finder::SlotImp::state : input state is not connected an " "invalid state"); // Can't gracefully close on an outbound connection attempt XRPL_ASSERT( state != State::Closing || state_ != State::Connect, - "xrpl::PeerFinder::SlotImp::state : input state is not closing an " + "xrpl::peer_finder::SlotImp::state : input state is not closing an " "invalid state"); state_ = state; @@ -83,7 +83,7 @@ SlotImp::activate(clock_type::time_point const& now) // Can only become active from the accept or connected state XRPL_ASSERT( state_ == State::Accept || state_ == State::Connected, - "xrpl::PeerFinder::SlotImp::activate : valid state"); + "xrpl::peer_finder::SlotImp::activate : valid state"); state_ = State::Active; whenAcceptEndpoints = now; @@ -100,7 +100,7 @@ SlotImp::RecentT::RecentT(clock_type& clock) : cache_(clock) } void -SlotImp::RecentT::insert(beast::IP::Endpoint const& ep, std::uint32_t hops) +SlotImp::RecentT::insert(beast::ip::Endpoint const& ep, std::uint32_t hops) { auto const result(cache_.emplace(ep, hops)); if (!result.second) @@ -115,7 +115,7 @@ SlotImp::RecentT::insert(beast::IP::Endpoint const& ep, std::uint32_t hops) } bool -SlotImp::RecentT::filter(beast::IP::Endpoint const& ep, std::uint32_t hops) +SlotImp::RecentT::filter(beast::ip::Endpoint const& ep, std::uint32_t hops) { auto const iter(cache_.find(ep)); if (iter == cache_.end()) @@ -129,7 +129,7 @@ SlotImp::RecentT::filter(beast::IP::Endpoint const& ep, std::uint32_t hops) void SlotImp::RecentT::expire() { - beast::expire(cache_, Tuning::kLiveCacheSecondsToLive); + beast::expire(cache_, tuning::kLiveCacheSecondsToLive); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/libxrpl/peerfinder/SourceStrings.cpp b/src/libxrpl/peerfinder/SourceStrings.cpp index f47e0cd51d..ca6ff07cba 100644 --- a/src/libxrpl/peerfinder/SourceStrings.cpp +++ b/src/libxrpl/peerfinder/SourceStrings.cpp @@ -8,7 +8,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { class SourceStringsImp : public SourceStrings { @@ -33,9 +33,9 @@ public: results.addresses.reserve(strings_.size()); for (auto const& str : strings_) { - beast::IP::Endpoint ep(beast::IP::Endpoint::fromString(str)); + beast::ip::Endpoint ep(beast::ip::Endpoint::fromString(str)); if (isUnspecified(ep)) - ep = beast::IP::Endpoint::fromString(str); + ep = beast::ip::Endpoint::fromString(str); if (!isUnspecified(ep)) results.addresses.push_back(ep); } @@ -54,4 +54,4 @@ SourceStrings::make(std::string const& name, Strings const& strings) return std::make_shared(name, strings); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/libxrpl/protocol/BuildInfo.cpp b/src/libxrpl/protocol/BuildInfo.cpp index 6ac352f3e1..8a18b3f228 100644 --- a/src/libxrpl/protocol/BuildInfo.cpp +++ b/src/libxrpl/protocol/BuildInfo.cpp @@ -13,7 +13,7 @@ #include #include -namespace xrpl::BuildInfo { +namespace xrpl::build_info { namespace { @@ -173,4 +173,4 @@ isNewerVersion(std::uint64_t version) return false; } -} // namespace xrpl::BuildInfo +} // namespace xrpl::build_info diff --git a/src/libxrpl/protocol/ErrorCodes.cpp b/src/libxrpl/protocol/ErrorCodes.cpp index 87761ce13e..e81f975844 100644 --- a/src/libxrpl/protocol/ErrorCodes.cpp +++ b/src/libxrpl/protocol/ErrorCodes.cpp @@ -9,7 +9,7 @@ #include namespace xrpl { -namespace RPC { +namespace rpc { namespace detail { @@ -215,12 +215,12 @@ errorCodeHttpStatus(ErrorCodeI code) return getErrorInfo(code).httpStatus; } -} // namespace RPC +} // namespace rpc std::string rpcErrorString(json::Value const& jv) { - XRPL_ASSERT(RPC::containsError(jv), "xrpl::RPC::rpcErrorString : input contains an error"); + XRPL_ASSERT(rpc::containsError(jv), "xrpl::rpc::rpcErrorString : input contains an error"); return jv[jss::error].asString() + jv[jss::error_message].asString(); } diff --git a/src/libxrpl/protocol/NFTSyntheticSerializer.cpp b/src/libxrpl/protocol/NFTSyntheticSerializer.cpp index 4f0a2d5071..fd44ae1f33 100644 --- a/src/libxrpl/protocol/NFTSyntheticSerializer.cpp +++ b/src/libxrpl/protocol/NFTSyntheticSerializer.cpp @@ -9,7 +9,7 @@ #include -namespace xrpl::RPC { +namespace xrpl::rpc { void insertNFTSyntheticInJson( @@ -21,4 +21,4 @@ insertNFTSyntheticInJson( insertNFTokenOfferID(response[jss::meta], transaction, transactionMeta); } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/libxrpl/protocol/RPCErr.cpp b/src/libxrpl/protocol/RPCErr.cpp index ec9a3dee9d..c172a1898d 100644 --- a/src/libxrpl/protocol/RPCErr.cpp +++ b/src/libxrpl/protocol/RPCErr.cpp @@ -13,7 +13,7 @@ json::Value rpcError(ErrorCodeI iError) { json::Value jvResult(json::ValueType::Object); - RPC::injectError(iError, jvResult); + rpc::injectError(iError, jvResult); return jvResult; } diff --git a/src/libxrpl/protocol/STParsedJSON.cpp b/src/libxrpl/protocol/STParsedJSON.cpp index 33ca5424d1..6ab272b3d1 100644 --- a/src/libxrpl/protocol/STParsedJSON.cpp +++ b/src/libxrpl/protocol/STParsedJSON.cpp @@ -48,7 +48,7 @@ namespace xrpl { -namespace STParsedJSONDetail { +namespace st_parsed_json_detail { template constexpr U toUnsigned(S value) @@ -93,7 +93,7 @@ makeName(std::string const& object, std::string const& field) static inline json::Value notAnObject(std::string const& object, std::string const& field) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + makeName(object, field) + "' is not a JSON object."); } @@ -106,33 +106,33 @@ notAnObject(std::string const& object) static inline json::Value notAnArray(std::string const& object) { - return RPC::makeError(RpcInvalidParams, "Field '" + object + "' is not a JSON array."); + return rpc::makeError(RpcInvalidParams, "Field '" + object + "' is not a JSON array."); } static inline json::Value unknownField(std::string const& object, std::string const& field) { - return RPC::makeError(RpcInvalidParams, "Field '" + makeName(object, field) + "' is unknown."); + return rpc::makeError(RpcInvalidParams, "Field '" + makeName(object, field) + "' is unknown."); } static inline json::Value outOfRange(std::string const& object, std::string const& field) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + makeName(object, field) + "' is out of range."); } static inline json::Value badType(std::string const& object, std::string const& field) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + makeName(object, field) + "' has bad type."); } static inline json::Value invalidData(std::string const& object, std::string const& field) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + makeName(object, field) + "' has invalid data."); } @@ -145,14 +145,14 @@ invalidData(std::string const& object) static inline json::Value arrayExpected(std::string const& object, std::string const& field) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + makeName(object, field) + "' must be a JSON array."); } static inline json::Value arrayTooBig(std::string const& object, std::string const& field) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + makeName(object, field) + "' exceeds allowed JSON array size of " + std::to_string(kMaxParsedJsonArraySize) + " elements per field."); @@ -161,20 +161,20 @@ arrayTooBig(std::string const& object, std::string const& field) static inline json::Value stringExpected(std::string const& object, std::string const& field) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + makeName(object, field) + "' must be a string."); } static inline json::Value tooDeep(std::string const& object) { - return RPC::makeError(RpcInvalidParams, "Field '" + object + "' exceeds nesting depth limit."); + return rpc::makeError(RpcInvalidParams, "Field '" + object + "' exceeds nesting depth limit."); } static inline json::Value singletonExpected(std::string const& object, unsigned int index) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Field '" + object + "[" + std::to_string(index) + "]' must be an object with a single key/object value."); @@ -183,7 +183,7 @@ singletonExpected(std::string const& object, unsigned int index) static inline json::Value templateMismatch(SField const& sField) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Object '" + sField.getName() + "' contents did not meet requirements for that type."); } @@ -191,7 +191,7 @@ templateMismatch(SField const& sField) static inline json::Value nonObjectInArray(std::string const& item, json::UInt index) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Item '" + item + "' at index " + std::to_string(index) + " is not an object. Arrays may only contain objects."); @@ -791,7 +791,7 @@ parseLeaf( if (pathEl.isMember(jss::currency) && pathEl.isMember(jss::mpt_issuance_id)) { - error = RPC::makeError(RpcInvalidParams, "Invalid Asset."); + error = rpc::makeError(RpcInvalidParams, "Invalid Asset."); return ret; } @@ -1195,13 +1195,13 @@ parseArray( } } -} // namespace STParsedJSONDetail +} // namespace st_parsed_json_detail //------------------------------------------------------------------------------ STParsedJSONObject::STParsedJSONObject(std::string const& name, json::Value const& json) { - using namespace STParsedJSONDetail; + using namespace st_parsed_json_detail; object = parseObject(name, json, sfGeneric, 0, error); } diff --git a/src/libxrpl/protocol/XChainAttestations.cpp b/src/libxrpl/protocol/XChainAttestations.cpp index 792fe5da9d..7c887e785b 100644 --- a/src/libxrpl/protocol/XChainAttestations.cpp +++ b/src/libxrpl/protocol/XChainAttestations.cpp @@ -24,7 +24,7 @@ #include namespace xrpl { -namespace Attestations { +namespace attestations { AttestationBase::AttestationBase( AccountID attestationSignerAccount, @@ -385,7 +385,7 @@ operator==(AttestationCreateAccount const& lhs, AttestationCreateAccount const& std::tie(rhs.createCount, rhs.toCreate, rhs.rewardAmount); } -} // namespace Attestations +} // namespace attestations SField const& XChainClaimAttestation::arrayFieldName{sfXChainClaimAttestations}; SField const& XChainCreateAccountAttestation::arrayFieldName{sfXChainCreateAccountAttestations}; diff --git a/src/libxrpl/resource/Charge.cpp b/src/libxrpl/resource/Charge.cpp index e174c13522..f80588b143 100644 --- a/src/libxrpl/resource/Charge.cpp +++ b/src/libxrpl/resource/Charge.cpp @@ -6,7 +6,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { Charge::Charge(value_type cost, std::string label) : cost_(cost), label_(std::move(label)) { @@ -57,4 +57,4 @@ Charge::operator*(value_type m) const return Charge(cost_ * m, label_); } -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/src/libxrpl/resource/Consumer.cpp b/src/libxrpl/resource/Consumer.cpp index 58d5775a31..934aaddbf5 100644 --- a/src/libxrpl/resource/Consumer.cpp +++ b/src/libxrpl/resource/Consumer.cpp @@ -12,7 +12,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { Consumer::Consumer(Logic& logic, Entry& entry) : logic_(&logic), entry_(&entry) { @@ -99,14 +99,14 @@ Consumer::charge(Charge const& what, std::string const& context) bool Consumer::warn() { - XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::warn : non-null entry"); + XRPL_ASSERT(entry_, "xrpl::resource::Consumer::warn : non-null entry"); return logic_->warn(*entry_); } bool Consumer::disconnect(beast::Journal const& j) { - XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::disconnect : non-null entry"); + XRPL_ASSERT(entry_, "xrpl::resource::Consumer::disconnect : non-null entry"); bool const d = logic_->disconnect(*entry_); if (d) { @@ -118,14 +118,14 @@ Consumer::disconnect(beast::Journal const& j) int Consumer::balance() { - XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::balance : non-null entry"); + XRPL_ASSERT(entry_, "xrpl::resource::Consumer::balance : non-null entry"); return logic_->balance(*entry_); } Entry& Consumer::entry() { - XRPL_ASSERT(entry_, "xrpl::Resource::Consumer::entry : non-null entry"); + XRPL_ASSERT(entry_, "xrpl::resource::Consumer::entry : non-null entry"); return *entry_; } @@ -142,4 +142,4 @@ operator<<(std::ostream& os, Consumer const& v) return os; } -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/src/libxrpl/resource/Fees.cpp b/src/libxrpl/resource/Fees.cpp index bb825fa3c7..037f60051e 100644 --- a/src/libxrpl/resource/Fees.cpp +++ b/src/libxrpl/resource/Fees.cpp @@ -2,7 +2,7 @@ #include -namespace xrpl::Resource { +namespace xrpl::resource { Charge const kFeeMalformedRequest(200, "malformed request"); Charge const kFeeRequestNoReply(10, "unsatisfiable request"); @@ -23,6 +23,6 @@ Charge const kFeeHeavyBurdenPeer(2000, "heavy peer request"); Charge const kFeeWarning(4000, "received warning"); Charge const kFeeDrop(6000, "dropped"); -// See also Resource::Logic::charge for log level cutoff values +// See also resource::Logic::charge for log level cutoff values -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/src/libxrpl/resource/ResourceManager.cpp b/src/libxrpl/resource/ResourceManager.cpp index e3b4d9cc5c..cdfa95facd 100644 --- a/src/libxrpl/resource/ResourceManager.cpp +++ b/src/libxrpl/resource/ResourceManager.cpp @@ -23,7 +23,7 @@ #include #include -namespace xrpl::Resource { +namespace xrpl::resource { class ManagerImp : public Manager { @@ -58,14 +58,14 @@ public: } Consumer - newInboundEndpoint(beast::IP::Endpoint const& address) override + newInboundEndpoint(beast::ip::Endpoint const& address) override { return logic_.newInboundEndpoint(address); } Consumer newInboundEndpoint( - beast::IP::Endpoint const& address, + beast::ip::Endpoint const& address, bool const proxy, std::string_view forwardedFor) override { @@ -85,13 +85,13 @@ public: } Consumer - newOutboundEndpoint(beast::IP::Endpoint const& address) override + newOutboundEndpoint(beast::ip::Endpoint const& address) override { return logic_.newOutboundEndpoint(address); } Consumer - newUnlimitedEndpoint(beast::IP::Endpoint const& address) override + newUnlimitedEndpoint(beast::ip::Endpoint const& address) override { return logic_.newUnlimitedEndpoint(address); } @@ -136,7 +136,7 @@ private: void run() { - beast::setCurrentThreadName("Resource::Mngr"); + beast::setCurrentThreadName("resource::Mngr"); for (;;) { logic_.periodicActivity(); @@ -164,4 +164,4 @@ makeManager(beast::insight::Collector::ptr const& collector, beast::Journal jour return std::make_unique(collector, journal); } -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/src/libxrpl/server/InfoSub.cpp b/src/libxrpl/server/InfoSub.cpp index 353c295856..39883873fb 100644 --- a/src/libxrpl/server/InfoSub.cpp +++ b/src/libxrpl/server/InfoSub.cpp @@ -104,7 +104,7 @@ InfoSub::~InfoSub() } } -Resource::Consumer& +resource::Consumer& InfoSub::getConsumer() { return consumer_; diff --git a/src/libxrpl/server/JSONRPCUtil.cpp b/src/libxrpl/server/JSONRPCUtil.cpp index f38ff280ac..d59582fc1a 100644 --- a/src/libxrpl/server/JSONRPCUtil.cpp +++ b/src/libxrpl/server/JSONRPCUtil.cpp @@ -42,7 +42,7 @@ httpReply(int nStatus, std::string const& content, json::Output const& output, b // CHECKME this returns a different version than the replies below. Is // this by design or an accident or should it be using - // BuildInfo::getFullVersionString () as well? + // build_info::getFullVersionString () as well? output("Server: " + systemName() + "-json-rpc/v1"); output("\r\n"); @@ -123,7 +123,7 @@ httpReply(int nStatus, std::string const& content, json::Output const& output, b "Content-Type: application/json; charset=UTF-8\r\n"); output("Server: " + systemName() + "-json-rpc/"); - output(BuildInfo::getFullVersionString()); + output(build_info::getFullVersionString()); output( "\r\n" "\r\n"); diff --git a/src/libxrpl/server/Port.cpp b/src/libxrpl/server/Port.cpp index c1a79019af..694d4448d5 100644 --- a/src/libxrpl/server/Port.cpp +++ b/src/libxrpl/server/Port.cpp @@ -107,7 +107,7 @@ populate( { // First, check to see if 0.0.0.0 or ipv6 equivalent was configured, // which means all IP addresses. - auto const addr = beast::IP::Endpoint::fromStringChecked(ip); + auto const addr = beast::ip::Endpoint::fromStringChecked(ip); if (addr) { if (isUnspecified(*addr)) diff --git a/src/libxrpl/shamap/SHAMapNodeID.cpp b/src/libxrpl/shamap/SHAMapNodeID.cpp index 16aaafe709..a511fc038c 100644 --- a/src/libxrpl/shamap/SHAMapNodeID.cpp +++ b/src/libxrpl/shamap/SHAMapNodeID.cpp @@ -129,7 +129,8 @@ selectBranch(SHAMapNodeID const& id, uint256 const& hash) SHAMapNodeID SHAMapNodeID::createID(int depth, uint256 const& key) { - XRPL_ASSERT((depth >= 0) && (depth < 65), "xrpl::SHAMapNodeID::createID : valid branch input"); + XRPL_ASSERT( + depth >= 0 && depth <= SHAMap::kLeafDepth, "xrpl::SHAMapNodeID::createID : valid depth"); return SHAMapNodeID(depth, key & depthMask(depth)); } diff --git a/src/libxrpl/shamap/SHAMapSync.cpp b/src/libxrpl/shamap/SHAMapSync.cpp index cc30426f9d..cbed6885c9 100644 --- a/src/libxrpl/shamap/SHAMapSync.cpp +++ b/src/libxrpl/shamap/SHAMapSync.cpp @@ -107,7 +107,7 @@ SHAMap::visitNodes(std::function const& function) const void SHAMap::visitDifferences( - SHAMap const* have, + SHAMap const* map, std::function const& function) const { // Visit every node in this SHAMap that is not present @@ -118,13 +118,13 @@ SHAMap::visitDifferences( if (root_->getHash().isZero()) return; - if ((have != nullptr) && (root_->getHash() == have->root_->getHash())) + if ((map != nullptr) && (root_->getHash() == map->root_->getHash())) return; if (root_->isLeaf()) { auto leaf = intr_ptr::staticPointerCast(root_); - if ((have == nullptr) || !have->hasLeafNode(leaf->peekItem()->key(), leaf->getHash())) + if ((map == nullptr) || !map->hasLeafNode(leaf->peekItem()->key(), leaf->getHash())) function(*root_); return; } @@ -149,18 +149,15 @@ SHAMap::visitDifferences( if (!node->isEmptyBranch(i)) { auto const& childHash = node->getChildHash(i); - SHAMapNodeID const childID = nodeID.getChildNodeID(i); + auto const childID = nodeID.getChildNodeID(i); auto next = descendThrow(node, i); if (next->isInner()) { - if ((have == nullptr) || !have->hasInnerNode(childID, childHash)) + if ((map == nullptr) || !map->hasInnerNode(childID, childHash)) stack.emplace(safeDowncast(next), childID); } - else if ( - (have == nullptr) || - !have->hasLeafNode( - safeDowncast(next)->peekItem()->key(), childHash)) + else if ((map == nullptr) || !map->hasLeafNode(leafKey(*next), childHash)) { if (!function(*next)) return; @@ -414,7 +411,7 @@ SHAMap::getMissingNodes(int max, SHAMapSyncFilter const* filter) bool SHAMap::getNodeFat( SHAMapNodeID const& wanted, - std::vector>& data, + std::vector& data, bool fatLeaves, std::uint32_t depth) const { @@ -460,7 +457,7 @@ SHAMap::getNodeFat( // Add this node to the reply s.erase(); node->serializeForWire(s); - data.emplace_back(nodeID, s.getData()); + data.emplace_back(nodeID, node->isLeaf(), s.getData()); if (node->isInner()) { @@ -490,7 +487,7 @@ SHAMap::getNodeFat( // Just include this node s.erase(); childNode->serializeForWire(s); - data.emplace_back(childID, s.getData()); + data.emplace_back(childID, childNode->isLeaf(), s.getData()); } } } @@ -508,25 +505,33 @@ SHAMap::serializeRoot(Serializer& s) const } SHAMapAddNode -SHAMap::addRootNode(SHAMapHash const& hash, Slice const& rootNode, SHAMapSyncFilter const* filter) +SHAMap::addRootNode( + SHAMapHash const& hash, + SHAMapTreeNodePtr rootNode, + SHAMapSyncFilter const* filter) { + XRPL_ASSERT(cowid_ >= 1, "xrpl::SHAMap::addRootNode : valid cowid"); + XRPL_ASSERT(rootNode, "xrpl::SHAMap::addRootNode : non-null root node"); + // we already have a root_ node if (root_->getHash().isNonZero()) { - JLOG(journal_.trace()) << "got root node, already have one"; - XRPL_ASSERT(root_->getHash() == hash, "xrpl::SHAMap::addRootNode : valid hash input"); + JLOG(journal_.trace()) << "Got root node, already have one"; + XRPL_ASSERT(root_->getHash() == hash, "xrpl::SHAMap::addRootNode : valid hash"); return SHAMapAddNode::duplicate(); } - XRPL_ASSERT(cowid_ >= 1, "xrpl::SHAMap::addRootNode : valid cowid"); - auto node = SHAMapTreeNode::makeFromWire(rootNode); - if (!node || node->getHash() != hash) + if (rootNode->getHash() != hash) + { + JLOG(journal_.warn()) << "Corrupt root node received: expected hash " << hash << ", got " + << rootNode->getHash(); return SHAMapAddNode::invalid(); + } if (backed_) - canonicalize(hash, node); + canonicalize(hash, rootNode); - root_ = node; + root_ = std::move(rootNode); if (root_->isLeaf()) clearSynching(); @@ -543,9 +548,18 @@ SHAMap::addRootNode(SHAMapHash const& hash, Slice const& rootNode, SHAMapSyncFil } SHAMapAddNode -SHAMap::addKnownNode(SHAMapNodeID const& node, Slice const& rawNode, SHAMapSyncFilter const* filter) +SHAMap::addKnownNode( + SHAMapNodeID const& nodeID, + SHAMapTreeNodePtr treeNode, + SHAMapSyncFilter const* filter) { - XRPL_ASSERT(!node.isRoot(), "xrpl::SHAMap::addKnownNode : valid node input"); + XRPL_ASSERT(!nodeID.isRoot(), "xrpl::SHAMap::addKnownNode : valid node"); + XRPL_ASSERT(treeNode, "xrpl::SHAMap::addKnownNode : non-null tree node"); + XRPL_ASSERT( + !treeNode->isLeaf() || + SHAMapNodeID::createID(nodeID.getDepth(), leafKey(*treeNode)).getNodeID() == + nodeID.getNodeID(), + "xrpl::SHAMap::addKnownNode : leaf position consistent with node ID"); if (!isSynching()) { @@ -559,14 +573,15 @@ SHAMap::addKnownNode(SHAMapNodeID const& node, Slice const& rawNode, SHAMapSyncF while (currNode->isInner() && !safeDowncast(currNode)->isFullBelow(generation) && - (currNodeID.getDepth() < node.getDepth())) + (currNodeID.getDepth() < nodeID.getDepth())) { - int const branch = selectBranch(currNodeID, node.getNodeID()); + int const branch = selectBranch(currNodeID, nodeID.getNodeID()); XRPL_ASSERT(branch >= 0, "xrpl::SHAMap::addKnownNode : valid branch"); auto inner = safeDowncast(currNode); if (inner->isEmptyBranch(branch)) { - JLOG(journal_.warn()) << "Add known node for empty branch" << node; + JLOG(journal_.warn()) << "Add known node " << nodeID << " for empty branch " << branch + << " at " << currNodeID; return SHAMapAddNode::invalid(); } @@ -582,67 +597,45 @@ SHAMap::addKnownNode(SHAMapNodeID const& node, Slice const& rawNode, SHAMapSyncF if (currNode != nullptr) continue; - auto newNode = SHAMapTreeNode::makeFromWire(rawNode); - - if (!newNode || childHash != newNode->getHash()) + if (childHash != treeNode->getHash()) { - JLOG(journal_.warn()) << "Corrupt node received"; + JLOG(journal_.warn()) << "Corrupt node " << nodeID << " received: expected hash " + << childHash << ", got " << treeNode->getHash(); return SHAMapAddNode::invalid(); } - // In rare cases, a node can still be corrupt even after hash - // validation. For leaf nodes, we perform an additional check to - // ensure the node's position in the tree is consistent with its - // content to prevent inconsistencies that could - // propagate further down the line. - if (newNode->isLeaf()) - { - auto const& actualKey = - safeDowncast(newNode.get())->peekItem()->key(); - - // Validate that this leaf belongs at the target position - auto const expectedNodeID = SHAMapNodeID::createID(node.getDepth(), actualKey); - if (expectedNodeID.getNodeID() != node.getNodeID()) - { - JLOG(journal_.debug()) - << "Leaf node position mismatch: " - << "expected=" << expectedNodeID.getNodeID() << ", actual=" << node.getNodeID(); - return SHAMapAddNode::invalid(); - } - } - // Inner nodes must be at a level strictly less than 64 // but leaf nodes (while notionally at level 64) can be // at any depth up to and including 64: if ((currNodeID.getDepth() > kLeafDepth) || - (newNode->isInner() && currNodeID.getDepth() == kLeafDepth)) + (treeNode->isInner() && currNodeID.getDepth() == kLeafDepth)) { // Map is provably invalid state_ = SHAMapState::Invalid; return SHAMapAddNode::useful(); } - if (currNodeID != node) + if (currNodeID != nodeID) { // Either this node is broken or we didn't request it (yet) - JLOG(journal_.warn()) << "unable to hook node " << node; + JLOG(journal_.warn()) << "unable to hook node " << nodeID; JLOG(journal_.info()) << " stuck at " << currNodeID; - JLOG(journal_.info()) << "got depth=" << node.getDepth() + JLOG(journal_.info()) << "got depth=" << nodeID.getDepth() << ", walked to= " << currNodeID.getDepth(); return SHAMapAddNode::useful(); } if (backed_) - canonicalize(childHash, newNode); + canonicalize(childHash, treeNode); - newNode = prevNode->canonicalizeChild(branch, std::move(newNode)); + treeNode = prevNode->canonicalizeChild(branch, std::move(treeNode)); if (filter != nullptr) { Serializer s; - newNode->serializeWithPrefix(s); + treeNode->serializeWithPrefix(s); filter->gotNode( - false, childHash, ledgerSeq_, std::move(s.modData()), newNode->getType()); + false, childHash, ledgerSeq_, std::move(s.modData()), treeNode->getType()); } return SHAMapAddNode::useful(); diff --git a/src/libxrpl/tx/invariants/VaultInvariant.cpp b/src/libxrpl/tx/invariants/VaultInvariant.cpp index a9ba0ec874..eca50eb809 100644 --- a/src/libxrpl/tx/invariants/VaultInvariant.cpp +++ b/src/libxrpl/tx/invariants/VaultInvariant.cpp @@ -483,6 +483,12 @@ ValidVault::finalize( result = false; } + if (view.rules().enabled(fixCleanup3_4_0) && afterVault.lossUnrealized < kZero) + { + JLOG(j.fatal()) << "Invariant failed: loss unrealized must not be negative"; + result = false; + } + if (afterVault.assetsTotal < kZero) { JLOG(j.fatal()) << "Invariant failed: assets outstanding must be positive"; diff --git a/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp b/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp index e03cb56fd5..cbfb93c386 100644 --- a/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp +++ b/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp @@ -864,7 +864,7 @@ applyClaimAttestations( return std::unexpected(tecXCHAIN_NO_CLAIM_ID); // Add claims that are part of the signer's list to the "claims" vector - std::vector atts; + std::vector atts; atts.reserve(std::distance(attBegin, attEnd)); for (auto att = attBegin; att != attEnd; ++att) { @@ -1042,7 +1042,7 @@ applyCreateAccountAttestations( return std::unexpected(tecINSUFFICIENT_RESERVE); } - std::vector atts; + std::vector atts; atts.reserve(std::distance(attBegin, attEnd)); for (auto att = attBegin; att != attEnd; ++att) { @@ -1160,8 +1160,8 @@ std::optional toClaim(STTx const& tx) { static_assert( - std::is_same_v || - std::is_same_v); + std::is_same_v || + std::is_same_v); try { @@ -1301,10 +1301,10 @@ attestationDoApply(ApplyContext& ctx) auto const& [srcChain, signersList, quorum, thisDoor, bridgeK] = scopeResult.value(); static_assert( - std::is_same_v || - std::is_same_v); + std::is_same_v || + std::is_same_v); - if constexpr (std::is_same_v) + if constexpr (std::is_same_v) { return applyClaimAttestations( ctx.view(), @@ -1317,7 +1317,7 @@ attestationDoApply(ApplyContext& ctx) quorum, ctx.journal); } - else if constexpr (std::is_same_v) + else if constexpr (std::is_same_v) { return applyCreateAccountAttestations( ctx.view(), @@ -2067,19 +2067,19 @@ XChainCreateClaimID::doApply() NotTEC XChainAddClaimAttestation::preflight(PreflightContext const& ctx) { - return attestationPreflight(ctx); + return attestationPreflight(ctx); } TER XChainAddClaimAttestation::preclaim(PreclaimContext const& ctx) { - return attestationPreclaim(ctx); + return attestationPreclaim(ctx); } TER XChainAddClaimAttestation::doApply() { - return attestationDoApply(ctx_); + return attestationDoApply(ctx_); } //------------------------------------------------------------------------------ @@ -2087,19 +2087,19 @@ XChainAddClaimAttestation::doApply() NotTEC XChainAddAccountCreateAttestation::preflight(PreflightContext const& ctx) { - return attestationPreflight(ctx); + return attestationPreflight(ctx); } TER XChainAddAccountCreateAttestation::preclaim(PreclaimContext const& ctx) { - return attestationPreclaim(ctx); + return attestationPreclaim(ctx); } TER XChainAddAccountCreateAttestation::doApply() { - return attestationDoApply(ctx_); + return attestationDoApply(ctx_); } //------------------------------------------------------------------------------ diff --git a/src/libxrpl/tx/transactors/dex/OfferCreate.cpp b/src/libxrpl/tx/transactors/dex/OfferCreate.cpp index fb47cf0f97..b95d1001e1 100644 --- a/src/libxrpl/tx/transactors/dex/OfferCreate.cpp +++ b/src/libxrpl/tx/transactors/dex/OfferCreate.cpp @@ -283,10 +283,23 @@ OfferCreate::checkAcceptAsset( return asset.visit( [&](Issue const& issue) -> TER { auto const& issuer = issue.getIssuer(); + auto const trustLine = view.read(keylet::trustLine(id, issuer, issue.currency)); + + // Check if the issuer has lsfDisallowIncomingTrustline set. + // If so, the account must already have a trustline to receive tokens. + if (view.rules().enabled(fixCleanup3_4_0) && + issuerAccount->isFlag(lsfDisallowIncomingTrustline)) + { + if (!trustLine) + { + JLOG(j.debug()) << "delay: can't receive IOUs from issuer with " + "DisallowIncomingTrustline set"; + return ((flags & TapRetry) != 0u) ? TER{terNO_LINE} : TER{tecNO_LINE}; + } + } + if (issuerAccount->isFlag(lsfRequireAuth)) { - auto const trustLine = view.read(keylet::trustLine(id, issuer, issue.currency)); - if (!trustLine) { return ((flags & TapRetry) != 0u) ? TER{terNO_LINE} : TER{tecNO_LINE}; @@ -309,8 +322,6 @@ OfferCreate::checkAcceptAsset( } } - auto const trustLine = view.read(keylet::trustLine(id, issue.account, issue.currency)); - if (!trustLine) { return tesSUCCESS; diff --git a/src/libxrpl/tx/transactors/lending/LoanBrokerSet.cpp b/src/libxrpl/tx/transactors/lending/LoanBrokerSet.cpp index e9c153404c..b12cfb692f 100644 --- a/src/libxrpl/tx/transactors/lending/LoanBrokerSet.cpp +++ b/src/libxrpl/tx/transactors/lending/LoanBrokerSet.cpp @@ -34,7 +34,7 @@ LoanBrokerSet::checkExtraFeatures(PreflightContext const& ctx) NotTEC LoanBrokerSet::preflight(PreflightContext const& ctx) { - using namespace Lending; + using namespace lending; auto const& tx = ctx.tx; if (auto const data = tx[~sfData]; diff --git a/src/libxrpl/tx/transactors/lending/LoanManage.cpp b/src/libxrpl/tx/transactors/lending/LoanManage.cpp index a0aa948876..a312dba3b3 100644 --- a/src/libxrpl/tx/transactors/lending/LoanManage.cpp +++ b/src/libxrpl/tx/transactors/lending/LoanManage.cpp @@ -127,23 +127,6 @@ LoanManage::preclaim(PreclaimContext const& ctx) return tesSUCCESS; } -static Number -owedToVault(SLE::ref loanSle) -{ - // Spec section 3.2.3.2, defines the default amount as - // - // DefaultAmount = (Loan.PrincipalOutstanding + Loan.InterestOutstanding) - // - // Loan.InterestOutstanding is not stored directly on ledger. - // It is computed as - // - // Loan.TotalValueOutstanding - Loan.PrincipalOutstanding - - // Loan.ManagementFeeOutstanding - // - // Add that to the original formula, and you get this: - return loanSle->at(sfTotalValueOutstanding) - loanSle->at(sfManagementFeeOutstanding); -} - TER LoanManage::defaultLoan( ApplyView& view, @@ -158,7 +141,7 @@ LoanManage::defaultLoan( std::int32_t const loanScale = loanSle->at(sfLoanScale); auto brokerDebtTotalProxy = brokerSle->at(sfDebtTotal); - Number const totalDefaultAmount = owedToVault(loanSle); + Number const totalDefaultAmount = loanVaultExposure(vaultSle, loanSle); // Apply the First-Loss Capital to the Default Amount TenthBips32 const coverRateMinimum{brokerSle->at(sfCoverRateMinimum)}; @@ -304,7 +287,7 @@ LoanManage::impairLoan( Asset const& vaultAsset, beast::Journal j) { - Number const lossUnrealized = owedToVault(loanSle); + Number const lossUnrealized = loanVaultExposure(vaultSle, loanSle); // The vault may be at a different scale than the loan. Reduce rounding // errors during the accounting by rounding some of the values to that @@ -353,7 +336,7 @@ LoanManage::unimpairLoan( // Update the Vault object(clear "paper loss") auto vaultLossUnrealizedProxy = vaultSle->at(sfLossUnrealized); - Number const lossReversed = owedToVault(loanSle); + Number const lossReversed = loanVaultExposure(vaultSle, loanSle); if (vaultLossUnrealizedProxy < lossReversed) { // LCOV_EXCL_START diff --git a/src/libxrpl/tx/transactors/lending/LoanPay.cpp b/src/libxrpl/tx/transactors/lending/LoanPay.cpp index 54ee85b186..74e8efeda2 100644 --- a/src/libxrpl/tx/transactors/lending/LoanPay.cpp +++ b/src/libxrpl/tx/transactors/lending/LoanPay.cpp @@ -73,7 +73,7 @@ LoanPay::preflight(PreflightContext const& ctx) XRPAmount LoanPay::calculateBaseFee(ReadView const& view, STTx const& tx) { - using namespace Lending; + using namespace lending; auto const normalCost = Transactor::calculateBaseFee(view, tx); @@ -420,10 +420,13 @@ LoanPay::doApply() // LCOV_EXCL_STOP } + auto const [assetsTotalDelta, debtTotalDelta] = loanPaymentDeltas(vaultSle, *paymentParts); + JLOG(j_.debug()) << "Loan Pay: principal paid: " << paymentParts->principalPaid << ", interest paid: " << paymentParts->interestPaid << ", fee paid: " << paymentParts->feePaid - << ", value change: " << paymentParts->valueChange; + << ", assets total delta: " << assetsTotalDelta + << ", debt total delta: " << debtTotalDelta; //------------------------------------------------------ // LoanBroker object state changes @@ -439,13 +442,6 @@ LoanPay::doApply() !asset.integral() || totalPaidToVaultRaw == totalPaidToVaultRounded, "xrpl::LoanPay::doApply", "rounding does nothing for integral asset"); - // Account for value changes when reducing the broker's debt: - // - Positive value change (from full/late/overpayments): Subtract from the - // amount credited toward debt to avoid over-reducing the debt. - // - Negative value change (from full/overpayments): Add to the amount - // credited toward debt,effectively increasing the debt reduction. - auto const totalPaidToVaultForDebt = totalPaidToVaultRaw - paymentParts->valueChange; - auto const totalPaidToBroker = paymentParts->feePaid; XRPL_ASSERT_PARTS( @@ -455,16 +451,16 @@ LoanPay::doApply() "payments add up"); // Decrease LoanBroker Debt by the amount paid, add the Loan value change - // (which might be negative). totalPaidToVaultForDebt may be negative, - // increasing the debt + // (which might be negative). debtTotalDelta may be negative, increasing the + // debt XRPL_ASSERT_PARTS( - isRounded(asset, totalPaidToVaultForDebt, loanScale), + isRounded(asset, debtTotalDelta, loanScale), "xrpl::LoanPay::doApply", - "totalPaidToVaultForDebt rounding good"); + "debtTotalDelta rounding good"); // Despite our best efforts, it's possible for rounding errors to accumulate // in the loan broker's debt total. This is because the broker may have more // than one loan with significantly different scales. - adjustImpreciseNumber(debtTotalProxy, -totalPaidToVaultForDebt, asset, vaultScale); + adjustImpreciseNumber(debtTotalProxy, -debtTotalDelta, asset, vaultScale); //------------------------------------------------------ // Vault object state changes @@ -490,7 +486,7 @@ LoanPay::doApply() #endif assetsAvailableProxy += totalPaidToVaultRounded; - assetsTotalProxy += paymentParts->valueChange; + assetsTotalProxy += assetsTotalDelta; XRPL_ASSERT_PARTS( *assetsAvailableProxy <= *assetsTotalProxy, @@ -543,11 +539,11 @@ LoanPay::doApply() return tecPRECISION_LOSS; // LCOV_EXCL_STOP } - if (paymentParts->valueChange != beast::kZero && assetsTotalAfter == assetsTotalBefore) + if (assetsTotalDelta != beast::kZero && assetsTotalAfter == assetsTotalBefore) { - // Non-zero valueChange with an unchanged assetsTotal indicates that the - // actual value change rounded to zero. That should be impossible, but I - // can't rule it out for extreme edge cases, so fail gracefully if it + // Non-zero assetsTotalDelta with an unchanged assetsTotal indicates that + // the actual value change rounded to zero. That should be impossible, but + // I can't rule it out for extreme edge cases, so fail gracefully if it // happens. // // LCOV_EXCL_START @@ -555,20 +551,21 @@ LoanPay::doApply() << "LoanPay: Vault assets expected change, but unchanged after rounding: " // << "Before: " << assetsTotalBefore // << ", After: " << assetsTotalAfter // - << ", ValueChange: " << paymentParts->valueChange; + << ", AssetsTotalDelta: " << assetsTotalDelta; return tecPRECISION_LOSS; // LCOV_EXCL_STOP } - if (paymentParts->valueChange == beast::kZero && assetsTotalAfter != assetsTotalBefore) + if (assetsTotalDelta == beast::kZero && assetsTotalAfter != assetsTotalBefore) { - // A change in assetsTotal when there was no valueChange indicates that - // something really weird happened. That should be flat out impossible. + // A change in assetsTotal when there was no assetsTotalDelta indicates + // that something really weird happened. That should be flat out + // impossible. // // LCOV_EXCL_START JLOG(j_.fatal()) << "LoanPay: Vault assets changed unexpectedly after rounding: " // << "Before: " << assetsTotalBefore // << ", After: " << assetsTotalAfter // - << ", ValueChange: " << paymentParts->valueChange; + << ", AssetsTotalDelta: " << assetsTotalDelta; return tecINTERNAL; // LCOV_EXCL_STOP } diff --git a/src/libxrpl/tx/transactors/lending/LoanSet.cpp b/src/libxrpl/tx/transactors/lending/LoanSet.cpp index 694d01c69f..95a9581dd3 100644 --- a/src/libxrpl/tx/transactors/lending/LoanSet.cpp +++ b/src/libxrpl/tx/transactors/lending/LoanSet.cpp @@ -53,7 +53,7 @@ LoanSet::getFlagsMask(PreflightContext const& ctx) NotTEC LoanSet::preflight(PreflightContext const& ctx) { - using namespace Lending; + using namespace lending; auto const& tx = ctx.tx; @@ -439,12 +439,12 @@ LoanSet::doApply() principalRequested, properties.loanState.managementFeeDue); - auto const vaultMaximum = *vaultSle->at(sfAssetsMaximum); XRPL_ASSERT_PARTS( - vaultMaximum == 0 || vaultMaximum > *vaultTotalProxy, + *vaultSle->at(sfAssetsMaximum) == 0 || *vaultSle->at(sfAssetsMaximum) > *vaultTotalProxy, "xrpl::LoanSet::doApply", "Vault is below maximum limit"); - if (vaultMaximum != 0 && state.interestDue > vaultMaximum - vaultTotalProxy) + + if (loanOriginationExceedsVaultMaximum(vaultSle, vaultTotalProxy, state.interestDue)) { JLOG(j_.warn()) << "Loan would exceed the maximum assets of the vault"; return tecLIMIT_EXCEEDED; @@ -490,8 +490,9 @@ LoanSet::doApply() auto const loanAssetsToBorrower = principalRequested - originationFee; - auto const newDebtDelta = principalRequested + state.interestDue; - auto const newDebtTotal = brokerSle->at(sfDebtTotal) + newDebtDelta; + auto const [assetsTotalDelta, debtTotalDelta] = + loanOriginationDeltas(vaultSle, principalRequested, state.interestDue); + auto const newDebtTotal = brokerSle->at(sfDebtTotal) + debtTotalDelta; if (auto const debtMaximum = brokerSle->at(sfDebtMaximum); debtMaximum != 0 && debtMaximum < newDebtTotal) { @@ -634,7 +635,7 @@ LoanSet::doApply() // Update the balances in the vault vaultAvailableProxy -= principalRequested; - vaultTotalProxy += state.interestDue; + vaultTotalProxy += assetsTotalDelta; XRPL_ASSERT_PARTS( *vaultAvailableProxy <= *vaultTotalProxy, "xrpl::LoanSet::doApply", @@ -642,7 +643,7 @@ LoanSet::doApply() view.update(vaultSle); // Update the balances in the loan broker - adjustImpreciseNumber(brokerSle->at(sfDebtTotal), newDebtDelta, vaultAsset, vaultScale); + adjustImpreciseNumber(brokerSle->at(sfDebtTotal), debtTotalDelta, vaultAsset, vaultScale); adjustLoanBrokerOwnerCount(view, brokerSle, 1, j_); loanSequenceProxy += 1; // The sequence should be extremely unlikely to roll over, but fail if it diff --git a/src/libxrpl/tx/transactors/vault/VaultCreate.cpp b/src/libxrpl/tx/transactors/vault/VaultCreate.cpp index e1f5873a89..a522f62788 100644 --- a/src/libxrpl/tx/transactors/vault/VaultCreate.cpp +++ b/src/libxrpl/tx/transactors/vault/VaultCreate.cpp @@ -30,6 +30,7 @@ #include #include #include +#include namespace xrpl { @@ -241,6 +242,8 @@ VaultCreate::doApply() } if (scale != 0u) vault->at(sfScale) = scale; + if (view().rules().enabled(featureLendingProtocolV1_1)) + vault->at(sfLEVersion) = std::to_underlying(VaultVersion::CashBasis); view().insert(vault); // Explicitly create MPToken for the vault owner diff --git a/src/test/app/Batch_test.cpp b/src/test/app/Batch_test.cpp index ffaf26b5a7..5230a1f7dd 100644 --- a/src/test/app/Batch_test.cpp +++ b/src/test/app/Batch_test.cpp @@ -3179,7 +3179,7 @@ class Batch_test : public beast::unit_test::Suite auto const brokerKeylet = keylet::loanBroker(lender.id(), env.seq(lender)); { - using namespace loanBroker; + using namespace loan_broker; env(set(lender, vaultKeylet.key), kManagementFeeRate(TenthBips16(100)), kDebtMaximum(debtMaximumValue), diff --git a/src/test/app/ConfidentialTransfer_test.cpp b/src/test/app/ConfidentialTransfer_test.cpp index d3e0182db5..0fc5d6f845 100644 --- a/src/test/app/ConfidentialTransfer_test.cpp +++ b/src/test/app/ConfidentialTransfer_test.cpp @@ -5469,6 +5469,429 @@ class ConfidentialTransfer_test : public ConfidentialTransferTestBase } } + void + testSendOverdraftBulletproof(FeatureBitset features) + { + uint64_t const balance = 100; + testSendOverdraftBulletproofImpl(features, balance, balance); // SUCCEED + testSendOverdraftBulletproofImpl(features, balance, balance + 1); // FAIL + } + + void + testSendOverdraftBulletproofImpl(FeatureBitset features, unsigned balance, unsigned amt) + { + testcase("Send: overdraft prevention via bulletproof"); + using namespace test::jtx; + + // Attack scenario: Alice has 100 tokens, tries to send 101 to Bob. + // The client-side check in mpt-crypto:mpt_utility.cpp:743 prevents honest + // clients from creating this proof. We bypass it by manually + // constructing a forged proof to demonstrate that the ledger's + // range proof verification catches the overdraft. + + Env env{*this, features}; + Account const alice("alice"), bob("bob"), issuer("issuer"); + + uint64_t const aliceBalance = balance; + uint64_t const aliceAmount = amt; + uint64_t const aliceRemaining = aliceBalance - aliceAmount; + + // Setup: Alice has 100 tokens converted to confidential + ConfidentialEnv confEnv{ + env, + issuer, + {{.account = alice, .payAmount = 1000, .convertAmount = aliceBalance}, + {.account = bob, .payAmount = 1000, .convertAmount = 30}}}; + auto& mptIssuer = confEnv.mpt; + + std::pair errors = aliceAmount > aliceBalance + ? std::make_pair(-1, TER(tecBAD_PROOF)) + : std::make_pair(0, TER(tesSUCCESS)); + + unsigned const numParticipants = 3; + + // Verify Alice's actual balance before attack + { + auto const balance = requireOptional( + mptIssuer.getDecryptedBalance(alice, MPTTester::holderEncryptedSpending), + "Missing Alice's balance"); + BEAST_EXPECT(balance == aliceBalance); + } + + // We cannot use ConfidentialSendSetup directly because it would + // call mpt_get_confidential_send_proof which has a client-side + // check (amount > balance) at line 743 in mpt_utility.cpp. + // Instead, we manually construct the transaction components. + + Buffer const randomElgamal = generateBlindingFactor(); + Buffer const randomBalance = generateBlindingFactor(); + + // Create encrypted amounts (using the OVERDRAFT amount) + Buffer const aliceEncAmt = mptIssuer.encryptAmount(alice, aliceAmount, randomElgamal); + Buffer const bobEncAmt = mptIssuer.encryptAmount(bob, aliceAmount, randomElgamal); + Buffer const issuerEncAmt = mptIssuer.encryptAmount(issuer, aliceAmount, randomElgamal); + + // Create commitments + // IMPORTANT: Amount commitment uses same randomness as ElGamal encryption! + Buffer const amtCommit = mptIssuer.getPedersenCommitment(aliceAmount, randomElgamal); + Buffer const balanceCommit = mptIssuer.getPedersenCommitment(aliceBalance, randomBalance); + + // Get Alice's current encrypted spending balance + Buffer const aliceEncBalance = requireOptional( + mptIssuer.getEncryptedBalance(alice, MPTTester::holderEncryptedSpending), + "Missing Alice's encrypted spending balance"); + + uint32_t const version = mptIssuer.getMPTokenVersion(alice); + auto const ctxHash = getSendContextHash( + alice.id(), mptIssuer.issuanceID(), env.seq(alice), bob.id(), version); + + // Now we need to manually generate the sigma proof part. + // The sigma proof verifies ciphertext consistency and commitments, + // but doesn't check the range. We'll construct it with the overdraft + // amount to bypass the client-side check. + + // Generate the sigma proof manually using the lower-level secp256k1 API + auto* ctx = mpt_secp256k1_context(); + Buffer sigmaProof(SECP256K1_COMPACT_STANDARD_PROOF_SIZE); + + // Parse all public keys and ciphertexts + secp256k1_pubkey c1, c2Alice, c2Bob, c2Issuer; + // Parse sender's ciphertext C1 (first 33(kCompressedEcPointLength) bytes) + auto x = secp256k1_ec_pubkey_parse(ctx, &c1, aliceEncAmt.data(), kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1, "Failed to parse C1")) + return; + // Parse C2 components for all recipients + x = secp256k1_ec_pubkey_parse( + ctx, &c2Alice, aliceEncAmt.data() + kCompressedEcPointLength, kCompressedEcPointLength); + auto y = secp256k1_ec_pubkey_parse( + ctx, &c2Bob, bobEncAmt.data() + kCompressedEcPointLength, kCompressedEcPointLength); + auto z = secp256k1_ec_pubkey_parse( + ctx, + &c2Issuer, + issuerEncAmt.data() + kCompressedEcPointLength, + kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1 && y == 1 && z == 1, "Failed to parse C2 components")) + return; + secp256k1_pubkey c2Vec[] = {c2Alice, c2Bob, c2Issuer}; + + // Parse public keys + secp256k1_pubkey pkAlice, pkBob, pkIssuer; + auto alicePubKey = requireOptional(mptIssuer.getPubKey(alice), "Missing alice pubkey"); + auto bobPubKey = requireOptional(mptIssuer.getPubKey(bob), "Missing bob pubkey"); + auto issuerPubKey = requireOptional(mptIssuer.getPubKey(issuer), "Missing issuer pubkey"); + x = secp256k1_ec_pubkey_parse(ctx, &pkAlice, alicePubKey.data(), kCompressedEcPointLength); + y = secp256k1_ec_pubkey_parse(ctx, &pkBob, bobPubKey.data(), kCompressedEcPointLength); + z = secp256k1_ec_pubkey_parse( + ctx, &pkIssuer, issuerPubKey.data(), kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1 && y == 1 && z == 1, "Failed to parse public keys")) + return; + secp256k1_pubkey pkVec[] = {pkAlice, pkBob, pkIssuer}; + + // Parse commitments + secp256k1_pubkey pcAmount, pcBalance, b1, b2; + x = secp256k1_ec_pubkey_parse(ctx, &pcAmount, amtCommit.data(), kCompressedEcPointLength); + y = secp256k1_ec_pubkey_parse( + ctx, &pcBalance, balanceCommit.data(), kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1 && y == 1, "Failed to parse commitments")) + return; + // Parse balance ciphertext + x = secp256k1_ec_pubkey_parse(ctx, &b1, aliceEncBalance.data(), kCompressedEcPointLength); + y = secp256k1_ec_pubkey_parse( + ctx, &b2, aliceEncBalance.data() + kCompressedEcPointLength, kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1 && y == 1, "Failed to parse balance ciphertext")) + return; + + // Get Alice's private key + auto alicePrivKey = requireOptional(mptIssuer.getPrivKey(alice), "Missing alice privkey"); + + // Generate the compact sigma proof (part of mpt_get_confidential_send_proof) + // This will succeed because sigma proof doesn't check amount vs balance + x = secp256k1_compact_standard_prove( + ctx, + sigmaProof.data(), + aliceAmount, + aliceBalance, + randomElgamal.data(), + alicePrivKey.data(), + randomBalance.data(), + numParticipants, + &c1, + c2Vec, + pkVec, + &pcAmount, + &pkAlice, + &pcBalance, + &b1, + &b2, + ctxHash.data()); + if (!BEAST_EXPECTS(x == 1, "Failed to generate sigma proof")) + return; + + // Direct verification + x = secp256k1_compact_standard_verify( + ctx, + sigmaProof.data(), + numParticipants, + &c1, + c2Vec, + pkVec, + &pcAmount, + &pkAlice, + &pcBalance, + &b1, + &b2, + ctxHash.data()); + if (!BEAST_EXPECTS(x == 1, "Sigma verification failed")) + return; + + // Compute the remaining blinding factor: r_remaining = r_balance - r_amount + // This is required because the ledger homomorphically computes: + // C_remaining = C_balance - C_amount = Commit(remaining, r_balance - r_amount) + Buffer randomRemaining(kEcBlindingFactorLength); + Buffer negRandomElgamal(kEcBlindingFactorLength); + secp256k1_mpt_scalar_negate(negRandomElgamal.data(), randomElgamal.data()); + secp256k1_mpt_scalar_add( + randomRemaining.data(), randomBalance.data(), negRandomElgamal.data()); + + // Now forge the bulletproof claiming + auto const forgedBulletproof = getForgedBulletproof( + {aliceAmount, aliceRemaining}, {randomElgamal, randomRemaining}, ctxHash); + + // Combine sigma proof + forged bulletproof + Buffer combinedProof(SECP256K1_COMPACT_STANDARD_PROOF_SIZE + kEcDoubleBulletproofLength); + std::memcpy(combinedProof.data(), sigmaProof.data(), SECP256K1_COMPACT_STANDARD_PROOF_SIZE); + std::memcpy( + combinedProof.data() + SECP256K1_COMPACT_STANDARD_PROOF_SIZE, + forgedBulletproof.data(), + kEcDoubleBulletproofLength); + + // Direct verification + x = mpt_verify_send_range_proof( + combinedProof.data() + SECP256K1_COMPACT_STANDARD_PROOF_SIZE, + amtCommit.data(), + balanceCommit.data(), + ctxHash.data()); + if (!BEAST_EXPECTS(x == errors.first, "Forged proof passed validation")) + return; + + // Attempt the transaction with forged proof + // Expected to FAIL with tecBAD_PROOF + mptIssuer.send({ + .account = alice, + .dest = bob, + .amt = aliceAmount, + .proof = strHex(combinedProof), + .senderEncryptedAmt = aliceEncAmt, + .destEncryptedAmt = bobEncAmt, + .issuerEncryptedAmt = issuerEncAmt, + .amountCommitment = amtCommit, + .balanceCommitment = balanceCommit, + .err = errors.second, + }); + + // Verify Alice's balance unchanged (attack prevented!) + { + auto const balance = requireOptional( + mptIssuer.getDecryptedBalance(alice, MPTTester::holderEncryptedSpending), + "Missing post-attack balance"); + if (aliceAmount > aliceBalance) + { + BEAST_EXPECT(balance == aliceBalance); + } + else + { + BEAST_EXPECT(balance < aliceBalance); + } + } + } + + void + testConvertBackOverdraftBulletproof(FeatureBitset features) + { + uint64_t const balance = 100; + testConvertBackOverdraftBulletproofImpl(features, balance, balance); // SUCCEED + testConvertBackOverdraftBulletproofImpl(features, balance, balance + 1); // FAIL + } + + void + testConvertBackOverdraftBulletproofImpl(FeatureBitset features, uint64_t balance, uint64_t amt) + { + testcase("Convert back: overdraft prevention via bulletproof"); + using namespace test::jtx; + + // Attack scenario: Bob has 100 confidential tokens, tries to convert back 101. + // The client-side check in mpt_get_convert_back_proof would prevent honest + // clients from creating this proof. We bypass it by manually constructing + // a forged proof to demonstrate that the ledger's bulletproof verification + // catches the overdraft. + + Env env{*this, features}; + Account const alice("alice"), bob("bob"), carol("carol"); + + uint64_t const bobBalance = balance; + uint64_t const convertAmount = amt; + uint64_t const bobRemaining = bobBalance - convertAmount; + + // Setup: Bob and Carol both have confidential balance + // Carol ensures outstanding amount >= convertAmount (bypass preclaim check) + // This allows us to test the bulletproof specifically + ConfidentialEnv confEnv{ + env, + alice, + { + {.account = bob, .payAmount = 1000, .convertAmount = bobBalance}, + {.account = carol, + .payAmount = 1000, + .convertAmount = std::max(convertAmount, bobBalance + 1)}, + }}; + auto& mptAlice = confEnv.mpt; + + std::pair errors = convertAmount > bobBalance + ? std::make_pair(-1, TER(tecBAD_PROOF)) + : std::make_pair(0, TER(tesSUCCESS)); + + // Verify Bob's actual balance before attack + { + auto const balance = requireOptional( + mptAlice.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending), + "Missing Bob's balance"); + BEAST_EXPECT(balance == bobBalance); + } + + // We cannot use the standard getConvertBackProof because it calls + // mpt_get_convert_back_proof which has client-side validation. + // Instead, we manually construct the sigma proof and forge the bulletproof. + + Buffer const blindingFactor = generateBlindingFactor(); + Buffer const pcBlindingFactor = generateBlindingFactor(); + + // Create encrypted amounts for the conversion + Buffer const bobEncAmt = mptAlice.encryptAmount(bob, convertAmount, blindingFactor); + Buffer const issuerEncAmt = mptAlice.encryptAmount(alice, convertAmount, blindingFactor); + + // Create Pedersen commitment to the current balance + Buffer const balanceCommit = mptAlice.getPedersenCommitment(bobBalance, pcBlindingFactor); + + // Get Bob's current encrypted spending balance + Buffer const bobEncBalance = requireOptional( + mptAlice.getEncryptedBalance(bob, MPTTester::holderEncryptedSpending), + "Missing Bob's encrypted spending balance"); + + uint32_t const version = mptAlice.getMPTokenVersion(bob); + auto const ctxHash = + getConvertBackContextHash(bob.id(), mptAlice.issuanceID(), env.seq(bob), version); + + // Now manually generate the compact sigma proof for ConvertBack + auto* ctx = mpt_secp256k1_context(); + Buffer sigmaProof(SECP256K1_COMPACT_CONVERTBACK_PROOF_SIZE); + + // Parse the holder's public key + secp256k1_pubkey pkBob; + auto bobPubKey = requireOptional(mptAlice.getPubKey(bob), "Missing bob pubkey"); + auto x = secp256k1_ec_pubkey_parse(ctx, &pkBob, bobPubKey.data(), kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1, "Failed to parse Bob's public key")) + return; + + // Parse balance commitment + secp256k1_pubkey pcBalance; + x = secp256k1_ec_pubkey_parse( + ctx, &pcBalance, balanceCommit.data(), kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1, "Failed to parse balance commitment")) + return; + + // Parse balance ciphertext (B1, B2) + secp256k1_pubkey b1, b2; + x = secp256k1_ec_pubkey_parse(ctx, &b1, bobEncBalance.data(), kCompressedEcPointLength); + auto y = secp256k1_ec_pubkey_parse( + ctx, &b2, bobEncBalance.data() + kCompressedEcPointLength, kCompressedEcPointLength); + if (!BEAST_EXPECTS(x == 1 && y == 1, "Failed to parse balance ciphertext")) + return; + + // Get Bob's private key + auto bobPrivKey = requireOptional(mptAlice.getPrivKey(bob), "Missing bob privkey"); + + // Generate the compact sigma proof for ConvertBack + // This verifies balance ownership and commitment linkage + x = secp256k1_compact_convertback_prove( + ctx, + sigmaProof.data(), + bobBalance, + bobPrivKey.data(), + pcBlindingFactor.data(), + &pkBob, + &b1, + &b2, + &pcBalance, + ctxHash.data()); + if (!BEAST_EXPECTS(x == 1, "Failed to generate convertback sigma proof")) + return; + + // Verify the sigma proof passes (it doesn't check range) + x = secp256k1_compact_convertback_verify( + ctx, sigmaProof.data(), &pkBob, &b1, &b2, &pcBalance, ctxHash.data()); + if (!BEAST_EXPECTS(x == 1, "Sigma verification failed")) + return; + + // Now forge the single bulletproof claiming the remaining balance is valid + // For ConvertBack, we need to prove: (balance - convertAmount) >= 0 + // We create a commitment to the remainder and generate a bulletproof for it + + // The bulletproof needs the blinding factor for the remainder commitment + // The ledger computes: C_remainder = C_balance - convertAmount*G + // So the blinding factor is just pcBlindingFactor (no randomness in convertAmount*G) + + auto const forgedBulletproof = + getForgedSingleBulletproof(bobRemaining, pcBlindingFactor, ctxHash); + + // Combine sigma proof + forged bulletproof + Buffer combinedProof(kEcConvertBackProofLength); + std::memcpy( + combinedProof.data(), sigmaProof.data(), SECP256K1_COMPACT_CONVERTBACK_PROOF_SIZE); + std::memcpy( + combinedProof.data() + SECP256K1_COMPACT_CONVERTBACK_PROOF_SIZE, + forgedBulletproof.data(), + kEcSingleBulletproofLength); + + // Direct verification of the full proof + x = mpt_verify_convert_back_proof( + combinedProof.data(), + bobPubKey.data(), + bobEncBalance.data(), + balanceCommit.data(), + convertAmount, + ctxHash.data()); + if (!BEAST_EXPECTS(x == errors.first, "Forged proof verification mismatch")) + return; + + // Attempt the transaction with forged proof + // Expected to FAIL with tecBAD_PROOF when convertAmount > bobBalance + mptAlice.convertBack({ + .account = bob, + .amt = convertAmount, + .proof = combinedProof, + .holderEncryptedAmt = bobEncAmt, + .issuerEncryptedAmt = issuerEncAmt, + .blindingFactor = blindingFactor, + .pedersenCommitment = balanceCommit, + .err = errors.second, + }); + + // Verify Bob's balance unchanged (attack prevented!) + { + auto const postBalance = requireOptional( + mptAlice.getDecryptedBalance(bob, MPTTester::holderEncryptedSpending), + "Missing post-attack balance"); + if (convertAmount > bobBalance) + { + BEAST_EXPECT(postBalance == bobBalance); + } + else + { + BEAST_EXPECT(postBalance < bobBalance); + } + } + } + void testConvertBackBulletproof(FeatureBitset features) { @@ -8143,6 +8566,7 @@ class ConfidentialTransfer_test : public ConfidentialTransferTestBase testConvertBackWithAuditor(features); testConvertBackPedersenProof(features); testConvertBackBulletproof(features); + testConvertBackOverdraftBulletproof(features); // Homomorphic operation tests testSendHomomorphicOverflow(features); @@ -8177,6 +8601,7 @@ class ConfidentialTransfer_test : public ConfidentialTransferTestBase testSendInvalidProofContextBinding(features); testSendForgedEqualityProof(features); testSendForgedRangeProof(features); + testSendOverdraftBulletproof(features); testSendNegativeValueMalleability(features); testSendFiatShamirBinding(features); testSendProofComponentReuse(features); diff --git a/src/test/app/Delegate_test.cpp b/src/test/app/Delegate_test.cpp index 257ed33619..788514e284 100644 --- a/src/test/app/Delegate_test.cpp +++ b/src/test/app/Delegate_test.cpp @@ -2823,15 +2823,15 @@ class Delegate_test : public beast::unit_test::Suite auto [createTx, keylet] = vault.create({.owner = alice, .asset = xrpIssue()}); env(createTx); - env(loanBroker::set(alice, keylet.key), delegate::As(bob), Ter(temINVALID)); - env(loanBroker::del(alice, keylet.key), delegate::As(bob), Ter(temINVALID)); - env(loanBroker::coverDeposit(alice, keylet.key, XRP(1)), + env(loan_broker::set(alice, keylet.key), delegate::As(bob), Ter(temINVALID)); + env(loan_broker::del(alice, keylet.key), delegate::As(bob), Ter(temINVALID)); + env(loan_broker::coverDeposit(alice, keylet.key, XRP(1)), delegate::As(bob), Ter(temINVALID)); - env(loanBroker::coverWithdraw(alice, keylet.key, XRP(1)), + env(loan_broker::coverWithdraw(alice, keylet.key, XRP(1)), delegate::As(bob), Ter(temINVALID)); - env(loanBroker::coverClawback(alice), delegate::As(bob), Ter(temINVALID)); + env(loan_broker::coverClawback(alice), delegate::As(bob), Ter(temINVALID)); env(loan::set(alice, keylet.key, Number(100)), delegate::As(bob), Ter(temINVALID)); env(loan::manage(alice, keylet.key, 0), delegate::As(bob), Ter(temINVALID)); diff --git a/src/test/app/FixNFTokenPageLinks_test.cpp b/src/test/app/FixNFTokenPageLinks_test.cpp index 7b13fc060b..9be01b2abe 100644 --- a/src/test/app/FixNFTokenPageLinks_test.cpp +++ b/src/test/app/FixNFTokenPageLinks_test.cpp @@ -139,7 +139,7 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite env.fund(XRP(1000), alice); auto const linkFixFee = drops(env.current()->fees().increment); - env(ledgerStateFix::nftPageLinks(alice, alice), Fee(linkFixFee), Ter(temDISABLED)); + env(ledger_state_fix::nftPageLinks(alice, alice), Fee(linkFixFee), Ter(temDISABLED)); } Env env{*this, testableAmendments()}; @@ -151,38 +151,38 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite { // Fail preflight1. Can't combine AccountTxnID and ticket. - json::Value tx = ledgerStateFix::nftPageLinks(alice, alice); + json::Value tx = ledger_state_fix::nftPageLinks(alice, alice); tx[sfAccountTxnID.jsonName] = "00000000000000000000000000000000" "00000000000000000000000000000000"; env(tx, ticket::Use(ticketSeq), Ter(temINVALID)); } // Fee too low. - env(ledgerStateFix::nftPageLinks(alice, alice), Ter(telINSUF_FEE_P)); + env(ledger_state_fix::nftPageLinks(alice, alice), Ter(telINSUF_FEE_P)); // Invalid flags. auto const linkFixFee = drops(env.current()->fees().increment); - env(ledgerStateFix::nftPageLinks(alice, alice), + env(ledger_state_fix::nftPageLinks(alice, alice), Fee(linkFixFee), Txflags(tfPassive), Ter(temINVALID_FLAG)); { - // ledgerStateFix::nftPageLinks requires an Owner field. - json::Value tx = ledgerStateFix::nftPageLinks(alice, alice); + // ledger_state_fix::nftPageLinks requires an Owner field. + json::Value tx = ledger_state_fix::nftPageLinks(alice, alice); tx.removeMember(sfOwner.jsonName); env(tx, Fee(linkFixFee), Ter(temINVALID)); } { // NFTokenPageLink fixes require sfOwner and reject fields that // belong to other LedgerStateFix types. - json::Value tx = ledgerStateFix::nftPageLinks(alice, alice); + json::Value tx = ledger_state_fix::nftPageLinks(alice, alice); tx[sfBookDirectory.jsonName] = to_string(uint256{1}); env(tx, Fee(linkFixFee), Ter(temINVALID)); } { // Invalid LedgerFixType codes. - json::Value tx = ledgerStateFix::nftPageLinks(alice, alice); + json::Value tx = ledger_state_fix::nftPageLinks(alice, alice); tx[sfLedgerFixType.jsonName] = 0; env(tx, Fee(linkFixFee), Ter(tefINVALID_LEDGER_FIX_TYPE)); @@ -193,7 +193,9 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite // Preclaim Account const carol("carol"); env.memoize(carol); - env(ledgerStateFix::nftPageLinks(alice, carol), Fee(linkFixFee), Ter(tecOBJECT_NOT_FOUND)); + env(ledger_state_fix::nftPageLinks(alice, carol), + Fee(linkFixFee), + Ter(tecOBJECT_NOT_FOUND)); } void @@ -214,13 +216,17 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite // Owner has no pages to fix. auto const linkFixFee = drops(env.current()->fees().increment); - env(ledgerStateFix::nftPageLinks(alice, alice), Fee(linkFixFee), Ter(tecFAILED_PROCESSING)); + env(ledger_state_fix::nftPageLinks(alice, alice), + Fee(linkFixFee), + Ter(tecFAILED_PROCESSING)); // Alice has only one page. env(token::mint(alice), Txflags(tfTransferable)); env.close(); - env(ledgerStateFix::nftPageLinks(alice, alice), Fee(linkFixFee), Ter(tecFAILED_PROCESSING)); + env(ledger_state_fix::nftPageLinks(alice, alice), + Fee(linkFixFee), + Ter(tecFAILED_PROCESSING)); // Alice has at least three pages. for (std::uint32_t i = 0; i < 64; ++i) @@ -229,7 +235,9 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite env.close(); } - env(ledgerStateFix::nftPageLinks(alice, alice), Fee(linkFixFee), Ter(tecFAILED_PROCESSING)); + env(ledger_state_fix::nftPageLinks(alice, alice), + Fee(linkFixFee), + Ter(tecFAILED_PROCESSING)); } void @@ -439,7 +447,7 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite //********************************************************************** // Verify that the LedgerStateFix transaction is not enabled. auto const linkFixFee = drops(env.current()->fees().increment); - env(ledgerStateFix::nftPageLinks(daria, alice), Fee(linkFixFee), Ter(temDISABLED)); + env(ledger_state_fix::nftPageLinks(daria, alice), Fee(linkFixFee), Ter(temDISABLED)); // Wait 15 ledgers so the LedgerStateFix transaction is no longer // retried. @@ -475,7 +483,7 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite env(noop(daria)); // daria fixes the links in alice's NFToken directory. - env(ledgerStateFix::nftPageLinks(daria, alice), Fee(linkFixFee)); + env(ledger_state_fix::nftPageLinks(daria, alice), Fee(linkFixFee)); env.close(); // alice's last page should now be present and include no links. @@ -516,7 +524,7 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite } // daria fixes the links in bob's NFToken directory. - env(ledgerStateFix::nftPageLinks(daria, bob), Fee(linkFixFee)); + env(ledger_state_fix::nftPageLinks(daria, bob), Fee(linkFixFee)); env.close(); // bob's last page should now be present and include a previous @@ -574,7 +582,7 @@ class FixNFTokenPageLinks_test : public beast::unit_test::Suite } // carol fixes the links in their own NFToken directory. - env(ledgerStateFix::nftPageLinks(carol, carol), Fee(linkFixFee)); + env(ledger_state_fix::nftPageLinks(carol, carol), Fee(linkFixFee)); env.close(); { diff --git a/src/test/app/Invariants_test.cpp b/src/test/app/Invariants_test.cpp index eaf1f2704c..9bc9524f80 100644 --- a/src/test/app/Invariants_test.cpp +++ b/src/test/app/Invariants_test.cpp @@ -2412,7 +2412,7 @@ class Invariants_test : public beast::unit_test::Suite vaultID = vKeylet.key; // Create Loan Broker - using namespace loanBroker; + using namespace loan_broker; auto const loanBrokerKeylet = keylet::loanBroker(a.id(), env.seq(a)); // Create a Loan Broker with all default values. @@ -2721,7 +2721,7 @@ class Invariants_test : public beast::unit_test::Suite brokerKeylet = this->createLoanBroker(alice, env, asset); if (!BEAST_EXPECT(env.le(brokerKeylet))) return false; - env(loanBroker::coverDeposit(alice, brokerKeylet.key, asset(10))); + env(loan_broker::coverDeposit(alice, brokerKeylet.key, asset(10))); env.close(); return BEAST_EXPECT(env.le(brokerKeylet)); }; @@ -3374,6 +3374,41 @@ class Invariants_test : public beast::unit_test::Suite precloseXrp, TxAccount::A2); + // A negative loss unrealized must trip the invariant. ttLOAN_MANAGE is + // allowed to change loss unrealized, so it isolates this check from the + // "must not change loss unrealized" invariant. Gated behind + // fixCleanup3_4_0 (see below). + doInvariantCheck( + {"loss unrealized must not be negative"}, + [&](Account const& a1, Account const& a2, ApplyContext& ac) { + auto const keylet = keylet::vault(a1.id(), ac.view().seq()); + return kAdjust(ac.view(), keylet, kArgs(a2.id(), 0, [&](Adjustments& sample) { + sample.lossUnrealized = -1; + })); + }, + XRPAmount{}, + STTx{ttLOAN_MANAGE, [](STObject& tx) {}}, + {tecINVARIANT_FAILED, tecINVARIANT_FAILED}, + precloseXrp, + TxAccount::A2); + + // Without fixCleanup3_4_0 the same state must NOT trip the invariant, + // preserving pre-amendment behavior (no fork risk). + doInvariantCheck( + makeEnv(defaultAmendments() - fixCleanup3_4_0), + {}, + [&](Account const& a1, Account const& a2, ApplyContext& ac) { + auto const keylet = keylet::vault(a1.id(), ac.view().seq()); + return kAdjust(ac.view(), keylet, kArgs(a2.id(), 0, [&](Adjustments& sample) { + sample.lossUnrealized = -1; + })); + }, + XRPAmount{}, + STTx{ttLOAN_MANAGE, [](STObject& tx) {}}, + {tesSUCCESS, tesSUCCESS}, + precloseXrp, + TxAccount::A2); + doInvariantCheck( {"set assets outstanding must not exceed assets maximum"}, [&](Account const& a1, Account const& a2, ApplyContext& ac) { diff --git a/src/test/app/LedgerNodeHelpers_test.cpp b/src/test/app/LedgerNodeHelpers_test.cpp new file mode 100644 index 0000000000..a9e4e3ebfc --- /dev/null +++ b/src/test/app/LedgerNodeHelpers_test.cpp @@ -0,0 +1,260 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include + +namespace xrpl::tests { + +class LedgerNodeHelpers_test : public beast::unit_test::Suite +{ + static boost::intrusive_ptr + makeTestItem(std::uint32_t seed) + { + Serializer s; + s.add32(seed); + s.add32(seed + 1); + s.add32(seed + 2); + return makeShamapitem(s.getSHA512Half(), s.slice()); + } + + static std::string + serializeNode(SHAMapTreeNodePtr const& node) + { + Serializer s; + node->serializeForWire(s); + auto const slice = s.slice(); + return std::string(slice.begin(), slice.end()); + } + + void + testGetTreeNode() + { + testcase("getTreeNode"); + + // Valid: inner node. It must have at least one child for `serializeNode` to work. + { + auto const innerNode = intr_ptr::makeShared(1); + auto const childNode = intr_ptr::makeShared(1); + innerNode->setChild(0, childNode); + auto const innerData = serializeNode(innerNode); + auto const result = getTreeNode(innerData); + BEAST_EXPECT(result && result->isInner()); + } + + // Valid: leaf node. + { + auto const leafItem = makeTestItem(12345); + auto const leafNode = intr_ptr::makeShared(leafItem, 1); + auto const leafData = serializeNode(leafNode); + auto const result = getTreeNode(leafData); + BEAST_EXPECT(result && result->isLeaf()); + } + + // Invalid: empty data. + { + auto const result = getTreeNode(""); + BEAST_EXPECT(!result); + } + + // Invalid: garbage data. + { + auto const result = getTreeNode("invalid"); + BEAST_EXPECT(!result); + } + + // Invalid: truncated data. + { + auto const leafItem = makeTestItem(54321); + auto const leafNode = intr_ptr::makeShared(leafItem, 1); + // Truncate the data to trigger an exception in SHAMapTreeNode::makeAccountState when + // the data is used to deserialize the node. + uint256 const tag; + auto const leafData = serializeNode(leafNode).substr(0, tag.kBytes - 1); + auto const result = getTreeNode(leafData); + BEAST_EXPECT(!result); + } + } + + void + testGetSHAMapNodeID() + { + testcase("getSHAMapNodeID"); + + { + // Tests using inner nodes at various depths. + auto const innerNode = intr_ptr::makeShared(1); + auto const childNode = intr_ptr::makeShared(1); + innerNode->setChild(0, childNode); + auto const innerData = serializeNode(innerNode); + + // Valid: legacy `nodeid` field at arbitrary depth. + { + auto const innerDepth = 3; + auto const innerID = SHAMapNodeID::createID(innerDepth, uint256{}); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(innerData); + ledgerNode.set_nodeid(innerID.getRawString()); + auto const result = getSHAMapNodeID(ledgerNode, *innerNode); + BEAST_EXPECT(result == innerID); + } + + // Valid: new `id` field at minimum depth. + { + auto const innerDepth = 0; + auto const innerID = SHAMapNodeID::createID(innerDepth, uint256{}); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(innerData); + ledgerNode.set_id(innerID.getRawString()); + auto const result = getSHAMapNodeID(ledgerNode, *innerNode); + BEAST_EXPECT(result == innerID); + } + + // Invalid: new `depth` field should not be used for inner nodes. + { + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(innerData); + ledgerNode.set_depth(10); + auto const result = getSHAMapNodeID(ledgerNode, *innerNode); + BEAST_EXPECT(!result); + } + + // Invalid: both legacy `nodeid` and new `id` fields set for an inner node. + { + auto const innerDepth = 9; + auto const innerID = SHAMapNodeID::createID(innerDepth, uint256{}); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(innerData); + ledgerNode.set_nodeid(innerID.getRawString()); + ledgerNode.set_id(innerID.getRawString()); + auto const result = getSHAMapNodeID(ledgerNode, *innerNode); + BEAST_EXPECT(!result); + } + } + + { + // Tests using leaf nodes at various depths. + auto const leafItem = makeTestItem(12345); + auto const leafNode = intr_ptr::makeShared(leafItem, 1); + auto const leafData = serializeNode(leafNode); + auto const leafKey = leafItem->key(); + + // Valid: legacy `nodeid` field at arbitrary depth. + { + auto const kLeafDepth = 5; + auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(leafData); + ledgerNode.set_nodeid(leafID.getRawString()); + auto const result = getSHAMapNodeID(ledgerNode, *leafNode); + BEAST_EXPECT(result == leafID); + } + + // Invalid: new `id` field should not be used for leaf nodes. + { + auto const kLeafDepth = 5; + auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(leafData); + ledgerNode.set_id(leafID.getRawString()); + auto const result = getSHAMapNodeID(ledgerNode, *leafNode); + BEAST_EXPECT(!result); + } + + // Valid: new `depth` field at minimum depth. + { + auto const kLeafDepth = 0; + auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(leafData); + ledgerNode.set_depth(kLeafDepth); + auto const result = getSHAMapNodeID(ledgerNode, *leafNode); + BEAST_EXPECT(result == leafID); + } + + // Valid: new `depth` field at arbitrary depth between minimum and maximum. + { + auto const kLeafDepth = 10; + auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(leafData); + ledgerNode.set_depth(kLeafDepth); + auto const result = getSHAMapNodeID(ledgerNode, *leafNode); + BEAST_EXPECT(result == leafID); + } + + // Valid: new `depth` field at maximum depth. + // Note that we do not test a depth greater than the maximum depth, because the proto + // message is assumed to have been validated by the time the getSHAMapNodeID function is + // called. + { + auto const kLeafDepth = SHAMap::kLeafDepth; + auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(leafData); + ledgerNode.set_depth(kLeafDepth); + auto const result = getSHAMapNodeID(ledgerNode, *leafNode); + BEAST_EXPECT(result == leafID); + } + + // Invalid: legacy `nodeid` field where the node ID is inconsistent with the key. + { + auto const otherItem = makeTestItem(54321); + auto const otherNode = + intr_ptr::makeShared(otherItem, 1); + auto const otherData = serializeNode(otherNode); + auto const otherKey = otherItem->key(); + auto const otherDepth = 1; + auto const otherID = SHAMapNodeID::createID(otherDepth, otherKey); + + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata(otherData); + ledgerNode.set_nodeid(otherID.getRawString()); + auto const result = getSHAMapNodeID(ledgerNode, *leafNode); + BEAST_EXPECT(!result); + } + } + + // Invalid: no field set. + { + auto const innerNode = intr_ptr::makeShared(1); + protocol::TMLedgerNode ledgerNode; + ledgerNode.set_nodedata("test_data"); + auto const result = getSHAMapNodeID(ledgerNode, *innerNode); + BEAST_EXPECT(!result); + } + } + +public: + void + run() override + { + testGetTreeNode(); + testGetSHAMapNodeID(); + } +}; + +BEAST_DEFINE_TESTSUITE(LedgerNodeHelpers, app, xrpl); + +} // namespace xrpl::tests diff --git a/src/test/app/LedgerReplay_test.cpp b/src/test/app/LedgerReplay_test.cpp index 4cc83608d6..7b521402a4 100644 --- a/src/test/app/LedgerReplay_test.cpp +++ b/src/test/app/LedgerReplay_test.cpp @@ -280,13 +280,13 @@ public: send(std::shared_ptr const& m) override { } - [[nodiscard]] beast::IP::Endpoint + [[nodiscard]] beast::ip::Endpoint getRemoteAddress() const override { return {}; } void - charge(Resource::Charge const& fee, std::string const& context = {}) override + charge(resource::Charge const& fee, std::string const& context = {}) override { } [[nodiscard]] id_t @@ -1206,7 +1206,7 @@ struct LedgerReplayer_test : public beast::unit_test::Suite if (serverResult != expecting) return false; - beast::IP::Address const addr = boost::asio::ip::make_address("172.1.1.100"); + beast::ip::Address const addr = boost::asio::ip::make_address("172.1.1.100"); jtx::Env serverEnv(*this); serverEnv.app().config().ledgerReplay = server; auto httpResp = xrpl::makeResponse( diff --git a/src/test/app/LendingHelpers_test.cpp b/src/test/app/LendingHelpers_test.cpp index ac8e0764fc..5d67cdc3c5 100644 --- a/src/test/app/LendingHelpers_test.cpp +++ b/src/test/app/LendingHelpers_test.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include #include +#include #include namespace xrpl::test { @@ -1470,6 +1472,326 @@ class LendingHelpers_test : public beast::unit_test::Suite Number{-18304, -5})); } + void + testAccrualLoanOriginationDeltas() + { + using namespace xrpl::accrual; + + struct TestCase + { + std::string name; + Number principalRequested; + Number interestDue; + }; + + auto const testCases = std::vector{ + {.name = "Zero interest", + .principalRequested = Number{1'000}, + .interestDue = Number{0}}, + {.name = "Nonzero interest", + .principalRequested = Number{1'000}, + .interestDue = Number{75}}, + }; + + for (auto const& tc : testCases) + { + testcase("accrual::loanOriginationDeltas: " + tc.name); + + auto const deltas = loanOriginationDeltas(tc.principalRequested, tc.interestDue); + BEAST_EXPECTS( + deltas.assetsTotalDelta == tc.interestDue, + "assetsTotalDelta mismatch: expected " + to_string(tc.interestDue) + ", got " + + to_string(deltas.assetsTotalDelta)); + BEAST_EXPECTS( + deltas.debtTotalDelta == tc.principalRequested + tc.interestDue, + "debtTotalDelta mismatch: expected " + + to_string(tc.principalRequested + tc.interestDue) + ", got " + + to_string(deltas.debtTotalDelta)); + } + } + + void + testCashBasisLoanOriginationDeltas() + { + using namespace xrpl::cash_basis; + + testcase("cash_basis::loanOriginationDeltas: interestDue is ignored"); + + Number const principalRequested{1'000}; + Number const interestDue{75}; + + auto const deltas = loanOriginationDeltas(principalRequested); + BEAST_EXPECTS( + deltas.assetsTotalDelta == 0, + "assetsTotalDelta mismatch: expected 0, got " + to_string(deltas.assetsTotalDelta)); + BEAST_EXPECTS( + deltas.debtTotalDelta == principalRequested, + "debtTotalDelta mismatch: expected " + to_string(principalRequested) + ", got " + + to_string(deltas.debtTotalDelta)); + } + + void + testAccrualLoanOriginationExceedsVaultMaximum() + { + using namespace xrpl::accrual; + + struct TestCase + { + std::string name; + Number vaultMaximum; + Number vaultTotal; + Number interestDue; + bool expected; + }; + + auto const testCases = std::vector{ + {.name = "No maximum configured", + .vaultMaximum = Number{0}, + .vaultTotal = Number{900}, + .interestDue = Number{1'000}, + .expected = false}, + {.name = "Interest fits under headroom", + .vaultMaximum = Number{1'000}, + .vaultTotal = Number{900}, + .interestDue = Number{50}, + .expected = false}, + {.name = "Interest exactly fills headroom", + .vaultMaximum = Number{1'000}, + .vaultTotal = Number{900}, + .interestDue = Number{100}, + .expected = false}, + {.name = "Interest exceeds headroom", + .vaultMaximum = Number{1'000}, + .vaultTotal = Number{900}, + .interestDue = Number{101}, + .expected = true}, + }; + + for (auto const& tc : testCases) + { + testcase("accrual::loanOriginationExceedsVaultMaximum: " + tc.name); + BEAST_EXPECT( + loanOriginationExceedsVaultMaximum( + tc.vaultMaximum, tc.vaultTotal, tc.interestDue) == tc.expected); + } + } + + // Constructs a minimal ltLOAN SLE with just the fields needed by + // loanVaultExposure. Mirrors the bare-SLE pattern used by + // testCanApplyToBrokerCover for ltLOAN_BROKER. + static std::shared_ptr + makeLoanSle( + Number const& totalValueOutstanding, + Number const& principalOutstanding, + Number const& managementFeeOutstanding) + { + auto sle = std::make_shared(ltLOAN, uint256{1u}); + sle->at(sfTotalValueOutstanding) = totalValueOutstanding; + sle->at(sfPrincipalOutstanding) = principalOutstanding; + sle->at(sfManagementFeeOutstanding) = managementFeeOutstanding; + return sle; + } + + // Constructs a minimal ltVAULT SLE with just LEVersion set (or left + // absent), for exercising the dispatchers' per-Vault gating. + static std::shared_ptr + makeVaultSle( + std::optional leVersion = std::nullopt, + std::optional assetsMaximum = std::nullopt, + std::optional assetsTotal = std::nullopt) + { + auto sle = std::make_shared(ltVAULT, uint256{2u}); + if (leVersion) + sle->at(sfLEVersion) = std::to_underlying(*leVersion); + if (assetsMaximum) + sle->at(sfAssetsMaximum) = *assetsMaximum; + if (assetsTotal) + sle->at(sfAssetsTotal) = *assetsTotal; + return sle; + } + + void + testAccrualLoanVaultExposure() + { + testcase("accrual::loanVaultExposure"); + + auto sle = makeLoanSle(Number{1'000}, Number{800}, Number{50}); + BEAST_EXPECT(xrpl::accrual::loanVaultExposure(sle) == Number{950}); + } + + void + testCashBasisLoanVaultExposure() + { + testcase("cash_basis::loanVaultExposure"); + + auto sle = makeLoanSle(Number{1'000}, Number{800}, Number{50}); + BEAST_EXPECT(xrpl::cash_basis::loanVaultExposure(sle) == Number{800}); + } + + void + testLoanPaymentDeltas() + { + // principalPaid, interestPaid, feePaid, valueChange are all distinct + // and nonzero, with a nonzero valueChange simulating a late-payment + // penalty, so Accrual's formula is meaningfully exercised. + LoanPaymentParts const parts{ + .principalPaid = Number{100}, + .interestPaid = Number{20}, + .valueChange = Number{5}, + .feePaid = Number{3}}; + + { + testcase("accrual::loanPaymentDeltas: nonzero valueChange"); + auto const deltas = xrpl::accrual::loanPaymentDeltas(parts); + BEAST_EXPECT(deltas.assetsTotalDelta == parts.valueChange); + BEAST_EXPECT( + deltas.debtTotalDelta == + (parts.principalPaid + parts.interestPaid) - parts.valueChange); + } + + { + testcase("cash_basis::loanPaymentDeltas: nonzero valueChange ignored"); + auto const deltas = xrpl::cash_basis::loanPaymentDeltas(parts); + BEAST_EXPECT(deltas.assetsTotalDelta == parts.interestPaid); + BEAST_EXPECT(deltas.debtTotalDelta == parts.principalPaid); + } + } + + void + testLoanOriginationDeltasDispatcher() + { + using namespace jtx; + + Number const principalRequested{1'000}; + Number const interestDue{75}; + + auto const legacyVault = makeVaultSle(); + auto const cashBasisVault = makeVaultSle(VaultVersion::CashBasis); + + { + testcase( + "loanOriginationDeltas dispatcher: amendment enabled, legacy vault picks " + "Accrual"); + Env const env{*this}; + auto const deltas = loanOriginationDeltas(legacyVault, principalRequested, interestDue); + auto const expected = + xrpl::accrual::loanOriginationDeltas(principalRequested, interestDue); + BEAST_EXPECT(deltas.assetsTotalDelta == expected.assetsTotalDelta); + BEAST_EXPECT(deltas.debtTotalDelta == expected.debtTotalDelta); + } + + { + testcase( + "loanOriginationDeltas dispatcher: amendment enabled, LEVersion == " + "VaultVersion::CashBasis picks CashBasis"); + Env const env{*this}; + auto const deltas = + loanOriginationDeltas(cashBasisVault, principalRequested, interestDue); + auto const expected = xrpl::cash_basis::loanOriginationDeltas(principalRequested); + BEAST_EXPECT(deltas.assetsTotalDelta == expected.assetsTotalDelta); + BEAST_EXPECT(deltas.debtTotalDelta == expected.debtTotalDelta); + } + } + + void + testLoanOriginationExceedsVaultMaximumDispatcher() + { + using namespace jtx; + + Number const vaultMaximum{1'000}; + Number const vaultTotal{900}; + // Exceeds Accrual's headroom (100), but must never trip CashBasis. + Number const interestDue{101}; + + auto const legacyVault = makeVaultSle(std::nullopt, vaultMaximum, vaultTotal); + auto const cashBasisVault = makeVaultSle(VaultVersion::CashBasis, vaultMaximum, vaultTotal); + + { + testcase( + "loanOriginationExceedsVaultMaximum dispatcher: amendment enabled, legacy vault " + "picks Accrual"); + Env const env{*this}; + BEAST_EXPECT( + loanOriginationExceedsVaultMaximum(legacyVault, vaultTotal, interestDue) == + xrpl::accrual::loanOriginationExceedsVaultMaximum( + vaultMaximum, vaultTotal, interestDue)); + } + + { + testcase( + "loanOriginationExceedsVaultMaximum dispatcher: amendment enabled, LEVersion == " + "VaultVersion::CashBasis picks CashBasis"); + Env const env{*this}; + BEAST_EXPECT( + loanOriginationExceedsVaultMaximum(cashBasisVault, vaultTotal, interestDue) == + false); + } + } + + void + testLoanVaultExposureDispatcher() + { + using namespace jtx; + + auto const legacyVault = makeVaultSle(); + auto const cashBasisVault = makeVaultSle(VaultVersion::CashBasis); + + { + testcase("loanVaultExposure dispatcher: amendment enabled, legacy vault picks Accrual"); + Env const env{*this}; + auto sle = makeLoanSle(Number{1'000}, Number{800}, Number{50}); + BEAST_EXPECT( + loanVaultExposure(legacyVault, sle) == xrpl::accrual::loanVaultExposure(sle)); + } + + { + testcase( + "loanVaultExposure dispatcher: amendment enabled, LEVersion == " + "VaultVersion::CashBasis " + "picks CashBasis"); + Env const env{*this}; + auto sle = makeLoanSle(Number{1'000}, Number{800}, Number{50}); + BEAST_EXPECT( + loanVaultExposure(cashBasisVault, sle) == xrpl::cash_basis::loanVaultExposure(sle)); + } + } + + void + testLoanPaymentDeltasDispatcher() + { + using namespace jtx; + + LoanPaymentParts const parts{ + .principalPaid = Number{100}, + .interestPaid = Number{20}, + .valueChange = Number{5}, + .feePaid = Number{3}}; + + auto const legacyVault = makeVaultSle(); + auto const cashBasisVault = makeVaultSle(VaultVersion::CashBasis); + + { + testcase("loanPaymentDeltas dispatcher: amendment enabled, legacy vault picks Accrual"); + Env const env{*this}; + auto const deltas = loanPaymentDeltas(legacyVault, parts); + auto const expected = xrpl::accrual::loanPaymentDeltas(parts); + BEAST_EXPECT(deltas.assetsTotalDelta == expected.assetsTotalDelta); + BEAST_EXPECT(deltas.debtTotalDelta == expected.debtTotalDelta); + } + + { + testcase( + "loanPaymentDeltas dispatcher: amendment enabled, LEVersion == " + "VaultVersion::CashBasis " + "picks CashBasis"); + Env const env{*this}; + auto const deltas = loanPaymentDeltas(cashBasisVault, parts); + auto const expected = xrpl::cash_basis::loanPaymentDeltas(parts); + BEAST_EXPECT(deltas.assetsTotalDelta == expected.assetsTotalDelta); + BEAST_EXPECT(deltas.debtTotalDelta == expected.debtTotalDelta); + } + } + public: void testCanApplyToBrokerCover() @@ -1573,6 +1895,17 @@ public: testComputeOverpaymentComponents(); testComputeInterestAndFeeParts(); testCanApplyToBrokerCover(); + + testAccrualLoanOriginationDeltas(); + testCashBasisLoanOriginationDeltas(); + testAccrualLoanOriginationExceedsVaultMaximum(); + testAccrualLoanVaultExposure(); + testCashBasisLoanVaultExposure(); + testLoanPaymentDeltas(); + testLoanOriginationDeltasDispatcher(); + testLoanOriginationExceedsVaultMaximumDispatcher(); + testLoanVaultExposureDispatcher(); + testLoanPaymentDeltasDispatcher(); } }; diff --git a/src/test/app/LoanBroker_test.cpp b/src/test/app/LoanBroker_test.cpp index f6f85a0cca..ee398bfc3b 100644 --- a/src/test/app/LoanBroker_test.cpp +++ b/src/test/app/LoanBroker_test.cpp @@ -93,7 +93,7 @@ class LoanBroker_test : public beast::unit_test::Suite env.close(); BEAST_EXPECT(static_cast(env.le(keylet)) == goodVault); - using namespace loanBroker; + using namespace loan_broker; // Can't create a loan broker regardless of whether the vault exists env(set(alice, keylet.key), Ter(temDISABLED)); auto const brokerKeylet = keylet::loanBroker(alice.id(), env.seq(alice)); @@ -168,7 +168,7 @@ class LoanBroker_test : public beast::unit_test::Suite } using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; // Bogus assets to use in test cases static PrettyAsset const kBadMptAsset = [&]() { @@ -645,8 +645,8 @@ class LoanBroker_test : public beast::unit_test::Suite } } - using namespace loanBroker; - using namespace xrpl::Lending; + using namespace loan_broker; + using namespace xrpl::lending; TenthBips32 const tenthBipsZero{0}; @@ -862,7 +862,7 @@ class LoanBroker_test : public beast::unit_test::Suite LoanBrokerTest brokerTest) { using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer{"issuer"}; Account const alice{"alice"}; Env env(*this); @@ -1093,7 +1093,7 @@ class LoanBroker_test : public beast::unit_test::Suite { testcase("Invalid LoanBrokerCoverClawback"); using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; // preflight { @@ -1220,7 +1220,7 @@ class LoanBroker_test : public beast::unit_test::Suite auto const brokerKeylet = keylet::loanBroker(alice.id(), env.seq(alice)); // Create LoanBroker pointing to the vault - env(loanBroker::set(alice, vaultKeylet.key)); + env(loan_broker::set(alice, vaultKeylet.key)); env.close(); // Build the CoverDeposit STTx directly @@ -1256,7 +1256,7 @@ class LoanBroker_test : public beast::unit_test::Suite { testcase("Require Auth - Implicit Pseudo-account authorization"); using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer{"issuer"}; Account const alice{"alice"}; @@ -1364,7 +1364,7 @@ class LoanBroker_test : public beast::unit_test::Suite { testcase("testLoanBrokerSetDebtMaximum"); using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer{"issuer"}; Account const alice{"alice"}; Env env(*this); @@ -1550,10 +1550,10 @@ class LoanBroker_test : public beast::unit_test::Suite auto const brokerKeylet = keylet::loanBroker(broker, env.seq(broker)); - env(loanBroker::set(broker, keylet.key)); + env(loan_broker::set(broker, keylet.key)); env.close(); - env(loanBroker::coverDeposit(broker, brokerKeylet.key, deposit), Ter(err)); + env(loan_broker::coverDeposit(broker, brokerKeylet.key, deposit), Ter(err)); env.close(); }; @@ -1621,7 +1621,7 @@ class LoanBroker_test : public beast::unit_test::Suite auto const vaultPseudoAcct = Account("VaultPseudo", vaultPseudo); env(trust(issuer, vaultPseudoAcct["IOU"](0), tfSetFreeze)); - env(loanBroker::set(lender, vaultKeylet.key), Ter(tecFROZEN)); + env(loan_broker::set(lender, vaultKeylet.key), Ter(tecFROZEN)); } void @@ -1629,7 +1629,7 @@ class LoanBroker_test : public beast::unit_test::Suite { testcase << "LoanBrokerDelete - locked broker pseudo-account MPT"; using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer("issuer"); Account const alice("alice"); @@ -1749,7 +1749,7 @@ class LoanBroker_test : public beast::unit_test::Suite { testcase << "LoanBrokerDelete - frozen broker pseudo-account IOU"; using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer("issuer"); Account const alice("alice"); @@ -1833,7 +1833,7 @@ class LoanBroker_test : public beast::unit_test::Suite testCoverDepositFreezes() { using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer{"issuer"}; Account const alice{"alice"}; @@ -1984,7 +1984,7 @@ class LoanBroker_test : public beast::unit_test::Suite testcase("LoanBrokerCoverWithdraw IOU self-withdrawal while individually frozen"); using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer{"issuer"}; Account const alice{"alice"}; @@ -2046,7 +2046,7 @@ class LoanBroker_test : public beast::unit_test::Suite testCoverWithdrawFreezes() { using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer{"issuer"}; Account const alice{"alice"}; @@ -2351,7 +2351,7 @@ class LoanBroker_test : public beast::unit_test::Suite env.close(); env(vault.withdraw({.depositor = broker, .id = keylet.key, .amount = token(1'000)}), - loanBroker::kDestination(dest), + loan_broker::kDestination(dest), Ter(std::ignore)); BEAST_EXPECT(env.ter() == tecNO_LINE); env.close(); @@ -2361,14 +2361,14 @@ class LoanBroker_test : public beast::unit_test::Suite // Test LoanBroker withdraw auto const brokerKeylet = keylet::loanBroker(broker, env.seq(broker)); - env(loanBroker::set(broker, keylet.key)); + env(loan_broker::set(broker, keylet.key)); env.close(); - env(loanBroker::coverDeposit(broker, brokerKeylet.key, token(1'000))); + env(loan_broker::coverDeposit(broker, brokerKeylet.key, token(1'000))); env.close(); - env(loanBroker::coverWithdraw(broker, brokerKeylet.key, token(100)), - loanBroker::kDestination(dest), + env(loan_broker::coverWithdraw(broker, brokerKeylet.key, token(100)), + loan_broker::kDestination(dest), Ter(std::ignore)); BEAST_EXPECT(env.ter() == tecNO_LINE); env.close(); @@ -2379,8 +2379,8 @@ class LoanBroker_test : public beast::unit_test::Suite env(fclear(issuer, asfRequireAuth)); env.close(); - env(loanBroker::coverWithdraw(broker, brokerKeylet.key, token(100)), - loanBroker::kDestination(dest), + env(loan_broker::coverWithdraw(broker, brokerKeylet.key, token(100)), + loan_broker::kDestination(dest), Ter(std::ignore)); BEAST_EXPECT(env.ter() == tecNO_LINE); env.close(); @@ -2472,7 +2472,7 @@ class LoanBroker_test : public beast::unit_test::Suite env.close(); env(vault.withdraw({.depositor = broker, .id = keylet.key, .amount = token(1'000)}), - loanBroker::kDestination(dest), + loan_broker::kDestination(dest), Ter(std::ignore)); // Shouldn't fail if at MaximumAmount since no new tokens are issued @@ -2489,14 +2489,14 @@ class LoanBroker_test : public beast::unit_test::Suite // Test LoanBroker withdraw auto const brokerKeylet = keylet::loanBroker(broker, env.seq(broker)); - env(loanBroker::set(broker, keylet.key)); + env(loan_broker::set(broker, keylet.key)); env.close(); - env(loanBroker::coverDeposit(broker, brokerKeylet.key, token(1'000))); + env(loan_broker::coverDeposit(broker, brokerKeylet.key, token(1'000))); env.close(); - env(loanBroker::coverWithdraw(broker, brokerKeylet.key, token(100)), - loanBroker::kDestination(dest), + env(loan_broker::coverWithdraw(broker, brokerKeylet.key, token(100)), + loan_broker::kDestination(dest), Ter(std::ignore)); BEAST_EXPECT(env.ter() == err); env.close(); @@ -2522,7 +2522,7 @@ class LoanBroker_test : public beast::unit_test::Suite testCoverPrecisionGuard() { using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Account const issuer{"issuer"}; Account const alice{"alice"}; diff --git a/src/test/app/Loan_test.cpp b/src/test/app/Loan_test.cpp index 231a3b405a..977cdb443c 100644 --- a/src/test/app/Loan_test.cpp +++ b/src/test/app/Loan_test.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -92,8 +93,13 @@ class Loan_test : public beast::unit_test::Suite protected: // Ensure that all the features needed for Lending Protocol are included, // even if they are set to unsupported. - - FeatureBitset const all_{jtx::testableAmendments()}; + // + // featureLendingProtocolV1_1 is excluded from the default set: it changes + // Vault/LoanBroker accounting (AssetsTotal/DebtTotal/LossUnrealized), and + // most of this file's tests assert whole-life-specific expected values + // for those fields. Tests that specifically exercise the amendment opt + // it back in explicitly (e.g. `all_ | featureLendingProtocolV1_1`). + FeatureBitset const all_{jtx::testableAmendments() - featureLendingProtocolV1_1}; std::string const iouCurrency_{"IOU"}; void @@ -363,16 +369,21 @@ protected: { TenthBips16 const managementFeeRate{brokerSle->at(sfManagementFeeRate)}; auto const brokerDebt = brokerSle->at(sfDebtTotal); - auto const expectedDebt = principalOutstanding + interestOwed; - env.test.BEAST_EXPECT(brokerDebt == expectedDebt); - env.test.BEAST_EXPECT( - env.balance(pseudoAccount, broker.asset).number() == - brokerSle->at(sfCoverAvailable)); - env.test.BEAST_EXPECT(brokerSle->at(sfOwnerCount) == ownerCount); if (auto vaultSle = env.le(keylet::vault(brokerSle->at(sfVaultID))); env.test.BEAST_EXPECT(vaultSle)) { + auto const expectedDebt = + env.current()->rules().enabled(featureLendingProtocolV1_1) && + getVaultVersion(vaultSle) == VaultVersion::CashBasis + ? principalOutstanding + : principalOutstanding + interestOwed; + env.test.BEAST_EXPECT(brokerDebt == expectedDebt); + env.test.BEAST_EXPECT( + env.balance(pseudoAccount, broker.asset).number() == + brokerSle->at(sfCoverAvailable)); + env.test.BEAST_EXPECT(brokerSle->at(sfOwnerCount) == ownerCount); + Account const vaultPseudo{"vaultPseudoAccount", vaultSle->at(sfAccount)}; env.test.BEAST_EXPECT( vaultSle->at(sfAssetsAvailable) == @@ -468,7 +479,10 @@ protected: { env.test.BEAST_EXPECT( vaultSle->at(sfLossUnrealized) == - totalValue - managementFeeOutstanding); + (env.current()->rules().enabled(featureLendingProtocolV1_1) && + getVaultVersion(vaultSle) == VaultVersion::CashBasis + ? principalOutstanding + : totalValue - managementFeeOutstanding)); } else { @@ -531,7 +545,7 @@ protected: auto const keylet = keylet::loanBroker(lender.id(), env.seq(lender)); - using namespace loanBroker; + using namespace loan_broker; env(set(lender, vaultKeylet.key, params.flags), kData(params.data), kManagementFeeRate(params.managementFeeRate), @@ -635,8 +649,11 @@ protected: // log << vaultSle->getJson() << std::endl; auto const assetsUnavailable = vaultSle->at(sfAssetsTotal) - vaultSle->at(sfAssetsAvailable); - auto const unrealizedLoss = vaultSle->at(sfLossUnrealized) + state.totalValue - - state.managementFeeOutstanding; + auto const unrealizedLoss = vaultSle->at(sfLossUnrealized) + + (env.current()->rules().enabled(featureLendingProtocolV1_1) && + getVaultVersion(vaultSle) == VaultVersion::CashBasis + ? state.principalOutstanding + : state.totalValue - state.managementFeeOutstanding); if (!BEAST_EXPECT(unrealizedLoss <= assetsUnavailable)) { @@ -1516,7 +1533,7 @@ protected: auto const borrowerStartingBalance = env.balance(borrower, broker.asset); // Try to delete the loan broker with an active loan - env(loanBroker::del(lender, broker.brokerID), Ter(tecHAS_OBLIGATIONS)); + env(loan_broker::del(lender, broker.brokerID), Ter(tecHAS_OBLIGATIONS)); // Ensure the above tx doesn't get ordered after the LoanDelete and // delete our broker! env.close(); @@ -1591,7 +1608,7 @@ protected: int interestExponent) { using namespace jtx; - using namespace Lending; + using namespace lending; auto const& asset = broker.asset.raw(); auto const currencyLabel = getCurrencyLabel(asset); @@ -2055,7 +2072,7 @@ protected: Number const& startingCoverAvailable, Number const& amountToBeCovered) { coverAvailable(broker.brokerID, startingCoverAvailable - amountToBeCovered); - env(loanBroker::coverDeposit( + env(loan_broker::coverDeposit( brokerAcct, broker.brokerID, STAmount{broker.asset, amountToBeCovered})); coverAvailable(broker.brokerID, startingCoverAvailable); env.close(); @@ -3555,7 +3572,7 @@ protected: BEAST_EXPECT(brokerSle->at(sfDebtTotal) == 0); auto const coverAvailable = brokerSle->at(sfCoverAvailable); - env(loanBroker::coverWithdraw( + env(loan_broker::coverWithdraw( lender, broker.brokerID, STAmount(broker.asset, coverAvailable))); env.close(); @@ -3563,7 +3580,7 @@ protected: BEAST_EXPECT(brokerSle && brokerSle->at(sfCoverAvailable) == 0); } // Verify we can delete the loan broker - env(loanBroker::del(lender, broker.brokerID)); + env(loan_broker::del(lender, broker.brokerID)); env.close(); } } @@ -4642,7 +4659,7 @@ protected: using namespace jtx; using namespace std::chrono_literals; - using namespace Lending; + using namespace lending; Env env(*this, features); Account const issuer{"issuer"}; @@ -4801,7 +4818,7 @@ protected: using namespace jtx; using namespace std::chrono_literals; - using namespace Lending; + using namespace lending; Env env(*this, features); Account const issuer{"issuer"}; @@ -4942,7 +4959,7 @@ protected: using namespace jtx; using namespace std::chrono_literals; - using namespace Lending; + using namespace lending; Env env(*this, features); Account const issuer{"issuer"}; @@ -5043,7 +5060,7 @@ protected: using namespace jtx; using namespace std::chrono_literals; - using namespace Lending; + using namespace lending; Env env(*this, features); Account const issuer{"issuer"}; @@ -5067,7 +5084,7 @@ protected: BrokerInfo const broker{createVaultAndBroker(env, iouAsset, lender)}; { auto const coverDepositValue = broker.asset(broker.params.coverDeposit * 10).value(); - env(loanBroker::coverDeposit(lender, broker.brokerID, coverDepositValue)); + env(loan_broker::coverDeposit(lender, broker.brokerID, coverDepositValue)); env.close(); } @@ -5128,7 +5145,7 @@ protected: using namespace jtx; using namespace std::chrono_literals; - using namespace Lending; + using namespace lending; Env env{*this, features}; Account const issuer{"issuer"}; @@ -5433,7 +5450,7 @@ protected: testcase("Lending: CanTrade disabled has no impact"); using namespace jtx; using namespace loan; - using namespace loanBroker; + using namespace loan_broker; Env env(*this, all_); @@ -5654,7 +5671,7 @@ protected: using namespace jtx; using namespace loan; - using namespace loanBroker; + using namespace loan_broker; Env env(*this, features); @@ -6266,7 +6283,7 @@ protected: auto const brokerKeyLet = keylet::loanBroker(lender.id(), env.seq(lender)); - env(loanBroker::set(lender, vaultKeyLet.key), txFee); + env(loan_broker::set(lender, vaultKeyLet.key), txFee); env.close(); // BrokerInfo brokerInfo{xrpIssue(), keylet, vaultKeyLet, {}}; @@ -6300,7 +6317,7 @@ protected: testcase("Minimum cover rounding allows undercoverage (XRP)"); using namespace jtx; - using namespace loanBroker; + using namespace loan_broker; Env env{*this, features}; @@ -6471,7 +6488,7 @@ protected: auto const brokerKeylet = keylet::loanBroker(broker.id(), env.seq(broker)); - env(loanBroker::set(broker, vaultKeylet.key), txFee); + env(loan_broker::set(broker, vaultKeylet.key), txFee); env.close(); auto const serviceFee = 101; @@ -6750,7 +6767,7 @@ protected: // at least 1,000 cover. Default cover is 1,000, so we add more to be // safe. auto const additionalCover = iou(50'000).value(); - env(loanBroker::coverDeposit(broker, brokerInfo.brokerID, STAmount{iou, additionalCover})); + env(loan_broker::coverDeposit(broker, brokerInfo.brokerID, STAmount{iou, additionalCover})); env.close(); // Verify broker owner has a trustline auto const brokerTrustline = keylet::trustLine(broker, iou); @@ -6830,7 +6847,7 @@ protected: // at least 1,000 cover. Default cover is 1,000, so we add more to be // safe. auto const additionalCover = mpt(50'000).value(); - env(loanBroker::coverDeposit(broker, brokerInfo.brokerID, STAmount{mpt, additionalCover})); + env(loan_broker::coverDeposit(broker, brokerInfo.brokerID, STAmount{mpt, additionalCover})); env.close(); // Verify broker owner is authorized auto const brokerMpt = keylet::mptoken(mptt.issuanceID(), broker); @@ -6930,7 +6947,7 @@ protected: // at least 1,000 cover. Default cover is 1,000, so we add more to be // safe. auto const additionalCover = mpt(50'000).value(); - env(loanBroker::coverDeposit(broker, brokerInfo.brokerID, STAmount{mpt, additionalCover})); + env(loan_broker::coverDeposit(broker, brokerInfo.brokerID, STAmount{mpt, additionalCover})); env.close(); // Verify broker owner is authorized auto const brokerMpt = keylet::mptoken(mptt.issuanceID(), broker); @@ -7045,7 +7062,7 @@ protected: using namespace jtx; using namespace loan; - using namespace loanBroker; + using namespace loan_broker; Env env{*this, features}; @@ -7906,8 +7923,8 @@ protected: env.close(); auto const brokerKeylet = keylet::loanBroker(lender.id(), env.seq(lender)); - env(loanBroker::set(lender, vaultKeylet.key), - loanBroker::kDebtMaximum(Number{100}), + env(loan_broker::set(lender, vaultKeylet.key), + loan_broker::kDebtMaximum(Number{100}), Fee(env.current()->fees().base * 2)); env.close(); @@ -8151,7 +8168,7 @@ protected: { using namespace jtx; using namespace loan; - using namespace loanBroker; + using namespace loan_broker; bool const withAmendment = features[fixCleanup3_2_0]; @@ -8547,6 +8564,972 @@ protected: }); } + // LendingProtocolV1_1 ("cash-basis" accounting) dedicated coverage. + // + // Existing tests never enable featureLendingProtocolV1_1 (see `all_` + // above), so these are the only tests in this file that exercise the + // amendment. They are called once, directly, from + // runAmendmentIndependent() -- not looped through + // runAmendmentSensitive()/amendmentCombinations(), since doing so would + // require re-deriving whole-life-specific expected values for ~15 + // unrelated regression tests. + + // 1. LoanSet origination: Vault.AssetsTotal/LoanBroker.DebtTotal deltas, + // and the AssetsMaximum/DebtMaximum guards (which always check against + // principal + interestDue, regardless of the amendment). + void + testCashBasisLoanSetOrigination() + { + testcase("cash-basis: LoanSet origination"); + + using namespace jtx; + using namespace loan; + + PrettyAsset const xrpAsset{xrpIssue(), 1'000'000}; + BrokerParameters const brokerParams{ + .vaultDeposit = 100'000, + .debtMax = 0, + .coverRateMin = TenthBips32{0}, + .coverDeposit = 0, + .managementFeeRate = TenthBips16{0}, + .coverRateLiquidation = TenthBips32{0}}; + + Number const principalRequest{10'000}; + TenthBips32 const interestRate{percentageToTenthBips(10)}; + std::uint32_t const paymentTotal = 2; + std::uint32_t const paymentInterval = 86400; + + // Creates a broker/vault, submits a single LoanSet with a nonzero + // interest rate, and returns the observed Vault.AssetsTotal / + // LoanBroker.DebtTotal deltas plus the loan's own computed + // interestDue and principalOutstanding. + auto runOrigination = [&](FeatureBitset features) { + Env env(*this, features); + + Account const lender{"lender"}; + Account const borrower{"borrower"}; + env.fund(XRP(1'000'000), lender, borrower); + env.close(); + + BrokerInfo const broker{createVaultAndBroker(env, xrpAsset, lender, brokerParams)}; + + auto const vaultBefore = env.le(broker.vaultKeylet()); + auto const brokerBefore = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultBefore && brokerBefore); + Number const assetsTotalBefore = vaultBefore->at(sfAssetsTotal); + Number const debtTotalBefore = brokerBefore->at(sfDebtTotal); + + auto const loanSequence = brokerBefore->at(sfLoanSequence); + auto const loanKeylet = keylet::loan(broker.brokerID, loanSequence); + + env(set(borrower, broker.brokerID, xrpAsset(principalRequest).value()), + kCounterparty(lender), + kInterestRate(interestRate), + kPaymentTotal(paymentTotal), + kPaymentInterval(paymentInterval), + Sig(sfCounterpartySignature, lender), + Fee(env.current()->fees().base * 2), + Ter(tesSUCCESS)); + env.close(); + + auto const loanSle = env.le(loanKeylet); + BEAST_EXPECT(loanSle); + Number const principalOutstanding = loanSle->at(sfPrincipalOutstanding); + Number const totalValueOutstanding = loanSle->at(sfTotalValueOutstanding); + Number const interestDue = totalValueOutstanding - principalOutstanding; + BEAST_EXPECT(interestDue > beast::kZero); + BEAST_EXPECT(principalOutstanding == xrpAsset(principalRequest).value()); + + auto const vaultAfter = env.le(broker.vaultKeylet()); + auto const brokerAfter = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultAfter && brokerAfter); + Number const assetsTotalDelta = + Number(vaultAfter->at(sfAssetsTotal)) - assetsTotalBefore; + Number const debtTotalDelta = Number(brokerAfter->at(sfDebtTotal)) - debtTotalBefore; + + return std::make_tuple( + assetsTotalDelta, debtTotalDelta, interestDue, principalOutstanding); + }; + + Number interestDueCash{}; + Number principalOutstandingCash{}; + { + auto const [assetsTotalDelta, debtTotalDelta, interestDue, principalOutstanding] = + runOrigination(all_ | featureLendingProtocolV1_1); + interestDueCash = interestDue; + principalOutstandingCash = principalOutstanding; + + BEAST_EXPECTS( + assetsTotalDelta == beast::kZero, + "cash-basis origination must not change AssetsTotal; delta=" + + to_string(assetsTotalDelta)); + BEAST_EXPECTS( + debtTotalDelta == principalOutstanding, + "cash-basis origination must add principal-only to DebtTotal; delta=" + + to_string(debtTotalDelta) + " principal=" + to_string(principalOutstanding)); + } + + { + auto const [assetsTotalDelta, debtTotalDelta, interestDue, principalOutstanding] = + runOrigination(all_); + + BEAST_EXPECTS( + assetsTotalDelta == interestDue, + "whole-life origination must add interestDue to AssetsTotal; delta=" + + to_string(assetsTotalDelta) + " interestDue=" + to_string(interestDue)); + BEAST_EXPECTS( + debtTotalDelta == principalOutstanding + interestDue, + "whole-life origination must add principal+interest to DebtTotal; delta=" + + to_string(debtTotalDelta)); + } + + // AssetsMaximum guard checks interestDue headroom only under + // whole-life accounting; DebtMaximum guard also varies by model. + auto runVaultGuard = [&](FeatureBitset features, Number const& slack, TER expected) { + Env env(*this, features); + + Account const lender{"lender"}; + Account const borrower{"borrower"}; + env.fund(XRP(1'000'000), lender, borrower); + env.close(); + + BrokerInfo const broker{createVaultAndBroker(env, xrpAsset, lender, brokerParams)}; + + auto const vaultSle = env.le(broker.vaultKeylet()); + BEAST_EXPECT(vaultSle); + Number const assetsTotalBefore = vaultSle->at(sfAssetsTotal); + + Vault const vault{env}; + auto tx = vault.set({.owner = lender, .id = broker.vaultID}); + tx[sfAssetsMaximum] = assetsTotalBefore + slack; + env(tx); + env.close(); + + env(set(borrower, broker.brokerID, xrpAsset(principalRequest).value()), + kCounterparty(lender), + kInterestRate(interestRate), + kPaymentTotal(paymentTotal), + kPaymentInterval(paymentInterval), + Sig(sfCounterpartySignature, lender), + Fee(env.current()->fees().base * 2), + Ter(expected)); + env.close(); + }; + + auto runBrokerGuard = [&](FeatureBitset features, Number const& debtMaximum, TER expected) { + Env env(*this, features); + + Account const lender{"lender"}; + Account const borrower{"borrower"}; + env.fund(XRP(1'000'000), lender, borrower); + env.close(); + + BrokerInfo const broker{createVaultAndBroker(env, xrpAsset, lender, brokerParams)}; + + env(loan_broker::set(lender, broker.vaultID), + loan_broker::kLoanBrokerId(broker.brokerID), + loan_broker::kDebtMaximum(debtMaximum), + Fee(env.current()->fees().base * 2)); + env.close(); + + env(set(borrower, broker.brokerID, xrpAsset(principalRequest).value()), + kCounterparty(lender), + kInterestRate(interestRate), + kPaymentTotal(paymentTotal), + kPaymentInterval(paymentInterval), + Sig(sfCounterpartySignature, lender), + Fee(env.current()->fees().base * 2), + Ter(expected)); + env.close(); + }; + + Number const oneDrop = xrpAsset(1).value(); + { + testcase("whole-life: LoanSet AssetsMaximum guard checks interestDue headroom"); + // Guard rejects when there's not quite enough headroom for the + // interest. + runVaultGuard(all_, interestDueCash - oneDrop, tecLIMIT_EXCEEDED); + // Guard accepts at the exact boundary. + runVaultGuard(all_, interestDueCash, tesSUCCESS); + } + + { + testcase("cash-basis: LoanSet AssetsMaximum guard ignores interestDue headroom"); + // Even far less headroom than interestDue still succeeds, since + // cash-basis origination never adds interest to AssetsTotal. + runVaultGuard(all_ | featureLendingProtocolV1_1, oneDrop, tesSUCCESS); + } + + // DebtMaximum guard: cash-basis projects principal-only DebtTotal; + // whole-life projects principal + interestDue. + for (auto const cashBasis : {true, false}) + { + testcase( + std::string("LoanSet DebtMaximum guard (") + + (cashBasis ? "cash-basis)" : "whole-life)")); + auto const features = cashBasis ? all_ | featureLendingProtocolV1_1 : all_; + Number const newDebtTotal = + principalOutstandingCash + (cashBasis ? Number{} : interestDueCash); + runBrokerGuard(features, newDebtTotal - oneDrop, tecLIMIT_EXCEEDED); + runBrokerGuard(features, newDebtTotal, tesSUCCESS); + } + } + + // 2. LoanPay: regular, late, overpayment, and full-payment types. + // Assert Vault.AssetsTotal/LoanBroker.DebtTotal deltas match + // interestPaid/principalPaid under cash-basis, and cross-check the + // amendment-disabled run's deltas against the documented whole-life + // formula (AssetsTotal += valueChange; DebtTotal mirrors the loan's own + // TotalValueOutstanding delta exactly, since whole-life debt recognition + // tracks total loan value). + void + testCashBasisLoanPay() + { + using namespace jtx; + using namespace loan; + using namespace std::chrono_literals; + using tp = NetClock::time_point; + + PrettyAsset const xrpAsset{xrpIssue(), 1'000'000}; + BrokerParameters const brokerParams{ + .vaultDeposit = 1'000'000, + .debtMax = 0, + .coverRateMin = TenthBips32{0}, + .coverDeposit = 0, + .managementFeeRate = TenthBips16{0}, + .coverRateLiquidation = TenthBips32{0}}; + + Number const principalRequest{12'000}; + TenthBips32 const interestRate{percentageToTenthBips(12)}; + std::uint32_t const paymentTotal = 4; + std::uint32_t const paymentInterval = 600; + std::uint32_t const gracePeriod = 300; + + struct PaymentDeltas + { + Number principalPaid; + Number assetsTotalDelta; + Number debtTotalDelta; + Number totalValueDelta; + }; + + // Sets up a fresh broker + loan, advances time, submits a single + // payment of the given type/amount, and returns the observed deltas. + auto runPayment = [&](FeatureBitset features, + std::uint32_t loanSetFlags, + std::uint32_t payFlags, + std::function const& advanceTime, + std::function const& paymentAmount) { + Env env(*this, features); + + Account const lender{"lender"}; + Account const borrower{"borrower"}; + env.fund(XRP(10'000'000), lender, borrower); + env.close(); + + BrokerInfo const broker{createVaultAndBroker(env, xrpAsset, lender, brokerParams)}; + + LoanParameters const loanParams{ + .account = borrower, + .counter = lender, + .principalRequest = principalRequest, + .interest = interestRate, + .payTotal = paymentTotal, + .payInterval = paymentInterval, + .gracePd = gracePeriod, + .flags = loanSetFlags, + }; + + auto const brokerBeforeLoan = env.le(broker.brokerKeylet()); + BEAST_EXPECT(brokerBeforeLoan); + auto const loanSequence = brokerBeforeLoan->at(sfLoanSequence); + auto const loanKeylet = keylet::loan(broker.brokerID, loanSequence); + + env(loanParams(env, broker)); + env.close(); + + LoanState const state = getCurrentState(env, broker, loanKeylet); + + advanceTime(env, state.startDate); + + auto const vaultBefore = env.le(broker.vaultKeylet()); + auto const brokerBefore = env.le(broker.brokerKeylet()); + auto const loanBefore = env.le(loanKeylet); + BEAST_EXPECT(vaultBefore && brokerBefore && loanBefore); + + Number const principalBefore = loanBefore->at(sfPrincipalOutstanding); + Number const totalValueBefore = loanBefore->at(sfTotalValueOutstanding); + Number const assetsTotalBefore = vaultBefore->at(sfAssetsTotal); + Number const debtTotalBefore = brokerBefore->at(sfDebtTotal); + + STAmount const amount = paymentAmount(state); + env(pay(borrower, loanKeylet.key, amount, payFlags), Ter(tesSUCCESS)); + env.close(); + + auto const vaultAfter = env.le(broker.vaultKeylet()); + auto const brokerAfter = env.le(broker.brokerKeylet()); + auto const loanAfter = env.le(loanKeylet); + BEAST_EXPECT(vaultAfter && brokerAfter && loanAfter); + + Number const principalAfter = loanAfter->at(sfPrincipalOutstanding); + Number const totalValueAfter = loanAfter->at(sfTotalValueOutstanding); + Number const assetsTotalAfter = vaultAfter->at(sfAssetsTotal); + Number const debtTotalAfter = brokerAfter->at(sfDebtTotal); + + return PaymentDeltas{ + .principalPaid = principalBefore - principalAfter, + .assetsTotalDelta = assetsTotalAfter - assetsTotalBefore, + .debtTotalDelta = debtTotalAfter - debtTotalBefore, + .totalValueDelta = totalValueAfter - totalValueBefore}; + }; + + // Compares the disabled (whole-life) and enabled (cash-basis) runs + // of the same payment scenario, and asserts the documented + // relationships between them. + auto checkScenario = [&](std::string const& label, + PaymentDeltas const& off, + PaymentDeltas const& on) { + testcase("cash-basis: LoanPay " + label); + + // The loan's own PrincipalOutstanding field is untouched by + // the amendment. + BEAST_EXPECTS( + off.principalPaid == on.principalPaid, + "principalPaid must be amendment-independent; off=" + to_string(off.principalPaid) + + " on=" + to_string(on.principalPaid)); + + // Whole-life structural invariant: DebtTotal (which + // recognizes a loan's full remaining value as debt) must + // change exactly as the loan's own TotalValueOutstanding + // does. + BEAST_EXPECTS( + off.debtTotalDelta == off.totalValueDelta, + "whole-life DebtTotal delta must mirror TotalValueOutstanding delta; " + "debtTotalDelta=" + + to_string(off.debtTotalDelta) + + " totalValueDelta=" + to_string(off.totalValueDelta)); + + // Derive interestPaid from the whole-life run's independent + // ledger deltas: + // assetsTotalDelta_off == valueChange + // debtTotalDelta_off == valueChange - (principalPaid + interestPaid) + // => interestPaid == assetsTotalDelta_off - debtTotalDelta_off - principalPaid + Number const interestPaid = + off.assetsTotalDelta - off.debtTotalDelta - off.principalPaid; + BEAST_EXPECTS( + interestPaid >= beast::kZero, + "derived interestPaid must be non-negative: " + to_string(interestPaid)); + + BEAST_EXPECTS( + on.assetsTotalDelta == interestPaid, + "cash-basis AssetsTotal delta must equal interestPaid; delta=" + + to_string(on.assetsTotalDelta) + " interestPaid=" + to_string(interestPaid)); + BEAST_EXPECTS( + on.debtTotalDelta == -on.principalPaid, + "cash-basis DebtTotal delta must equal -principalPaid; delta=" + + to_string(on.debtTotalDelta) + " principalPaid=" + to_string(on.principalPaid)); + }; + + // ---- Regular, on-time payment ---- + { + auto const noAdvance = [](Env& env, tp const&) { env.close(); }; + auto const regularAmount = [&](LoanState const& state) { + return STAmount{ + xrpAsset, + roundPeriodicPayment(xrpAsset, state.periodicPayment, state.loanScale) * + Number{3, -1} * 5}; // 1.5x, so only a single period is paid + }; + + auto const off = runPayment(all_, 0, 0, noAdvance, regularAmount); + auto const on = + runPayment(all_ | featureLendingProtocolV1_1, 0, 0, noAdvance, regularAmount); + + // Regular, on-time payments never change the loan's value beyond + // normal amortization (production asserts valueChange == 0), so + // AssetsTotal must be unaffected in the whole-life run. + BEAST_EXPECTS( + off.assetsTotalDelta == beast::kZero, + "regular on-time payment must not change AssetsTotal under whole-life; delta=" + + to_string(off.assetsTotalDelta)); + + checkScenario("regular payment", off, on); + } + + // ---- Late payment ---- + { + auto const advancePastDue = [&](Env& env, tp const& startDate) { + env.close(startDate + std::chrono::seconds(paymentInterval + 1)); + }; + auto const lateAmount = [&](LoanState const& state) { + return STAmount{ + xrpAsset, + roundPeriodicPayment(xrpAsset, state.periodicPayment, state.loanScale) * + Number{3}}; // generous; excess is not withdrawn + }; + + auto const off = runPayment(all_, 0, tfLoanLatePayment, advancePastDue, lateAmount); + auto const on = runPayment( + all_ | featureLendingProtocolV1_1, + 0, + tfLoanLatePayment, + advancePastDue, + lateAmount); + + checkScenario("late payment", off, on); + } + + // ---- Overpayment ---- + { + auto const noAdvance = [](Env& env, tp const&) { env.close(); }; + auto const overpayAmount = [&](LoanState const& state) { + // One regular period, plus a generous extra principal + // paydown. + return STAmount{ + xrpAsset, + roundPeriodicPayment(xrpAsset, state.periodicPayment, state.loanScale) + + xrpAsset(2'000).value()}; + }; + + auto const off = + runPayment(all_, tfLoanOverpayment, tfLoanOverpayment, noAdvance, overpayAmount); + auto const on = runPayment( + all_ | featureLendingProtocolV1_1, + tfLoanOverpayment, + tfLoanOverpayment, + noAdvance, + overpayAmount); + + checkScenario("overpayment", off, on); + } + + // ---- Full payment ---- + { + auto const noAdvance = [](Env& env, tp const&) { env.close(); }; + auto const fullAmount = [&](LoanState const&) { + // Generously large: full payment only ever consumes exactly + // what's due (principal + accrued interest; close fee/ + // prepayment penalty are 0 here), excess is not withdrawn. + return STAmount{xrpAsset, xrpAsset(principalRequest).value() * Number{2}}; + }; + + auto const off = runPayment(all_, 0, tfLoanFullPayment, noAdvance, fullAmount); + auto const on = runPayment( + all_ | featureLendingProtocolV1_1, 0, tfLoanFullPayment, noAdvance, fullAmount); + + checkScenario("full payment", off, on); + } + } + + // 3. LoanManage: impair, unimpair, and default. + void + testCashBasisLoanManage() + { + using namespace jtx; + using namespace loan; + using namespace std::chrono_literals; + + PrettyAsset const xrpAsset{xrpIssue(), 1'000'000}; + BrokerParameters const brokerParams{ + .vaultDeposit = 1'000'000, + .debtMax = 0, + .coverRateMin = TenthBips32{percentageToTenthBips(10)}, + .coverDeposit = 5'000, + .managementFeeRate = TenthBips16{0}, + .coverRateLiquidation = TenthBips32{percentageToTenthBips(25)}}; + + Number const principalRequest{10'000}; + TenthBips32 const interestRate{percentageToTenthBips(12)}; + std::uint32_t const paymentTotal = 4; + std::uint32_t const paymentInterval = 600; + std::uint32_t const gracePeriod = 60; + + auto setupLoan = [&](Env& env) { + Account const lender{"lender"}; + Account const borrower{"borrower"}; + env.fund(XRP(10'000'000), lender, borrower); + env.close(); + + BrokerInfo const broker{createVaultAndBroker(env, xrpAsset, lender, brokerParams)}; + + LoanParameters const loanParams{ + .account = borrower, + .counter = lender, + .principalRequest = principalRequest, + .interest = interestRate, + .payTotal = paymentTotal, + .payInterval = paymentInterval, + .gracePd = gracePeriod, + }; + + auto const brokerBeforeLoan = env.le(broker.brokerKeylet()); + BEAST_EXPECT(brokerBeforeLoan); + auto const loanSequence = brokerBeforeLoan->at(sfLoanSequence); + auto const loanKeylet = keylet::loan(broker.brokerID, loanSequence); + + env(loanParams(env, broker)); + env.close(); + + return std::make_tuple(broker, loanKeylet, lender, borrower); + }; + + // ---- impair / unimpair ---- + auto runImpairUnimpair = [&](FeatureBitset features) { + Env env(*this, features); + auto const [broker, loanKeylet, lender, borrower] = setupLoan(env); + + auto const loanBefore = env.le(loanKeylet); + BEAST_EXPECT(loanBefore); + Number const principalOutstanding = loanBefore->at(sfPrincipalOutstanding); + Number const totalValueOutstanding = loanBefore->at(sfTotalValueOutstanding); + Number const managementFeeOutstanding = loanBefore->at(sfManagementFeeOutstanding); + + Number const expectedExposure = + env.current()->rules().enabled(featureLendingProtocolV1_1) + ? principalOutstanding + : totalValueOutstanding - managementFeeOutstanding; + + auto const vaultBeforeImpair = env.le(broker.vaultKeylet()); + BEAST_EXPECT(vaultBeforeImpair); + Number const lossBefore = vaultBeforeImpair->at(sfLossUnrealized); + + env(manage(lender, loanKeylet.key, tfLoanImpair), Ter(tesSUCCESS)); + env.close(); + + auto const vaultAfterImpair = env.le(broker.vaultKeylet()); + BEAST_EXPECT(vaultAfterImpair); + Number const impairDelta = Number(vaultAfterImpair->at(sfLossUnrealized)) - lossBefore; + + env(manage(lender, loanKeylet.key, tfLoanUnimpair), Ter(tesSUCCESS)); + env.close(); + + auto const vaultAfterUnimpair = env.le(broker.vaultKeylet()); + BEAST_EXPECT(vaultAfterUnimpair); + Number const netDelta = Number(vaultAfterUnimpair->at(sfLossUnrealized)) - lossBefore; + + return std::make_tuple(expectedExposure, impairDelta, netDelta); + }; + + for (auto const features : {all_ | featureLendingProtocolV1_1, all_}) + { + testcase( + std::string("cash-basis: LoanManage impair/unimpair (") + + (features[featureLendingProtocolV1_1] ? "enabled)" : "disabled)")); + auto const [expectedExposure, impairDelta, netDelta] = runImpairUnimpair(features); + + BEAST_EXPECTS( + impairDelta == expectedExposure, + "impair must add loanVaultExposure to LossUnrealized; delta=" + + to_string(impairDelta) + " expected=" + to_string(expectedExposure)); + BEAST_EXPECTS( + netDelta == beast::kZero, + "unimpair must be an exact reversal of impair; net=" + to_string(netDelta)); + } + + // ---- impair, then default ---- + auto runDefault = [&](FeatureBitset features) { + Env env(*this, features); + auto const [broker, loanKeylet, lender, borrower] = setupLoan(env); + + auto const loanBeforeImpair = env.le(loanKeylet); + BEAST_EXPECT(loanBeforeImpair); + Number const principalOutstanding = loanBeforeImpair->at(sfPrincipalOutstanding); + Number const totalValueOutstanding = loanBeforeImpair->at(sfTotalValueOutstanding); + Number const managementFeeOutstanding = + loanBeforeImpair->at(sfManagementFeeOutstanding); + + Number const expectedExposure = + env.current()->rules().enabled(featureLendingProtocolV1_1) + ? principalOutstanding + : totalValueOutstanding - managementFeeOutstanding; + + env(manage(lender, loanKeylet.key, tfLoanImpair), Ter(tesSUCCESS)); + env.close(); + + LoanState const state = getCurrentState(env, broker, loanKeylet); + env.close( + state.startDate + std::chrono::seconds(paymentInterval) + + std::chrono::seconds(gracePeriod) + 60s); + + auto const vaultBefore = env.le(broker.vaultKeylet()); + auto const brokerBefore = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultBefore && brokerBefore); + Number const assetsTotalBefore = vaultBefore->at(sfAssetsTotal); + Number const debtTotalBefore = brokerBefore->at(sfDebtTotal); + Number const lossBefore = vaultBefore->at(sfLossUnrealized); + Number const coverAvailableBefore = brokerBefore->at(sfCoverAvailable); + + env(manage(lender, loanKeylet.key, tfLoanDefault), Ter(tesSUCCESS)); + env.close(); + + auto const vaultAfter = env.le(broker.vaultKeylet()); + auto const brokerAfter = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultAfter && brokerAfter); + Number const assetsTotalDelta = + Number(vaultAfter->at(sfAssetsTotal)) - assetsTotalBefore; + Number const debtTotalDelta = Number(brokerAfter->at(sfDebtTotal)) - debtTotalBefore; + Number const lossDelta = Number(vaultAfter->at(sfLossUnrealized)) - lossBefore; + Number const coverAvailableDelta = + Number(brokerAfter->at(sfCoverAvailable)) - coverAvailableBefore; + + Number const defaultCovered = -coverAvailableDelta; + Number const vaultDefaultAmount = expectedExposure - defaultCovered; + + return std::make_tuple( + expectedExposure, assetsTotalDelta, debtTotalDelta, lossDelta, vaultDefaultAmount); + }; + + for (auto const features : {all_ | featureLendingProtocolV1_1, all_}) + { + testcase( + std::string("cash-basis: LoanManage default (") + + (features[featureLendingProtocolV1_1] ? "enabled)" : "disabled)")); + auto const + [expectedExposure, + assetsTotalDelta, + debtTotalDelta, + lossDelta, + vaultDefaultAmount] = runDefault(features); + + BEAST_EXPECTS( + debtTotalDelta == -expectedExposure, + "default must reduce DebtTotal by the unified default amount; delta=" + + to_string(debtTotalDelta) + " expected=" + to_string(expectedExposure)); + BEAST_EXPECTS( + lossDelta == -expectedExposure, + "default must reverse the earlier impair's LossUnrealized exactly; delta=" + + to_string(lossDelta) + " expected=" + to_string(expectedExposure)); + BEAST_EXPECTS( + assetsTotalDelta == -vaultDefaultAmount, + "default must reduce AssetsTotal by (defaultAmount - defaultCovered); delta=" + + to_string(assetsTotalDelta) + " expected=" + to_string(-vaultDefaultAmount)); + } + } + + // 3b. LEVersion regression: a Vault created before featureLendingProtocolV1_1 + // activates (LEVersion absent) must keep whole-life (accrual) accounting + // forever, even after the amendment is later enabled -- the switch is + // per-Vault (LEVersion == VaultVersion::CashBasis), not a single global amendment + // flag. + void + testLegacyVaultKeepsAccrualAfterAmendmentEnabled() + { + testcase("LEVersion: legacy vault keeps accrual after amendment enabled"); + + using namespace jtx; + using namespace loan; + using namespace std::chrono_literals; + + PrettyAsset const xrpAsset{xrpIssue(), 1'000'000}; + BrokerParameters const brokerParams{ + .vaultDeposit = 1'000'000, + .debtMax = 0, + .coverRateMin = TenthBips32{percentageToTenthBips(10)}, + .coverDeposit = 5'000, + .managementFeeRate = TenthBips16{0}, + .coverRateLiquidation = TenthBips32{percentageToTenthBips(25)}}; + + Number const principalRequest{10'000}; + TenthBips32 const interestRate{percentageToTenthBips(12)}; + std::uint32_t const paymentTotal = 4; + std::uint32_t const paymentInterval = 600; + std::uint32_t const gracePeriod = 60; + + // Amendment disabled at Vault creation time: LEVersion stays absent. + Env env(*this, all_); + + Account const lender{"lender"}; + Account const borrower{"borrower"}; + env.fund(XRP(10'000'000), lender, borrower); + env.close(); + + BrokerInfo const broker{createVaultAndBroker(env, xrpAsset, lender, brokerParams)}; + + { + auto const vaultSle = env.le(broker.vaultKeylet()); + BEAST_EXPECT(vaultSle); + BEAST_EXPECT(!vaultSle->isFieldPresent(sfLEVersion)); + } + + // Now enable the amendment -- production dispatch must still treat + // this specific Vault as accrual-basis, since its LEVersion is + // (and remains) absent. + env.enableFeature(featureLendingProtocolV1_1); + env.close(); + + LoanParameters const loanParams{ + .account = borrower, + .counter = lender, + .principalRequest = principalRequest, + .interest = interestRate, + .payTotal = paymentTotal, + .payInterval = paymentInterval, + .gracePd = gracePeriod, + }; + + auto const brokerBeforeLoan = env.le(broker.brokerKeylet()); + BEAST_EXPECT(brokerBeforeLoan); + auto const loanSequence = brokerBeforeLoan->at(sfLoanSequence); + auto const loanKeylet = keylet::loan(broker.brokerID, loanSequence); + + // ---- LoanSet origination: whole-life formulas expected ---- + auto const vaultBeforeSet = env.le(broker.vaultKeylet()); + auto const brokerBeforeSet = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultBeforeSet && brokerBeforeSet); + Number const assetsTotalBeforeSet = vaultBeforeSet->at(sfAssetsTotal); + Number const debtTotalBeforeSet = brokerBeforeSet->at(sfDebtTotal); + + env(loanParams(env, broker)); + env.close(); + + auto const loanAfterSet = env.le(loanKeylet); + BEAST_EXPECT(loanAfterSet); + Number const principalOutstanding = loanAfterSet->at(sfPrincipalOutstanding); + Number const totalValueOutstanding = loanAfterSet->at(sfTotalValueOutstanding); + Number const interestDue = totalValueOutstanding - principalOutstanding; + BEAST_EXPECT(interestDue > beast::kZero); + + auto const vaultAfterSet = env.le(broker.vaultKeylet()); + auto const brokerAfterSet = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultAfterSet && brokerAfterSet); + Number const assetsTotalDeltaSet = + Number(vaultAfterSet->at(sfAssetsTotal)) - assetsTotalBeforeSet; + Number const debtTotalDeltaSet = + Number(brokerAfterSet->at(sfDebtTotal)) - debtTotalBeforeSet; + + BEAST_EXPECTS( + assetsTotalDeltaSet == interestDue, + "legacy vault origination must still add interestDue to AssetsTotal; delta=" + + to_string(assetsTotalDeltaSet) + " interestDue=" + to_string(interestDue)); + BEAST_EXPECTS( + debtTotalDeltaSet == principalOutstanding + interestDue, + "legacy vault origination must still add principal+interest to DebtTotal; delta=" + + to_string(debtTotalDeltaSet)); + + LoanState const state = getCurrentState(env, broker, loanKeylet); + env.close(); + + // ---- LoanPay: whole-life formulas expected ---- + auto const vaultBeforePay = env.le(broker.vaultKeylet()); + auto const brokerBeforePay = env.le(broker.brokerKeylet()); + auto const loanBeforePay = env.le(loanKeylet); + BEAST_EXPECT(vaultBeforePay && brokerBeforePay && loanBeforePay); + Number const totalValueBeforePay = loanBeforePay->at(sfTotalValueOutstanding); + Number const assetsTotalBeforePay = vaultBeforePay->at(sfAssetsTotal); + Number const debtTotalBeforePay = brokerBeforePay->at(sfDebtTotal); + + STAmount const paymentAmount{ + xrpAsset, roundPeriodicPayment(xrpAsset, state.periodicPayment, state.loanScale)}; + env(pay(borrower, loanKeylet.key, paymentAmount), Ter(tesSUCCESS)); + env.close(); + + auto const vaultAfterPay = env.le(broker.vaultKeylet()); + auto const brokerAfterPay = env.le(broker.brokerKeylet()); + auto const loanAfterPay = env.le(loanKeylet); + BEAST_EXPECT(vaultAfterPay && brokerAfterPay && loanAfterPay); + Number const totalValueAfterPay = loanAfterPay->at(sfTotalValueOutstanding); + Number const assetsTotalDeltaPay = + Number(vaultAfterPay->at(sfAssetsTotal)) - assetsTotalBeforePay; + Number const debtTotalDeltaPay = + Number(brokerAfterPay->at(sfDebtTotal)) - debtTotalBeforePay; + Number const totalValueDeltaPay = totalValueAfterPay - totalValueBeforePay; + + // A regular, on-time payment has valueChange == 0, so whole-life + // AssetsTotal is untouched and DebtTotal mirrors TotalValueOutstanding. + BEAST_EXPECTS( + assetsTotalDeltaPay == beast::kZero, + "legacy vault regular payment must not change AssetsTotal; delta=" + + to_string(assetsTotalDeltaPay)); + BEAST_EXPECTS( + debtTotalDeltaPay == totalValueDeltaPay, + "legacy vault DebtTotal delta must mirror TotalValueOutstanding delta; " + "debtTotalDelta=" + + to_string(debtTotalDeltaPay) + " totalValueDelta=" + to_string(totalValueDeltaPay)); + + // ---- LoanManage: impair, then default -- whole-life exposure expected ---- + auto const loanBeforeImpair = env.le(loanKeylet); + BEAST_EXPECT(loanBeforeImpair); + Number const totalValueBeforeImpair = loanBeforeImpair->at(sfTotalValueOutstanding); + Number const managementFeeBeforeImpair = loanBeforeImpair->at(sfManagementFeeOutstanding); + Number const expectedExposure = totalValueBeforeImpair - managementFeeBeforeImpair; + + env(manage(lender, loanKeylet.key, tfLoanImpair), Ter(tesSUCCESS)); + env.close(); + + LoanState const stateAtImpair = getCurrentState(env, broker, loanKeylet); + env.close( + stateAtImpair.startDate + std::chrono::seconds(paymentInterval) + + std::chrono::seconds(gracePeriod) + 60s); + + auto const vaultBeforeDefault = env.le(broker.vaultKeylet()); + auto const brokerBeforeDefault = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultBeforeDefault && brokerBeforeDefault); + Number const debtTotalBeforeDefault = brokerBeforeDefault->at(sfDebtTotal); + Number const lossBeforeDefault = vaultBeforeDefault->at(sfLossUnrealized); + + env(manage(lender, loanKeylet.key, tfLoanDefault), Ter(tesSUCCESS)); + env.close(); + + auto const vaultAfterDefault = env.le(broker.vaultKeylet()); + auto const brokerAfterDefault = env.le(broker.brokerKeylet()); + BEAST_EXPECT(vaultAfterDefault && brokerAfterDefault); + Number const debtTotalDeltaDefault = + Number(brokerAfterDefault->at(sfDebtTotal)) - debtTotalBeforeDefault; + Number const lossDeltaDefault = + Number(vaultAfterDefault->at(sfLossUnrealized)) - lossBeforeDefault; + + BEAST_EXPECTS( + debtTotalDeltaDefault == -expectedExposure, + "legacy vault default must reduce DebtTotal by whole-life exposure; delta=" + + to_string(debtTotalDeltaDefault) + " expected=" + to_string(expectedExposure)); + BEAST_EXPECTS( + lossDeltaDefault == -expectedExposure, + "legacy vault default must reverse the earlier impair's LossUnrealized exactly; " + "delta=" + + to_string(lossDeltaDefault) + " expected=" + to_string(expectedExposure)); + + // Confirm the Vault's LEVersion truly never got set, throughout. + { + auto const vaultSle = env.le(broker.vaultKeylet()); + BEAST_EXPECT(vaultSle); + BEAST_EXPECT(!vaultSle->isFieldPresent(sfLEVersion)); + BEAST_EXPECT(getVaultVersion(vaultSle) == VaultVersion::Legacy); + } + } + + // 4. End-to-end trajectory: LoanSet -> 2 LoanPays -> LoanManage(default), + // entirely under the amendment, with independently hand-computed + // expected AssetsTotal/DebtTotal/LossUnrealized/CoverAvailable values at + // each step. 0% interest keeps the arithmetic exact and tractable; the + // divergence from whole-life accounting is already covered directly by + // testCashBasisLoanSetOrigination/LoanPay/LoanManage above, so this test + // focuses purely on an independent, from-scratch trajectory check. + void + testCashBasisEndToEndTrajectory() + { + testcase("cash-basis: end-to-end trajectory"); + + using namespace jtx; + using namespace loan; + using namespace std::chrono_literals; + + PrettyAsset const xrpAsset{xrpIssue(), 1'000'000}; + BrokerParameters const brokerParams{ + .vaultDeposit = 100'000, .managementFeeRate = TenthBips16{0}}; + + Env env(*this, all_ | featureLendingProtocolV1_1); + + Account const lender{"lender"}; + Account const borrower{"borrower"}; + env.fund(XRP(10'000'000), lender, borrower); + env.close(); + + BrokerInfo const broker{createVaultAndBroker(env, xrpAsset, lender, brokerParams)}; + + // Hand computation (all values in XRP, drops == 1e-6 XRP): + // Vault: AssetsTotal starts at 100'000 (the deposit). + // Broker: DebtTotal starts at 0, CoverAvailable starts at 1'000 + // (BrokerParameters::defaults().coverDeposit). + auto const vaultKeylet = broker.vaultKeylet(); + auto const brokerKeylet = broker.brokerKeylet(); + + // All the "human XRP unit" constants below (e.g. `100'000`) are + // converted to raw native (drops) values via xrpAsset(...), since + // that's how the ledger fields are actually denominated. + auto const checkVaultBroker = [&](Number const& assetsTotalUnits, + Number const& debtTotalUnits, + Number const& lossUnrealizedUnits, + Number const& coverAvailableUnits, + char const* step) { + Number const assetsTotal = xrpAsset(assetsTotalUnits).value(); + Number const debtTotal = xrpAsset(debtTotalUnits).value(); + Number const lossUnrealized = xrpAsset(lossUnrealizedUnits).value(); + Number const coverAvailable = xrpAsset(coverAvailableUnits).value(); + + auto const vaultSle = env.le(vaultKeylet); + auto const brokerSle = env.le(brokerKeylet); + BEAST_EXPECT(vaultSle && brokerSle); + BEAST_EXPECTS( + vaultSle->at(sfAssetsTotal) == assetsTotal, + std::string(step) + ": AssetsTotal expected " + to_string(assetsTotal) + " got " + + to_string(Number(vaultSle->at(sfAssetsTotal)))); + BEAST_EXPECTS( + brokerSle->at(sfDebtTotal) == debtTotal, + std::string(step) + ": DebtTotal expected " + to_string(debtTotal) + " got " + + to_string(Number(brokerSle->at(sfDebtTotal)))); + BEAST_EXPECTS( + vaultSle->at(sfLossUnrealized) == lossUnrealized, + std::string(step) + ": LossUnrealized expected " + to_string(lossUnrealized) + + " got " + to_string(Number(vaultSle->at(sfLossUnrealized)))); + BEAST_EXPECTS( + brokerSle->at(sfCoverAvailable) == coverAvailable, + std::string(step) + ": CoverAvailable expected " + to_string(coverAvailable) + + " got " + to_string(Number(brokerSle->at(sfCoverAvailable)))); + }; + + checkVaultBroker(100'000, 0, 0, 1'000, "before LoanSet"); + + // Loan: principal=1200, 0% interest, 12 payments of 100 each, no fees. + Number const principalRequest{1'200}; + std::uint32_t const paymentTotal = 12; + std::uint32_t const paymentInterval = 600; + std::uint32_t const gracePeriod = 60; + + auto const brokerBeforeLoan = env.le(brokerKeylet); + BEAST_EXPECT(brokerBeforeLoan); + auto const loanSequence = brokerBeforeLoan->at(sfLoanSequence); + auto const loanKeylet = keylet::loan(broker.brokerID, loanSequence); + + LoanParameters const loanParams{ + .account = borrower, + .counter = lender, + .principalRequest = principalRequest, + .interest = TenthBips32{0}, + .payTotal = paymentTotal, + .payInterval = paymentInterval, + .gracePd = gracePeriod, + }; + env(loanParams(env, broker)); + env.close(); + + // Origination (cash-basis): AssetsTotal += 0, DebtTotal += principal. + checkVaultBroker(100'000, 1'200, 0, 1'000, "after LoanSet"); + + LoanState const state = getCurrentState(env, broker, loanKeylet); + BEAST_EXPECT(state.periodicPayment == xrpAsset(100).value()); + + // Payment 1: principalPaid=100, interestPaid=0. + // AssetsTotal += 0; DebtTotal -= 100. + env(pay(borrower, loanKeylet.key, xrpAsset(100).value()), Ter(tesSUCCESS)); + env.close(); + checkVaultBroker(100'000, 1'100, 0, 1'000, "after payment 1"); + + // Payment 2: same as above. + env(pay(borrower, loanKeylet.key, xrpAsset(100).value()), Ter(tesSUCCESS)); + env.close(); + checkVaultBroker(100'000, 1'000, 0, 1'000, "after payment 2"); + + // Default (no impair): principalOutstanding remaining is 1'000. + // totalDefaultAmount (cash-basis) = PrincipalOutstanding = 1'000. + // minimumCover = DebtTotal(1'000) * coverRateMin(10%) = 100. + // covered = min(minimumCover * coverRateLiquidation(25%), totalDefaultAmount) + // = min(25, 1'000) = 25. + // defaultCovered = min(covered, CoverAvailable(1'000)) = 25. + // vaultDefaultAmount = 1'000 - 25 = 975. + // DebtTotal -= 1'000 -> 0. CoverAvailable -= 25 -> 975. + // AssetsTotal -= 975 -> 99'025. LossUnrealized unaffected (never impaired). + auto const loanBeforeDefault = env.le(loanKeylet); + BEAST_EXPECT(loanBeforeDefault); + BEAST_EXPECT( + Number(loanBeforeDefault->at(sfPrincipalOutstanding)) == xrpAsset(1'000).value()); + + env.close(state.startDate + std::chrono::seconds((3 * paymentInterval) + gracePeriod) + 1s); + + env(manage(lender, loanKeylet.key, tfLoanDefault), Ter(tesSUCCESS)); + env.close(); + + checkVaultBroker(99'025, 0, 0, 975, "after LoanManage(default)"); + } + void runAmendmentIndependent() { @@ -8570,6 +9553,12 @@ protected: testBugInterestDueDeltaCrash(); testFullLifecycleVaultPnLNearZeroRate(); testLoanSetNearZeroInterestRateSucceeds(); + + testCashBasisLoanSetOrigination(); + testCashBasisLoanPay(); + testCashBasisLoanManage(); + testLegacyVaultKeepsAccrualAfterAmendmentEnabled(); + testCashBasisEndToEndTrajectory(); } // Tests run under each entry in amendmentCombinations(). diff --git a/src/test/app/Offer_test.cpp b/src/test/app/Offer_test.cpp index 7fc7161e36..33721e91d8 100644 --- a/src/test/app/Offer_test.cpp +++ b/src/test/app/Offer_test.cpp @@ -4324,6 +4324,165 @@ public: env.require(Balance(bob, gwUSD(10))); } + void + testDisallowIncomingTrustline(FeatureBitset features) + { + testcase("DisallowIncomingTrustline in OfferCreate"); + + // Test that asfDisallowIncomingTrustline flag prevents offer crossing + // when the taker doesn't have a trustline. + // + // 1. alice creates a trustline and sells USD/gw tokens. + // + // 2. gw sets asfDisallowIncomingTrustline flag. + // + // 3. An account without a trustline tries to create an offer for USD/gw. + // Without amendment: succeeds and crosses alice's offer (backward compatible). + // With amendment: fails with tecNO_LINE (new behavior). + // + // 4. An account WITH an existing trustline can create an offer. + // The offer succeeds and crosses alice's offer. + // + // Note: The DisallowIncomingTrustline flag also prevents NEW trustlines + // from being created via TrustSet (enforced by fixDisallowIncomingV1). + // So accounts must create trustlines BEFORE the issuer sets the flag. + + using namespace jtx; + auto const gw = Account("gw"); + auto const alice = Account("alice"); + auto const bob = Account("bob"); + auto const carol = Account("carol"); + auto const dan = Account("dan"); + auto const eve = Account("eve"); + auto const gwUSD = gw["USD"]; + + // Test without fixCleanup3_4_0 amendment + { + Env env{*this, features - fixCleanup3_4_0}; + + env.fund(XRP(400000), gw, alice, bob); + env.close(); + + // Alice creates trustline and gets some USD + env(trust(alice, gwUSD(100))); + env.close(); + env(pay(gw, alice, gwUSD(50))); + env.close(); + + // Alice creates sell offer + env(offer(alice, XRP(4000), gwUSD(40))); + env.close(); + env.require(offers(alice, 1)); + + // GW sets DisallowIncomingTrustline flag + env(fset(gw, asfDisallowIncomingTrustline)); + env.close(); + + // Without the amendment, bob can still create offer without trustline + // and the offer should cross (old behavior) + env(offer(bob, gwUSD(40), XRP(4000))); + env.close(); + + // Offer should have crossed + env.require(offers(alice, 0)); + env.require(offers(bob, 0)); + env.require(Balance(bob, gwUSD(40))); + } + + // Test with fixCleanup3_4_0 amendment + { + Env env{*this, features}; + + env.fund(XRP(400000), gw, alice, bob, carol, dan); + env.close(); + + // Alice creates trustline and gets some USD + env(trust(alice, gwUSD(100))); + env.close(); + env(pay(gw, alice, gwUSD(50))); + env.close(); + + // Bob and carol create trustlines BEFORE the flag is set + env(trust(bob, gwUSD(100))); + env.close(); + env(trust(carol, gwUSD(100))); + env.close(); + + // Alice creates sell offer + env(offer(alice, XRP(4000), gwUSD(40))); + env.close(); + env.require(offers(alice, 1)); + env.require(Balance(alice, gwUSD(50))); + + // GW sets DisallowIncomingTrustline flag + env(fset(gw, asfDisallowIncomingTrustline)); + env.close(); + + // Dan tries to create offer without trustline - should fail + env(offer(dan, gwUSD(40), XRP(4000)), Ter(tecNO_LINE)); + env.close(); + + // Alice's offer should still exist + env.require(offers(alice, 1)); + env.require(Balance(alice, gwUSD(50))); + + // Dan shouldn't have any offers or balance + env.require(offers(dan, 0)); + BEAST_EXPECT(env.le(keylet::trustLine(dan, gwUSD)) == nullptr); + + // Bob already has trustline, so his offer should succeed and cross + env(offer(bob, gwUSD(40), XRP(4000))); + env.close(); + + // Offer should have crossed + env.require(offers(alice, 0)); + env.require(offers(bob, 0)); + env.require(Balance(alice, gwUSD(10))); + env.require(Balance(bob, gwUSD(40))); + + // Test scenario where carol already has a trustline (created before flag was set) + // Carol should be able to create offer since trustline already exists + env(pay(gw, alice, gwUSD(50))); + env.close(); + env(offer(alice, XRP(1000), gwUSD(10))); + env.close(); + env.require(offers(alice, 1)); + + env(offer(carol, gwUSD(10), XRP(1000))); + env.close(); + + // Offer should have crossed + env.require(offers(alice, 0)); + env.require(offers(carol, 0)); + env.require(Balance(alice, gwUSD(50))); + env.require(Balance(carol, gwUSD(10))); + + // Test that gw can clear the flag + env(fclear(gw, asfDisallowIncomingTrustline)); + env.close(); + + // Create new account eve without trustline + env.fund(XRP(400000), eve); + env.close(); + + // Bob creates another sell offer + env(pay(gw, bob, gwUSD(50))); + env.close(); + env(offer(bob, XRP(5000), gwUSD(50))); + env.close(); + env.require(offers(bob, 1)); + + // Eve should now be able to create offer without trustline (flag is cleared) + env(offer(eve, gwUSD(50), XRP(5000))); + env.close(); + + // Offer should have crossed + env.require(offers(bob, 0)); + env.require(offers(eve, 0)); + env.require(Balance(eve, gwUSD(50))); + } + } + void testRCSmoketest(FeatureBitset features) { @@ -5167,6 +5326,7 @@ public: testSelfPayUnlimitedFunds(features); testRequireAuth(features); testMissingAuth(features); + testDisallowIncomingTrustline(features); testRCSmoketest(features); testSelfAuth(features); testDeletedOfferIssuer(features); diff --git a/src/test/app/PathMPT_test.cpp b/src/test/app/PathMPT_test.cpp index 3ba67b58a6..ff4a024cb8 100644 --- a/src/test/app/PathMPT_test.cpp +++ b/src/test/app/PathMPT_test.cpp @@ -112,10 +112,10 @@ public: MPTTester({.env = env, .issuer = gw, .holders = {alice, bob}, .maxAmt = 100}); auto& app = env.app(); - Resource::Charge loadType = Resource::kFeeReferenceRpc; - Resource::Consumer c; + resource::Charge loadType = resource::kFeeReferenceRpc; + resource::Consumer c; - RPC::JsonContext context{ + rpc::JsonContext context{ {.j = env.journal, .app = app, .loadType = loadType, @@ -125,39 +125,39 @@ public: .role = Role::USER, .coro = {}, .infoSub = {}, - .apiVersion = RPC::kApiVersionIfUnspecified}, + .apiVersion = rpc::kApiVersionIfUnspecified}, {}, {}}; json::Value result; Gate g; - // Test RPC::Tuning::max_src_cur source currencies. + // Test rpc::tuning::max_src_cur source currencies. std::vector numSrc; - numSrc.reserve(RPC::Tuning::kMaxSrcCur); - for (std::uint8_t i = 0; i < RPC::Tuning::kMaxSrcCur; ++i) + numSrc.reserve(rpc::tuning::kMaxSrcCur); + for (std::uint8_t i = 0; i < rpc::tuning::kMaxSrcCur; ++i) numSrc.push_back(makeMptID(i, bob)); app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = xrpl::test::detail::rpf(alice, bob, usd, numSrc); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); BEAST_EXPECT(!result.isMember(jss::error)); - // Test more than RPC::Tuning::max_src_cur source currencies. - numSrc.push_back(makeMptID(RPC::Tuning::kMaxSrcCur, bob)); + // Test more than rpc::tuning::max_src_cur source currencies. + numSrc.push_back(makeMptID(rpc::tuning::kMaxSrcCur, bob)); app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = xrpl::test::detail::rpf(alice, bob, usd, numSrc); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); BEAST_EXPECT(result.isMember(jss::error)); - // Test RPC::Tuning::max_auto_src_cur source currencies. + // Test rpc::tuning::max_auto_src_cur source currencies. numSrc.clear(); - for (auto i = 0; i < (RPC::Tuning::kMaxAutoSrcCur - 1); ++i) + for (auto i = 0; i < (rpc::tuning::kMaxAutoSrcCur - 1); ++i) { auto curm = MPTTester({.env = env, .issuer = alice, .holders = {bob}}); numSrc.push_back(curm.issuanceID()); @@ -165,18 +165,18 @@ public: app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = xrpl::test::detail::rpf(alice, bob, usd, {}); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); BEAST_EXPECT(!result.isMember(jss::error)); - // Test more than RPC::Tuning::max_auto_src_cur source currencies. + // Test more than rpc::tuning::max_auto_src_cur source currencies. auto curm = MPTTester({.env = env, .issuer = alice, .holders = {bob}}); app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = xrpl::test::detail::rpf(alice, bob, usd, {}); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); diff --git a/src/test/app/Path_test.cpp b/src/test/app/Path_test.cpp index 8f19a419a0..409a9e86f8 100644 --- a/src/test/app/Path_test.cpp +++ b/src/test/app/Path_test.cpp @@ -151,10 +151,10 @@ public: using namespace jtx; auto& app = env.app(); - Resource::Charge loadType = Resource::kFeeReferenceRpc; - Resource::Consumer c; + resource::Charge loadType = resource::kFeeReferenceRpc; + resource::Consumer c; - RPC::JsonContext context{ + rpc::JsonContext context{ {.j = env.journal, .app = app, .loadType = loadType, @@ -164,7 +164,7 @@ public: .role = Role::USER, .coro = {}, .infoSub = {}, - .apiVersion = RPC::kApiVersionIfUnspecified}, + .apiVersion = rpc::kApiVersionIfUnspecified}, {}, {}}; @@ -190,7 +190,7 @@ public: app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = std::move(params); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); @@ -262,10 +262,10 @@ public: env.close(); auto& app = env.app(); - Resource::Charge loadType = Resource::kFeeReferenceRpc; - Resource::Consumer c; + resource::Charge loadType = resource::kFeeReferenceRpc; + resource::Consumer c; - RPC::JsonContext context{ + rpc::JsonContext context{ {.j = env.journal, .app = app, .loadType = loadType, @@ -275,49 +275,49 @@ public: .role = Role::USER, .coro = {}, .infoSub = {}, - .apiVersion = RPC::kApiVersionIfUnspecified}, + .apiVersion = rpc::kApiVersionIfUnspecified}, {}, {}}; json::Value result; Gate g; - // Test RPC::Tuning::max_src_cur source currencies. + // Test rpc::tuning::max_src_cur source currencies. app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { - context.params = rpf(Account("alice"), Account("bob"), RPC::Tuning::kMaxSrcCur); + context.params = rpf(Account("alice"), Account("bob"), rpc::tuning::kMaxSrcCur); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); BEAST_EXPECT(!result.isMember(jss::error)); - // Test more than RPC::Tuning::max_src_cur source currencies. + // Test more than rpc::tuning::max_src_cur source currencies. app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { - context.params = rpf(Account("alice"), Account("bob"), RPC::Tuning::kMaxSrcCur + 1); + context.params = rpf(Account("alice"), Account("bob"), rpc::tuning::kMaxSrcCur + 1); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); BEAST_EXPECT(result.isMember(jss::error)); - // Test RPC::Tuning::max_auto_src_cur source currencies. - for (auto i = 0; i < (RPC::Tuning::kMaxAutoSrcCur - 1); ++i) + // Test rpc::tuning::max_auto_src_cur source currencies. + for (auto i = 0; i < (rpc::tuning::kMaxAutoSrcCur - 1); ++i) env.trust(Account("alice")[std::to_string(i + 100)](100), "bob"); app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = rpf(Account("alice"), Account("bob"), 0); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); BEAST_EXPECT(!result.isMember(jss::error)); - // Test more than RPC::Tuning::max_auto_src_cur source currencies. + // Test more than rpc::tuning::max_auto_src_cur source currencies. env.trust(Account("alice")["AUD"](100), "bob"); app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = rpf(Account("alice"), Account("bob"), 0); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); BEAST_EXPECT(g.waitFor(5s)); diff --git a/src/test/app/PermissionedDEX_test.cpp b/src/test/app/PermissionedDEX_test.cpp index 67cb7602a0..68b2fa99a7 100644 --- a/src/test/app/PermissionedDEX_test.cpp +++ b/src/test/app/PermissionedDEX_test.cpp @@ -1816,7 +1816,7 @@ class PermissionedDEX_test : public beast::unit_test::Suite env.fund(XRP(1000), carol); env.close(); - env(ledgerStateFix::bookExchangeRate(carol, uint256{1}), Ter(temDISABLED)); + env(ledger_state_fix::bookExchangeRate(carol, uint256{1}), Ter(temDISABLED)); } { @@ -1829,13 +1829,13 @@ class PermissionedDEX_test : public beast::unit_test::Suite env.close(); // BookExchangeRate fixes require sfBookDirectory. - auto missingBookDirectory = ledgerStateFix::bookExchangeRate(carol, uint256{1}); + auto missingBookDirectory = ledger_state_fix::bookExchangeRate(carol, uint256{1}); missingBookDirectory.removeMember(sfBookDirectory.jsonName); env(missingBookDirectory, Ter(temINVALID)); // BookExchangeRate fixes reject fields that belong to other // LedgerStateFix types. - auto extraOwner = ledgerStateFix::bookExchangeRate(carol, uint256{1}); + auto extraOwner = ledger_state_fix::bookExchangeRate(carol, uint256{1}); extraOwner[sfOwner.jsonName] = carol.human(); env(extraOwner, Ter(temINVALID)); } @@ -1847,7 +1847,7 @@ class PermissionedDEX_test : public beast::unit_test::Suite { // Preclaim check: the target directory must exist. - env(ledgerStateFix::bookExchangeRate(setup.carol, uint256{1}), + env(ledger_state_fix::bookExchangeRate(setup.carol, uint256{1}), Fee(fixFee), Ter(tecOBJECT_NOT_FOUND)); } @@ -1861,7 +1861,7 @@ class PermissionedDEX_test : public beast::unit_test::Suite BEAST_EXPECT(ownerDirSle); BEAST_EXPECT(!ownerDirSle->isFieldPresent(sfExchangeRate)); - env(ledgerStateFix::bookExchangeRate(setup.carol, ownerDir.key), + env(ledger_state_fix::bookExchangeRate(setup.carol, ownerDir.key), Fee(fixFee), Ter(tecNO_PERMISSION)); } @@ -1885,7 +1885,7 @@ class PermissionedDEX_test : public beast::unit_test::Suite BEAST_EXPECT(exchangeRate == quality); } - env(ledgerStateFix::bookExchangeRate(setup.carol, dirKey), + env(ledger_state_fix::bookExchangeRate(setup.carol, dirKey), Fee(fixFee), Ter(tecNO_PERMISSION)); } @@ -1932,7 +1932,7 @@ class PermissionedDEX_test : public beast::unit_test::Suite env.close(); auto const fixFee = drops(env.current()->fees().increment); - env(ledgerStateFix::bookExchangeRate(carol_, openDirKey), Fee(fixFee)); + env(ledger_state_fix::bookExchangeRate(carol_, openDirKey), Fee(fixFee)); env.close(); // Confirm sfExchangeRate now matches the key quality. @@ -1947,7 +1947,7 @@ class PermissionedDEX_test : public beast::unit_test::Suite } // Submitting again should fail — nothing to fix. - env(ledgerStateFix::bookExchangeRate(carol_, openDirKey), + env(ledger_state_fix::bookExchangeRate(carol_, openDirKey), Fee(fixFee), Ter(tecNO_PERMISSION)); } diff --git a/src/test/app/SHAMapStore_test.cpp b/src/test/app/SHAMapStore_test.cpp index c219bd8737..6ee7442d23 100644 --- a/src/test/app/SHAMapStore_test.cpp +++ b/src/test/app/SHAMapStore_test.cpp @@ -60,7 +60,7 @@ class SHAMapStore_test : public beast::unit_test::Suite static bool goodLedger(jtx::Env& env, json::Value const& json, std::string ledgerID, bool checkDB = false) { - auto good = json.isMember(jss::result) && !RPC::containsError(json[jss::result]) && + auto good = json.isMember(jss::result) && !rpc::containsError(json[jss::result]) && json[jss::result][jss::ledger][jss::ledger_index] == ledgerID; if (!good || !checkDB) return good; @@ -99,7 +99,7 @@ class SHAMapStore_test : public beast::unit_test::Suite static bool bad(json::Value const& json, ErrorCodeI error = RpcLgrNotFound) { - return json.isMember(jss::result) && RPC::containsError(json[jss::result]) && + return json.isMember(jss::result) && rpc::containsError(json[jss::result]) && json[jss::result][jss::error_code] == error; } @@ -347,11 +347,11 @@ public: BEAST_EXPECT(lastRotated != 2); auto canDelete = env.rpc("can_delete"); - BEAST_EXPECT(!RPC::containsError(canDelete[jss::result])); + BEAST_EXPECT(!rpc::containsError(canDelete[jss::result])); BEAST_EXPECT(canDelete[jss::result][jss::can_delete] == 0); canDelete = env.rpc("can_delete", "never"); - BEAST_EXPECT(!RPC::containsError(canDelete[jss::result])); + BEAST_EXPECT(!rpc::containsError(canDelete[jss::result])); BEAST_EXPECT(canDelete[jss::result][jss::can_delete] == 0); auto const firstBatch = kDeleteInterval + ledgerSeq; @@ -370,7 +370,7 @@ public: // This does not kick off a cleanup canDelete = env.rpc("can_delete", std::to_string(ledgerSeq + (kDeleteInterval / 2))); - BEAST_EXPECT(!RPC::containsError(canDelete[jss::result])); + BEAST_EXPECT(!rpc::containsError(canDelete[jss::result])); BEAST_EXPECT(canDelete[jss::result][jss::can_delete] == ledgerSeq + (kDeleteInterval / 2)); store.rendezvous(); @@ -423,7 +423,7 @@ public: // This does not kick off a cleanup canDelete = env.rpc("can_delete", "always"); - BEAST_EXPECT(!RPC::containsError(canDelete[jss::result])); + BEAST_EXPECT(!rpc::containsError(canDelete[jss::result])); BEAST_EXPECT( canDelete[jss::result][jss::can_delete] == std::numeric_limits::max()); @@ -457,7 +457,7 @@ public: // This does not kick off a cleanup canDelete = env.rpc("can_delete", "now"); - BEAST_EXPECT(!RPC::containsError(canDelete[jss::result])); + BEAST_EXPECT(!rpc::containsError(canDelete[jss::result])); BEAST_EXPECT(canDelete[jss::result][jss::can_delete] == ledgerSeq - 1); for (; ledgerSeq < lastRotated + kDeleteInterval; ++ledgerSeq) @@ -489,7 +489,7 @@ public: lastRotated = ledgerSeq - 1; } - std::unique_ptr + std::unique_ptr makeBackendRotating(jtx::Env& env, NodeStoreScheduler& scheduler, std::string path) { Section section{env.app().config().section(Sections::kNodeDatabase)}; @@ -500,7 +500,7 @@ public: newPath = path; section.set(Keys::kPath, newPath.string()); - auto backend{NodeStore::Manager::instance().makeBackend( + auto backend{node_store::Manager::instance().makeBackend( section, megabytes(env.app().config().getValueFor(SizedItem::BurstSize, std::nullopt)), scheduler, @@ -549,7 +549,7 @@ public: auto archiveBackend = makeBackendRotating(env, scheduler, archiveDb); static constexpr int kReadThreads = 4; - auto dbr = std::make_unique( + auto dbr = std::make_unique( scheduler, kReadThreads, std::move(writableBackend), diff --git a/src/test/app/Sponsor_test.cpp b/src/test/app/Sponsor_test.cpp index f20aac68f9..393a6e58f7 100644 --- a/src/test/app/Sponsor_test.cpp +++ b/src/test/app/Sponsor_test.cpp @@ -1664,11 +1664,11 @@ public: env.close(); auto const brokerKeylet = keylet::loanBroker(alice.id(), env.seq(alice)); - env(loanBroker::set(alice, vaultKeylet.key), - loanBroker::kDebtMaximum(xrpAsset(1000).value()), - loanBroker::kManagementFeeRate(TenthBips16{0}), - loanBroker::kCoverRateMinimum(TenthBips32{0}), - loanBroker::kCoverRateLiquidation(TenthBips32{0})); + env(loan_broker::set(alice, vaultKeylet.key), + loan_broker::kDebtMaximum(xrpAsset(1000).value()), + loan_broker::kManagementFeeRate(TenthBips16{0}), + loan_broker::kCoverRateMinimum(TenthBips32{0}), + loan_broker::kCoverRateLiquidation(TenthBips32{0})); env.close(); auto const loanKeylet = keylet::loan(brokerKeylet.key, 1); @@ -2019,7 +2019,7 @@ public: env(sponsor::set_fee(sponsor, 0, fixFee), sponsor::SponseeAcc(alice)); env.close(); - env(ledgerStateFix::nftPageLinks(alice, alice), + env(ledger_state_fix::nftPageLinks(alice, alice), Fee(fixFee), sponsor::As(sponsor, spfSponsorFee), Ter(tecFAILED_PROCESSING)); @@ -2043,7 +2043,7 @@ public: OpenView overlay(&*env.closed()); auto jt = env.jt( - ledgerStateFix::nftPageLinks(alice, alice), + ledger_state_fix::nftPageLinks(alice, alice), Fee(fixFee), sponsor::As(sponsor, spfSponsorFee)); diff --git a/src/test/app/TxQ_test.cpp b/src/test/app/TxQ_test.cpp index 3175e742d9..1fe48add27 100644 --- a/src/test/app/TxQ_test.cpp +++ b/src/test/app/TxQ_test.cpp @@ -2571,7 +2571,7 @@ public: auto fee = env.rpc("fee"); if (BEAST_EXPECT(fee.isMember(jss::result)) && - BEAST_EXPECT(!RPC::containsError(fee[jss::result]))) + BEAST_EXPECT(!rpc::containsError(fee[jss::result]))) { auto const& result = fee[jss::result]; BEAST_EXPECT( @@ -2600,7 +2600,7 @@ public: fee = env.rpc("fee"); if (BEAST_EXPECT(fee.isMember(jss::result)) && - BEAST_EXPECT(!RPC::containsError(fee[jss::result]))) + BEAST_EXPECT(!rpc::containsError(fee[jss::result]))) { auto const& result = fee[jss::result]; BEAST_EXPECT( @@ -3225,7 +3225,7 @@ public: { auto const info = env.rpc("json", "account_info", to_string(prevLedgerWithQueue)); - BEAST_EXPECT(info.isMember(jss::result) && RPC::containsError(info[jss::result])); + BEAST_EXPECT(info.isMember(jss::result) && rpc::containsError(info[jss::result])); } env.close(); @@ -4630,7 +4630,7 @@ public: auto const fee = env.rpc("fee"); if (BEAST_EXPECT(fee.isMember(jss::result)) && - BEAST_EXPECT(!RPC::containsError(fee[jss::result]))) + BEAST_EXPECT(!rpc::containsError(fee[jss::result]))) { auto const& result = fee[jss::result]; @@ -4688,7 +4688,7 @@ public: auto const fee = env.rpc("fee"); if (BEAST_EXPECT(fee.isMember(jss::result)) && - BEAST_EXPECT(!RPC::containsError(fee[jss::result]))) + BEAST_EXPECT(!rpc::containsError(fee[jss::result]))) { auto const& result = fee[jss::result]; diff --git a/src/test/app/Vault_test.cpp b/src/test/app/Vault_test.cpp index 12ad7e6782..791cf216c3 100644 --- a/src/test/app/Vault_test.cpp +++ b/src/test/app/Vault_test.cpp @@ -3936,7 +3936,7 @@ class Vault_test : public beast::unit_test::Suite // Deposit 100 IOU → 1000 shares. Borrow 40 → assetsAvailable=60. // Clawback 80 IOU → clamped to 60, then share math uses truncation. testCase(1, [&, this](Env& env, Data d) { - using namespace loanBroker; + using namespace loan_broker; using namespace loan; testcase("Scale clawback clamped with outstanding loan"); @@ -4410,7 +4410,7 @@ class Vault_test : public beast::unit_test::Suite testVaultClawbackBurnShares() { using namespace test::jtx; - using namespace loanBroker; + using namespace loan_broker; using namespace loan; Env env(*this, beast::Severity::Warning); @@ -4670,7 +4670,7 @@ class Vault_test : public beast::unit_test::Suite testVaultClawbackAssets() { using namespace test::jtx; - using namespace loanBroker; + using namespace loan_broker; using namespace loan; Env env(*this); env.enableFeature(fixCleanup3_1_3); @@ -6093,7 +6093,7 @@ class Vault_test : public beast::unit_test::Suite // Loan broker: no cover, no management fee, debt cap 10x principal. f.brokerID = keylet::loanBroker(f.lender.id(), env.seq(f.lender)).key; { - using namespace loanBroker; + using namespace loan_broker; env(set(f.lender, vaultKeylet.key), kDebtMaximum((*f.asset)(kStuckPrincipal * 10).value())); env.close(); @@ -7645,6 +7645,83 @@ class Vault_test : public beast::unit_test::Suite } } + void + testVaultCreateLEVersion() + { + using namespace test::jtx; + + Account const owner{"owner"}; + PrettyAsset const xrpAsset = xrpIssue(); + + { + testcase("VaultCreate LEVersion: featureLendingProtocolV1_1 disabled, field absent"); + Env env{*this}; + env.disableFeature(featureLendingProtocolV1_1); + env.fund(XRP(1'000'000), owner); + env.close(); + + Vault const vault{env}; + auto const [tx, keylet] = vault.create({.owner = owner, .asset = xrpAsset}); + env(tx, Ter(tesSUCCESS)); + env.close(); + + auto const sleVault = env.le(keylet); + BEAST_EXPECT(sleVault); + BEAST_EXPECT(!sleVault->isFieldPresent(sfLEVersion)); + } + + { + testcase( + "VaultCreate LEVersion: featureLendingProtocolV1_1 enabled, LEVersion == " + "VaultVersion::CashBasis"); + Env env{*this}; + env.fund(XRP(1'000'000), owner); + env.close(); + + Vault const vault{env}; + auto const [tx, keylet] = vault.create({.owner = owner, .asset = xrpAsset}); + env(tx, Ter(tesSUCCESS)); + env.close(); + + auto const sleVault = env.le(keylet); + BEAST_EXPECT(sleVault); + BEAST_EXPECT(sleVault->isFieldPresent(sfLEVersion)); + BEAST_EXPECT(sleVault->at(sfLEVersion) == std::to_underlying(VaultVersion::CashBasis)); + } + + { + testcase("VaultCreate rejects LEVersion set in the transaction"); + Env env{*this}; + env.fund(XRP(1'000'000), owner); + env.close(); + + Vault const vault{env}; + auto [tx, keylet] = vault.create({.owner = owner, .asset = xrpAsset}); + tx[sfLEVersion] = 2; + env(tx, Ter(temMALFORMED)); + env.close(); + + BEAST_EXPECT(!env.le(keylet)); + } + + { + testcase("VaultSet rejects LEVersion set in the transaction"); + Env env{*this}; + env.fund(XRP(1'000'000), owner); + env.close(); + + Vault const vault{env}; + auto const [createTx, keylet] = vault.create({.owner = owner, .asset = xrpAsset}); + env(createTx, Ter(tesSUCCESS)); + env.close(); + + auto setTx = vault.set({.owner = owner, .id = keylet.key}); + setTx[sfLEVersion] = 2; + env(setTx, Ter(temMALFORMED)); + env.close(); + } + } + void testVaultDepositFreezeIOU() { @@ -8317,6 +8394,7 @@ public: testVaultEscrowedMPT(); testAssetsMaximum(); testVaultDeleteMemoData(); + testVaultCreateLEVersion(); testBug6LimitBypassWithShares(); testRemoveEmptyHoldingLockedAmount(); testRemoveEmptyHoldingConfidentialBalances(); diff --git a/src/test/basics/PerfLog_test.cpp b/src/test/basics/PerfLog_test.cpp index 41b5f81f5d..24ea971515 100644 --- a/src/test/basics/PerfLog_test.cpp +++ b/src/test/basics/PerfLog_test.cpp @@ -312,7 +312,7 @@ public: // Get the all the labels we can use for RPC interfaces without // causing an assert. - std::vector labels = test::jtx::makeVector(xrpl::RPC::getHandlerNames()); + std::vector labels = test::jtx::makeVector(xrpl::rpc::getHandlerNames()); std::shuffle(labels.begin(), labels.end(), defaultPrng()); // Get two IDs to associate with each label. Errors tend to happen at @@ -483,7 +483,7 @@ public: json::Value parsedLastLine; json::Reader().parse(lastLine, parsedLastLine); - if (!BEAST_EXPECT(!RPC::containsError(parsedLastLine))) + if (!BEAST_EXPECT(!rpc::containsError(parsedLastLine))) { // Avoid cascade of failures return; @@ -804,7 +804,7 @@ public: json::Value parsedLastLine; json::Reader().parse(lastLine, parsedLastLine); - if (!BEAST_EXPECT(!RPC::containsError(parsedLastLine))) + if (!BEAST_EXPECT(!rpc::containsError(parsedLastLine))) { // Avoid cascade of failures return; @@ -944,7 +944,7 @@ public: json::Value parsedLastLine; json::Reader().parse(lastLine, parsedLastLine); - if (!BEAST_EXPECT(!RPC::containsError(parsedLastLine))) + if (!BEAST_EXPECT(!rpc::containsError(parsedLastLine))) { // Avoid cascade of failures return; diff --git a/src/test/beast/IPEndpointCommon.h b/src/test/beast/IPEndpointCommon.h index 45d036476c..6fb2bd9569 100644 --- a/src/test/beast/IPEndpointCommon.h +++ b/src/test/beast/IPEndpointCommon.h @@ -7,7 +7,7 @@ #include -namespace beast::IP { +namespace beast::ip { inline Endpoint randomEP(bool v4 = true) @@ -44,4 +44,4 @@ randomEP(bool v4 = true) randInt(1, UINT16_MAX)}; } -} // namespace beast::IP +} // namespace beast::ip diff --git a/src/test/beast/IPEndpoint_test.cpp b/src/test/beast/IPEndpoint_test.cpp index bc04087891..b61878aa76 100644 --- a/src/test/beast/IPEndpoint_test.cpp +++ b/src/test/beast/IPEndpoint_test.cpp @@ -22,7 +22,7 @@ #include #include -namespace beast::IP { +namespace beast::ip { //------------------------------------------------------------------------------ @@ -475,4 +475,4 @@ public: BEAST_DEFINE_TESTSUITE(IPEndpoint, beast, beast); -} // namespace beast::IP +} // namespace beast::ip diff --git a/src/test/beast/LexicalCast_test.cpp b/src/test/beast/LexicalCast_test.cpp deleted file mode 100644 index b1d37daab8..0000000000 --- a/src/test/beast/LexicalCast_test.cpp +++ /dev/null @@ -1,280 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -namespace beast { - -class LexicalCast_test : public unit_test::Suite -{ -public: - template - static IntType - nextRandomInt(xor_shift_engine& r) - { - return static_cast(r()); - } - - template - void - testInteger(IntType in) - { - std::string s; - auto out = static_cast(~in); // Ensure out != in - - expect(lexicalCastChecked(s, in)); - expect(lexicalCastChecked(out, s)); - expect(out == in); - } - - template - void - testIntegers(xor_shift_engine& r) - { - { - std::stringstream ss; - ss << "random " << typeid(IntType).name(); - testcase(ss.str()); - - for (int i = 0; i < 1000; ++i) - { - auto const value = nextRandomInt(r); - testInteger(value); - } - } - - { - std::stringstream ss; - ss << "numeric_limits <" << typeid(IntType).name() << ">"; - testcase(ss.str()); - - testInteger(std::numeric_limits::min()); - testInteger(std::numeric_limits::max()); - } - } - - void - testPathologies() - { - testcase("pathologies"); - try - { - lexicalCastThrow("\xef\xbc\x91\xef\xbc\x90"); // utf-8 encoded - } - catch (BadLexicalCast const&) - { - pass(); - } - } - - template - void - tryBadConvert(std::string const& s) - { - T out; - expect(!lexicalCastChecked(out, s), s); - } - - void - testConversionOverflows() - { - testcase("conversion overflows"); - - tryBadConvert("99999999999999999999"); - tryBadConvert("4294967300"); - tryBadConvert("75821"); - } - - void - testConversionUnderflows() - { - testcase("conversion underflows"); - - tryBadConvert("-1"); - - tryBadConvert("-99999999999999999999"); - tryBadConvert("-4294967300"); - tryBadConvert("-75821"); - } - - template - bool - tryEdgeCase(std::string const& s) - { - T ret; - - bool const result = lexicalCastChecked(ret, s); - - if (!result) - return false; - - return s == std::to_string(ret); - } - - void - testEdgeCases() - { - testcase("conversion edge cases"); - - expect(tryEdgeCase("18446744073709551614")); - expect(tryEdgeCase("18446744073709551615")); - expect(!tryEdgeCase("18446744073709551616")); - - expect(tryEdgeCase("9223372036854775806")); - expect(tryEdgeCase("9223372036854775807")); - expect(!tryEdgeCase("9223372036854775808")); - - expect(tryEdgeCase("-9223372036854775807")); - expect(tryEdgeCase("-9223372036854775808")); - expect(!tryEdgeCase("-9223372036854775809")); - - expect(tryEdgeCase("4294967294")); - expect(tryEdgeCase("4294967295")); - expect(!tryEdgeCase("4294967296")); - - expect(tryEdgeCase("2147483646")); - expect(tryEdgeCase("2147483647")); - expect(!tryEdgeCase("2147483648")); - - expect(tryEdgeCase("-2147483647")); - expect(tryEdgeCase("-2147483648")); - expect(!tryEdgeCase("-2147483649")); - - expect(tryEdgeCase("65534")); - expect(tryEdgeCase("65535")); - expect(!tryEdgeCase("65536")); - - expect(tryEdgeCase("32766")); - expect(tryEdgeCase("32767")); - expect(!tryEdgeCase("32768")); - - expect(tryEdgeCase("-32767")); - expect(tryEdgeCase("-32768")); - expect(!tryEdgeCase("-32769")); - } - - template - void - testThrowConvert(std::string const& s, bool success) - { - bool result = !success; - T out; - - try - { - out = lexicalCastThrow(s); - result = true; - } - catch (BadLexicalCast const&) - { - result = false; - } - - expect(result == success, s); - } - - void - testThrowingConversions() - { - testcase("throwing conversion"); - - testThrowConvert("99999999999999999999", false); - testThrowConvert("9223372036854775806", true); - - testThrowConvert("4294967290", true); - testThrowConvert("42949672900", false); - testThrowConvert("429496729000", false); - testThrowConvert("4294967290000", false); - - testThrowConvert("5294967295", false); - testThrowConvert("-2147483644", true); - - testThrowConvert("66666", false); - testThrowConvert("-5711", true); - } - - void - testZero() - { - testcase("zero conversion"); - - { - std::int32_t out = 0; - - expect(lexicalCastChecked(out, "-0"), "0"); - expect(lexicalCastChecked(out, "0"), "0"); - expect(lexicalCastChecked(out, "+0"), "0"); - } - - { - std::uint32_t out = 0; - - expect(!lexicalCastChecked(out, "-0"), "0"); - expect(lexicalCastChecked(out, "0"), "0"); - expect(lexicalCastChecked(out, "+0"), "0"); - } - } - - void - testEntireRange() - { - testcase("entire range"); - - std::int32_t i = std::numeric_limits::min(); - std::string const empty; - - while (i <= std::numeric_limits::max()) - { - auto const j = static_cast(i); - - auto actual = std::to_string(j); - - auto result = lexicalCast(j, empty); - - expect(result == actual, actual + " (string to integer)"); - - if (result == actual) - { - auto number = lexicalCast(result); - - if (number != j) - expect(false, actual + " (integer to string)"); - } - - i++; - } - } - - void - run() override - { - std::int64_t const seedValue = 50; - - xor_shift_engine r(seedValue); - - testIntegers(r); - testIntegers(r); - testIntegers(r); - testIntegers(r); - testIntegers(r); - testIntegers(r); - testIntegers(r); - testIntegers(r); - - testPathologies(); - testConversionOverflows(); - testConversionUnderflows(); - testThrowingConversions(); - testZero(); - testEdgeCases(); - testEntireRange(); - } -}; - -BEAST_DEFINE_TESTSUITE(LexicalCast, beast, beast); - -} // namespace beast diff --git a/src/test/beast/SemanticVersion_test.cpp b/src/test/beast/SemanticVersion_test.cpp deleted file mode 100644 index d7079080c8..0000000000 --- a/src/test/beast/SemanticVersion_test.cpp +++ /dev/null @@ -1,266 +0,0 @@ -#include -#include - -#include - -namespace beast { - -class SemanticVersion_test : public unit_test::Suite -{ - using identifier_list = SemanticVersion::identifier_list; - -public: - void - checkPass(std::string const& input, bool shouldPass = true) - { - SemanticVersion v; - - if (shouldPass) - { - BEAST_EXPECT(v.parse(input)); - BEAST_EXPECT(v.print() == input); - } - else - { - BEAST_EXPECT(!v.parse(input)); - } - } - - void - checkFail(std::string const& input) - { - checkPass(input, false); - } - - // check input and input with appended metadata - void - checkMeta(std::string const& input, bool shouldPass) - { - checkPass(input, shouldPass); - - checkPass(input + "+a", shouldPass); - checkPass(input + "+1", shouldPass); - checkPass(input + "+a.b", shouldPass); - checkPass(input + "+ab.cd", shouldPass); - - checkFail(input + "!"); - checkFail(input + "+"); - checkFail(input + "++"); - checkFail(input + "+!"); - checkFail(input + "+."); - checkFail(input + "+a.!"); - } - - void - checkMetaFail(std::string const& input) - { - checkMeta(input, false); - } - - // check input, input with appended release data, - // input with appended metadata, and input with both - // appended release data and appended metadata - // - void - checkRelease(std::string const& input, bool shouldPass = true) - { - checkMeta(input, shouldPass); - - checkMeta(input + "-1", shouldPass); - checkMeta(input + "-a", shouldPass); - checkMeta(input + "-a1", shouldPass); - checkMeta(input + "-a1.b1", shouldPass); - checkMeta(input + "-ab.cd", shouldPass); - checkMeta(input + "--", shouldPass); - - checkMetaFail(input + "+"); - checkMetaFail(input + "!"); - checkMetaFail(input + "-"); - checkMetaFail(input + "-!"); - checkMetaFail(input + "-."); - checkMetaFail(input + "-a.!"); - checkMetaFail(input + "-0.a"); - } - - // Checks the major.minor.version string alone and with all - // possible combinations of release identifiers and metadata. - // - void - check(std::string const& input, bool shouldPass = true) - { - checkRelease(input, shouldPass); - } - - void - negcheck(std::string const& input) - { - check(input, false); - } - - void - testParse() - { - testcase("parsing"); - - check("0.0.0"); - check("1.2.3"); - check("2147483647.2147483647.2147483647"); // max int - - // negative values - negcheck("-1.2.3"); - negcheck("1.-2.3"); - negcheck("1.2.-3"); - - // missing parts - negcheck(""); - negcheck("1"); - negcheck("1."); - negcheck("1.2"); - negcheck("1.2."); - negcheck(".2.3"); - - // whitespace - negcheck(" 1.2.3"); - negcheck("1 .2.3"); - negcheck("1.2 .3"); - negcheck("1.2.3 "); - - // leading zeroes - negcheck("01.2.3"); - negcheck("1.02.3"); - negcheck("1.2.03"); - } - - static identifier_list - ids() - { - return identifier_list(); - } - - static identifier_list - ids(std::string const& s1) - { - identifier_list v; - v.push_back(s1); - return v; - } - - static identifier_list - ids(std::string const& s1, std::string const& s2) - { - identifier_list v; - v.push_back(s1); - v.push_back(s2); - return v; - } - - static identifier_list - ids(std::string const& s1, std::string const& s2, std::string const& s3) - { - identifier_list v; - v.push_back(s1); - v.push_back(s2); - v.push_back(s3); - return v; - } - - // Checks the decomposition of the input into appropriate values - void - checkValues( - std::string const& input, - int majorVersion, - int minorVersion, - int patchVersion, - identifier_list const& preReleaseIdentifiers = identifier_list(), - identifier_list const& metaData = identifier_list()) - { - SemanticVersion v; - - BEAST_EXPECT(v.parse(input)); - - BEAST_EXPECT(v.majorVersion == majorVersion); - BEAST_EXPECT(v.minorVersion == minorVersion); - BEAST_EXPECT(v.patchVersion == patchVersion); - - BEAST_EXPECT(v.preReleaseIdentifiers == preReleaseIdentifiers); - BEAST_EXPECT(v.metaData == metaData); - } - - void - testValues() - { - testcase("values"); - - checkValues("0.1.2", 0, 1, 2); - checkValues("1.2.3", 1, 2, 3); - checkValues("1.2.3-rc1", 1, 2, 3, ids("rc1")); - checkValues("1.2.3-rc1.debug", 1, 2, 3, ids("rc1", "debug")); - checkValues("1.2.3-rc1.debug.asm", 1, 2, 3, ids("rc1", "debug", "asm")); - checkValues("1.2.3+full", 1, 2, 3, ids(), ids("full")); - checkValues("1.2.3+full.prod", 1, 2, 3, ids(), ids("full", "prod")); - checkValues("1.2.3+full.prod.x86", 1, 2, 3, ids(), ids("full", "prod", "x86")); - checkValues( - "1.2.3-rc1.debug.asm+full.prod.x86", - 1, - 2, - 3, - ids("rc1", "debug", "asm"), - ids("full", "prod", "x86")); - } - - // makes sure the left version is less than the right - void - checkLessInternal(std::string const& lhs, std::string const& rhs) - { - SemanticVersion left; - SemanticVersion right; - - BEAST_EXPECT(left.parse(lhs)); - BEAST_EXPECT(right.parse(rhs)); - - BEAST_EXPECT(compare(left, left) == 0); - BEAST_EXPECT(compare(right, right) == 0); - BEAST_EXPECT(compare(left, right) < 0); - BEAST_EXPECT(compare(right, left) > 0); - - BEAST_EXPECT(left < right); - BEAST_EXPECT(right > left); - BEAST_EXPECT(left == left); - BEAST_EXPECT(right == right); - } - - void - checkLess(std::string const& lhs, std::string const& rhs) - { - checkLessInternal(lhs, rhs); - checkLessInternal(lhs + "+meta", rhs); - checkLessInternal(lhs, rhs + "+meta"); - checkLessInternal(lhs + "+meta", rhs + "+meta"); - } - - void - testCompare() - { - testcase("comparisons"); - - checkLess("1.0.0-alpha", "1.0.0-alpha.1"); - checkLess("1.0.0-alpha.1", "1.0.0-alpha.beta"); - checkLess("1.0.0-alpha.beta", "1.0.0-beta"); - checkLess("1.0.0-beta", "1.0.0-beta.2"); - checkLess("1.0.0-beta.2", "1.0.0-beta.11"); - checkLess("1.0.0-beta.11", "1.0.0-rc.1"); - checkLess("1.0.0-rc.1", "1.0.0"); - checkLess("0.9.9", "1.0.0"); - } - - void - run() override - { - testParse(); - testValues(); - testCompare(); - } -}; - -BEAST_DEFINE_TESTSUITE(SemanticVersion, beast, beast); -} // namespace beast diff --git a/src/test/beast/beast_Zero_test.cpp b/src/test/beast/beast_Zero_test.cpp deleted file mode 100644 index bb61844caa..0000000000 --- a/src/test/beast/beast_Zero_test.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include - -namespace beast { - -struct AdlTester -{ -}; - -int -signum(AdlTester) -{ - return 0; -} - -namespace inner_adl_test { - -struct AdlTester2 -{ -}; - -int -signum(AdlTester2) -{ - return 0; -} - -} // namespace inner_adl_test - -class Zero_test : public beast::unit_test::Suite -{ -private: - struct IntegerWrapper - { - int value; - - IntegerWrapper(int v) : value(v) - { - } - - [[nodiscard]] int - signum() const - { - return value; - } - }; - -public: - void - expectSame(bool result, bool correct, char const* message) - { - expect(result == correct, message); - } - - void - testLhsZero(IntegerWrapper x) - { - expectSame(x >= kZero, x.signum() >= 0, "lhs greater-than-or-equal-to"); - expectSame(x > kZero, x.signum() > 0, "lhs greater than"); - expectSame(x == kZero, x.signum() == 0, "lhs equal to"); - expectSame(x != kZero, x.signum() != 0, "lhs not equal to"); - expectSame(x < kZero, x.signum() < 0, "lhs less than"); - expectSame(x <= kZero, x.signum() <= 0, "lhs less-than-or-equal-to"); - } - - void - testLhsZero() - { - testcase("lhs zero"); - - testLhsZero(-7); - testLhsZero(0); - testLhsZero(32); - } - - void - testRhsZero(IntegerWrapper x) - { - expectSame(kZero >= x, 0 >= x.signum(), "rhs greater-than-or-equal-to"); - expectSame(kZero > x, 0 > x.signum(), "rhs greater than"); - expectSame(kZero == x, 0 == x.signum(), "rhs equal to"); - expectSame(kZero != x, 0 != x.signum(), "rhs not equal to"); - expectSame(kZero < x, 0 < x.signum(), "rhs less than"); - expectSame(kZero <= x, 0 <= x.signum(), "rhs less-than-or-equal-to"); - } - - void - testRhsZero() - { - testcase("rhs zero"); - - testRhsZero(-4); - testRhsZero(0); - testRhsZero(64); - } - - void - testAdl() - { - expect(AdlTester{} == kZero, "ADL failure!"); - expect(inner_adl_test::AdlTester2{} == kZero, "ADL failure!"); - } - - void - run() override - { - testLhsZero(); - testRhsZero(); - testAdl(); - } -}; - -BEAST_DEFINE_TESTSUITE(Zero, beast, beast); - -} // namespace beast diff --git a/src/test/jtx/AMM.h b/src/test/jtx/AMM.h index 68b6d9f745..435e32b7b4 100644 --- a/src/test/jtx/AMM.h +++ b/src/test/jtx/AMM.h @@ -199,7 +199,7 @@ public: std::optional const& asset2 = std::nullopt, std::optional const& ammAccount = std::nullopt, bool ignoreParams = false, - unsigned apiVersion = RPC::kApiInvalidVersion) const; + unsigned apiVersion = rpc::kApiInvalidVersion) const; [[nodiscard]] json::Value ammRpcInfo( diff --git a/src/test/jtx/AbstractClient.h b/src/test/jtx/AbstractClient.h index f9d8de0768..58f57f67a5 100644 --- a/src/test/jtx/AbstractClient.h +++ b/src/test/jtx/AbstractClient.h @@ -40,6 +40,17 @@ public: */ [[nodiscard]] virtual unsigned version() const = 0; + + /** + * Close the client's connection to the server. + * + * Releases the connection the client holds against the server's per-port + * connection limit. After this call the client must not be used to + * invoke() again. Tests use this to deterministically free the slot + * rather than waiting for the server's idle timeout to drop it. + */ + virtual void + disconnect() = 0; }; } // namespace xrpl::test diff --git a/src/test/jtx/ConfidentialTransfer.h b/src/test/jtx/ConfidentialTransfer.h index 404ddbe31d..465bac03db 100644 --- a/src/test/jtx/ConfidentialTransfer.h +++ b/src/test/jtx/ConfidentialTransfer.h @@ -94,6 +94,36 @@ protected: return proof; } + // Generate a forged single bulletproof for a single value and blinding factor. + // Used to test ConvertBack overdraft prevention via bulletproof verification. + static Buffer + getForgedSingleBulletproof( + uint64_t value, + Buffer const& blindingFactor, + uint256 const& contextHash) + { + auto* const ctx = mpt_secp256k1_context(); + + secp256k1_pubkey h; + secp256k1_mpt_get_h_generator(ctx, &h); + + Buffer proof(kEcSingleBulletproofLength); + size_t proofLen = kEcSingleBulletproofLength; + + if (secp256k1_bulletproof_prove_agg( + ctx, + proof.data(), + &proofLen, + &value, + blindingFactor.data(), + 1, // m = 1 (single bulletproof) + &h, + contextHash.data()) == 0) + Throw("Failed to generate forged single bulletproof"); + + return proof; + } + // Get a bad ciphertext with valid structure but cryptographic invalid for // testing purposes. For preflight test purposes. static Buffer const& diff --git a/src/test/jtx/Env.h b/src/test/jtx/Env.h index 0df62c7e9b..5cb841578a 100644 --- a/src/test/jtx/Env.h +++ b/src/test/jtx/Env.h @@ -482,11 +482,52 @@ public: app().getNumberOfThreads() == 1, "syncClose() is only useful on an application with a single thread"); auto const result = close(); - auto serverBarrier = std::make_shared>(); - auto future = serverBarrier->get_future(); - boost::asio::post(app().getIOContext(), [serverBarrier]() { serverBarrier->set_value(); }); - auto const status = future.wait_for(timeout); - return result && status == std::future_status::ready; + return result && drainServerIo(timeout); + } + + /** + * Disconnect the Env's built-in client and wait for the server to + * register the dropped connection. + * + * Env holds one persistent client connection to the server's RPC port for + * its whole lifetime (see client()), and that connection counts against + * the port's connection limit. Tests that need a known starting occupancy + * can call this to deterministically release that slot instead of waiting + * out the server's localhost idle timeout. + * + * The server decrements its per-port connection count in the peer's + * destructor, which runs when the io_context processes the end-of-stream + * on the closed socket. After closing the client this drains the server's + * io_context twice: the first barrier guarantees the reactor has reaped + * the closed socket and queued the peer's teardown, and the second + * guarantees that teardown (and therefore the count decrement) has run. + * + * This is only sound when the server uses a single io_context thread, so + * that draining establishes ordering against the teardown - configure the + * Env with singleThreadIo() (as syncClose() also requires). Like + * syncClose(), it relies on loopback teardown latency being negligible. + * + * @param timeout Maximum time to wait for each barrier task to execute + * @return true if both barriers executed within timeout, false otherwise + */ + [[nodiscard]] bool + disconnectClient(std::chrono::steady_clock::duration timeout = std::chrono::seconds{1}) + { + XRPL_ASSERT( + app().getNumberOfThreads() == 1, + "disconnectClient() is only useful on an application with a single " + "thread"); + + bundle_.client->disconnect(); + + // Drain the server's single io thread twice: the first barrier flushes + // the reactor's reap of the closed socket (queuing the peer teardown), + // the second flushes that teardown - and therefore the connection-count + // decrement. Both run unconditionally so a timed-out first drain does + // not short-circuit the second. + bool const reaped = drainServerIo(timeout); + bool const toreDown = drainServerIo(timeout); + return reaped && toreDown; } /** @@ -846,6 +887,25 @@ public: } private: + /** + * Drain the (single) server io_context thread once. + * + * Posts a barrier task to the server's io_context and blocks until it + * runs, so every task queued before it has been processed. Only meaningful + * with a single io thread (see syncClose()/disconnectClient()). + * + * @param timeout Maximum time to wait for the barrier task to execute + * @return true if the barrier ran within timeout, false otherwise + */ + [[nodiscard]] bool + drainServerIo(std::chrono::steady_clock::duration timeout) + { + auto barrier = std::make_shared>(); + auto future = barrier->get_future(); + boost::asio::post(app().getIOContext(), [barrier]() { barrier->set_value(); }); + return future.wait_for(timeout) == std::future_status::ready; + } + void fund(bool setDefaultRipple, STAmount const& amount, Account const& account); @@ -1021,7 +1081,7 @@ Env::rpc( Args&&... args) { return doRpc( - RPC::kApiCommandLineVersion, + rpc::kApiCommandLineVersion, std::vector{cmd, std::forward(args)...}, headers); } diff --git a/src/test/jtx/TestHelpers.h b/src/test/jtx/TestHelpers.h index e7a2808f07..d5cd8e66b8 100644 --- a/src/test/jtx/TestHelpers.h +++ b/src/test/jtx/TestHelpers.h @@ -876,7 +876,7 @@ checkMetrics( /* LoanBroker */ /******************************************************************************/ -namespace loanBroker { +namespace loan_broker { json::Value set(AccountID const& account, uint256 const& vaultId, std::uint32_t flags = 0); @@ -917,7 +917,7 @@ auto const kCoverRateLiquidation = auto const kDestination = JTxFieldWrapper(sfDestination); -} // namespace loanBroker +} // namespace loan_broker /* Loan */ /******************************************************************************/ diff --git a/src/test/jtx/WSClient_test.cpp b/src/test/jtx/WSClient_test.cpp index d77e0f948b..801ca50504 100644 --- a/src/test/jtx/WSClient_test.cpp +++ b/src/test/jtx/WSClient_test.cpp @@ -13,8 +13,9 @@ class WSClient_test : public beast::unit_test::Suite { public: void - run() override + testSmoke() { + testcase("smoke"); using namespace jtx; Env env(*this); auto wsc = makeWSClient(env.app().config()); @@ -28,6 +29,47 @@ public: auto jv = wsc->getMsg(std::chrono::seconds(1)); pass(); } + + void + testGracefulDisconnect() + { + testcase("graceful disconnect"); + using namespace jtx; + using namespace std::chrono; + + Env env(*this); + auto wsc = makeWSClient(env.app().config()); + + // Put real traffic on the connection before closing it. + json::Value stream; + stream["streams"] = json::ValueType::Array; + stream["streams"].append("ledger"); + auto const sub = wsc->invoke("subscribe", stream); + BEAST_EXPECT(sub.isMember("result") || sub.isMember("status")); + + // disconnect() performs a graceful WebSocket closing handshake and + // blocks until the server acknowledges. On loopback that completes in + // well under its internal 1s timeout; only a broken async_close/ack + // coordination would fall through to the force-close path at ~1s. A + // generous bound keeps this from flaking under load while still + // catching that regression. + auto const start = steady_clock::now(); + wsc->disconnect(); + auto const elapsed = duration_cast(steady_clock::now() - start); + BEAST_EXPECT(elapsed < milliseconds{750}); + + // disconnect() must be idempotent: a second call (and the subsequent + // destructor) must not hang, double-close, or crash. + wsc->disconnect(); + pass(); + } + + void + run() override + { + testSmoke(); + testGracefulDisconnect(); + } }; BEAST_DEFINE_TESTSUITE(WSClient, jtx, xrpl); diff --git a/src/test/jtx/impl/AMM.cpp b/src/test/jtx/impl/AMM.cpp index 8effce288e..74232037ce 100644 --- a/src/test/jtx/impl/AMM.cpp +++ b/src/test/jtx/impl/AMM.cpp @@ -234,7 +234,7 @@ AMM::ammRpcInfo( jv[jss::amm_account] = *ammAccount; } auto jr = - (apiVersion == RPC::kApiInvalidVersion + (apiVersion == rpc::kApiInvalidVersion ? env_.rpc("json", "amm_info", to_string(jv)) : env_.rpc(apiVersion, "json", "amm_info", to_string(jv))); if (jr.isObject() && jr.isMember(jss::result) && jr[jss::result].isMember(jss::status)) diff --git a/src/test/jtx/impl/Env.cpp b/src/test/jtx/impl/Env.cpp index 4da2e2b521..35553bdeb2 100644 --- a/src/test/jtx/impl/Env.cpp +++ b/src/test/jtx/impl/Env.cpp @@ -498,9 +498,9 @@ Env::postconditions( !test.expect( parsed.rpcCode == jt.rpcCode->first && parsed.rpcMessage == jt.rpcCode->second, "apply " + locStr + ": Got RPC result "s + - (parsed.rpcCode ? RPC::getErrorInfo(*parsed.rpcCode).token.cStr() : "NO RESULT") + + (parsed.rpcCode ? rpc::getErrorInfo(*parsed.rpcCode).token.cStr() : "NO RESULT") + " (" + parsed.rpcMessage + "); Expected " + - RPC::getErrorInfo(jt.rpcCode->first).token.cStr() + " (" + jt.rpcCode->second + + rpc::getErrorInfo(jt.rpcCode->first).token.cStr() + " (" + jt.rpcCode->second + ")")) || bad; // If we have an rpcCode (just checked), then the rpcException check is diff --git a/src/test/jtx/impl/JSONRPCClient.cpp b/src/test/jtx/impl/JSONRPCClient.cpp index 495fc5a657..06474c3616 100644 --- a/src/test/jtx/impl/JSONRPCClient.cpp +++ b/src/test/jtx/impl/JSONRPCClient.cpp @@ -14,18 +14,23 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include #include #include #include +#include +#include +#include #include #include #include @@ -84,6 +89,40 @@ class JSONRPCClient : public AbstractClient boost::beast::multi_buffer bout_; unsigned rpcVersion_; + bool disconnected_ = false; + + // Errors that mean the persistent keep-alive connection was dropped by the + // server (rather than a genuine protocol failure), so the request can be + // safely retried on a fresh connection. + static bool + droppedConnection(boost::system::error_code const& ec) + { + namespace error = boost::asio::error; + static auto const kDroppedConnectionErrors = std::to_array({ + boost::beast::http::error::end_of_stream, + error::eof, + error::connection_reset, + error::connection_aborted, + error::broken_pipe, + error::not_connected, + }); + + return std::ranges::any_of( + kDroppedConnectionErrors, + [&ec](boost::system::error_code const& e) { return ec == e; }); + } + + // Tear down and re-establish the socket to ep_, discarding any buffered + // bytes left over from the dropped connection. + void + reconnect() + { + boost::system::error_code ec; + stream_.close(ec); + bin_.clear(); + stream_.connect(ep_); + } + public: explicit JSONRPCClient(Config const& cfg, unsigned rpcVersion) : ep_(getEndpoint(cfg)), stream_(ios_), rpcVersion_(rpcVersion) @@ -91,12 +130,10 @@ public: stream_.connect(ep_); } - /* - Return value is an Object type with up to three keys: - status - error - result - */ + // Return value is an Object type with up to three keys: + // status + // error + // result json::Value invoke(std::string const& cmd, json::Value const& params) override { @@ -104,6 +141,13 @@ public: using namespace boost::asio; using namespace std::string_literals; + // Once disconnect() has released the slot, the client must not be + // reused (see AbstractClient::disconnect). Refuse rather than let the + // failed write/read below trip the reconnect path and silently + // re-consume a connection slot, which would defeat disconnectClient(). + if (disconnected_) + Throw("JSONRPCClient::invoke called after disconnect()"); + request req; req.method(boost::beast::http::verb::post); req.target("/"); @@ -131,10 +175,29 @@ public: req.body() = to_string(jr); } req.prepare_payload(); - write(stream_, req); + // The client keeps a single keep-alive connection for its whole + // lifetime, but the server drops idle localhost connections after a few + // seconds (BaseHTTPPeer::kTimeoutSecondsLocal). If a slow gap between + // requests let the server close the socket, the write/read here fails + // with end_of_stream; reconnect and retry the request exactly once. response res; - read(stream_, bin_, res); + auto writeAndRead = [&] { + write(stream_, req); + read(stream_, bin_, res); + }; + try + { + writeAndRead(); + } + catch (boost::system::system_error const& e) + { + if (!droppedConnection(e.code())) + throw; + reconnect(); + res = {}; + writeAndRead(); + } json::Reader jr; json::Value jv; @@ -151,6 +214,19 @@ public: { return rpcVersion_; } + + void + disconnect() override + { + if (disconnected_) + return; + + disconnected_ = true; + + boost::system::error_code ec; + stream_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); + stream_.close(ec); + } }; std::unique_ptr diff --git a/src/test/jtx/impl/TestHelpers.cpp b/src/test/jtx/impl/TestHelpers.cpp index 4d3869b4f9..d73eb8adf4 100644 --- a/src/test/jtx/impl/TestHelpers.cpp +++ b/src/test/jtx/impl/TestHelpers.cpp @@ -211,10 +211,10 @@ findPathsRequest( using namespace jtx; auto& app = env.app(); - Resource::Charge loadType = Resource::kFeeReferenceRpc; - Resource::Consumer c; + resource::Charge loadType = resource::kFeeReferenceRpc; + resource::Consumer c; - RPC::JsonContext context{ + rpc::JsonContext context{ {.j = env.journal, .app = app, .loadType = loadType, @@ -224,7 +224,7 @@ findPathsRequest( .role = Role::USER, .coro = {}, .infoSub = {}, - .apiVersion = RPC::kApiVersionIfUnspecified}, + .apiVersion = rpc::kApiVersionIfUnspecified}, {}, {}}; @@ -252,7 +252,7 @@ findPathsRequest( app.getJobQueue().postCoro(JtClient, "RPC-Client", [&](auto const& coro) { context.params = std::move(params); context.coro = coro; - RPC::doCommand(context, result); + rpc::doCommand(context, result); g.signal(); }); @@ -743,7 +743,7 @@ issueHelperMPT(IssuerArgs const& args) /* LoanBroker */ /******************************************************************************/ -namespace loanBroker { +namespace loan_broker { json::Value set(AccountID const& account, uint256 const& vaultId, uint32_t flags) @@ -809,7 +809,7 @@ coverClawback(AccountID const& account, std::uint32_t flags) return jv; } -} // namespace loanBroker +} // namespace loan_broker /* Loan */ /******************************************************************************/ diff --git a/src/test/jtx/impl/WSClient.cpp b/src/test/jtx/impl/WSClient.cpp index ca322415fb..a8702c12d9 100644 --- a/src/test/jtx/impl/WSClient.cpp +++ b/src/test/jtx/impl/WSClient.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -112,10 +113,11 @@ class WSClientImpl : public WSClient bool peerClosed_ = false; - // synchronize destructor - bool b0_ = false; - std::mutex m0_; - std::condition_variable cv0_; + // disconnect() waits on this until the read loop ends (for any reason: + // the server acknowledged our close, or a timeout force-closed the socket). + static constexpr auto kDisconnectTimeout = std::chrono::seconds{1}; + xrpl::Mutex readEnded_; + std::condition_variable readEndCv_; // synchronize message queue std::mutex m_; @@ -127,23 +129,26 @@ class WSClientImpl : public WSClient void cleanup() { - boost::asio::post(ios_, boost::asio::bind_executor(strand_, [this] { - if (!peerClosed_) - { - ws_.async_close( - {}, boost::asio::bind_executor(strand_, [&](error_code) { - try - { - stream_.cancel(); - } - // NOLINTNEXTLINE(bugprone-empty-catch) - catch (boost::system::system_error const&) - { - // ignored - } - })); - } - })); + boost::asio::post( + ios_, // + boost::asio::bind_executor(strand_, [this] { + if (!peerClosed_) + { + ws_.async_close( + {}, // + boost::asio::bind_executor(strand_, [&](error_code) { + try + { + stream_.cancel(); + } + // NOLINTNEXTLINE(bugprone-empty-catch) + catch (boost::system::system_error const&) + { + // ignored + } + })); + } + })); work_ = std::nullopt; thread_.join(); } @@ -289,6 +294,44 @@ public: return rpcVersion_; } + void + disconnect() override + { + // Perform a graceful WebSocket closing handshake and block until the + // read loop ends, so the server observes a clean close (not a RST) and + // has finished tearing the connection down by the time we return. + // If the server already closed, the wait below returns immediately. + boost::asio::post( + ios_, + boost::asio::bind_executor( + strand_, // + [this] { + if (!peerClosed_) + { + ws_.async_close( + boost::beast::websocket::close_code::normal, + boost::asio::bind_executor(strand_, [](error_code) {})); + } + })); + + auto lock = readEnded_.lock(); + readEndCv_.wait_for(lock, kDisconnectTimeout, [&lock] { return *lock; }); + + // On timeout (server gone or not replying) force the socket closed so + // the outstanding read ends and the worker thread can later be joined. + if (!*lock) + { + boost::asio::post( + ios_, + boost::asio::bind_executor( + strand_, // + [this] { + boost::system::error_code ec; + stream_.close(ec); + })); + } + } + private: void onReadMsg(error_code const& ec) @@ -297,33 +340,31 @@ private: { if (ec == boost::beast::websocket::error::closed) peerClosed_ = true; + + *readEnded_.lock() = true; + readEndCv_.notify_all(); + return; } json::Value jv; json::Reader jr; + jr.parse(bufferString(rb_.data()), jv); rb_.consume(rb_.size()); + auto m = std::make_shared(std::move(jv)); { std::scoped_lock const lock(m_); msgs_.push_front(m); cv_.notify_all(); } + ws_.async_read( rb_, boost::asio::bind_executor(strand_, [this](error_code const& ec, std::size_t) { onReadMsg(ec); })); } - - // Called when the read op terminates - void - onReadDone() - { - std::scoped_lock const lock(m0_); - b0_ = true; - cv0_.notify_all(); - } }; std::unique_ptr diff --git a/src/test/jtx/impl/attester.cpp b/src/test/jtx/impl/attester.cpp index ac946a1bf3..3799d957e9 100644 --- a/src/test/jtx/impl/attester.cpp +++ b/src/test/jtx/impl/attester.cpp @@ -24,7 +24,7 @@ signClaimAttestation( std::uint64_t claimID, std::optional const& dst) { - auto const toSign = Attestations::AttestationClaim::message( + auto const toSign = attestations::AttestationClaim::message( bridge, sendingAccount, sendingAmount, rewardAccount, wasLockingChainSend, claimID, dst); return sign(pk, sk, makeSlice(toSign)); } @@ -42,7 +42,7 @@ signCreateAccountAttestation( std::uint64_t createCount, AccountID const& dst) { - auto const toSign = Attestations::AttestationCreateAccount::message( + auto const toSign = attestations::AttestationCreateAccount::message( bridge, sendingAccount, sendingAmount, diff --git a/src/test/jtx/impl/ledgerStateFixes.cpp b/src/test/jtx/impl/ledgerStateFixes.cpp index 30c6659124..ae195021b8 100644 --- a/src/test/jtx/impl/ledgerStateFixes.cpp +++ b/src/test/jtx/impl/ledgerStateFixes.cpp @@ -9,7 +9,7 @@ #include -namespace xrpl::test::jtx::ledgerStateFix { +namespace xrpl::test::jtx::ledger_state_fix { // Fix NFTokenPage links on owner's account. acct pays fee. json::Value @@ -35,4 +35,4 @@ bookExchangeRate(jtx::Account const& acct, uint256 const& bookDir) return jv; } -} // namespace xrpl::test::jtx::ledgerStateFix +} // namespace xrpl::test::jtx::ledger_state_fix diff --git a/src/test/jtx/ledgerStateFix.h b/src/test/jtx/ledgerStateFix.h index 2fe5c8accc..4ae22f891e 100644 --- a/src/test/jtx/ledgerStateFix.h +++ b/src/test/jtx/ledgerStateFix.h @@ -8,7 +8,7 @@ /** * LedgerStateFix operations. */ -namespace xrpl::test::jtx::ledgerStateFix { +namespace xrpl::test::jtx::ledger_state_fix { /** * Repair the links in an NFToken directory. @@ -22,4 +22,4 @@ nftPageLinks(jtx::Account const& acct, jtx::Account const& owner); json::Value bookExchangeRate(jtx::Account const& acct, uint256 const& bookDir); -} // namespace xrpl::test::jtx::ledgerStateFix +} // namespace xrpl::test::jtx::ledger_state_fix diff --git a/src/test/jtx/rpc.h b/src/test/jtx/rpc.h index 9bd99c15f8..7fd550563c 100644 --- a/src/test/jtx/rpc.h +++ b/src/test/jtx/rpc.h @@ -48,7 +48,7 @@ public: jt.ter = telENV_RPC_FAILED; if (code_) { - auto const& errorInfo = RPC::getErrorInfo(*code_); + auto const& errorInfo = rpc::getErrorInfo(*code_); // When an RPC request returns an error code ('error_code'), it // always includes an error message ('error_message'), and sometimes // includes an error token ('error'). If it does, the error token is diff --git a/src/test/nodestore/Backend_test.cpp b/src/test/nodestore/Backend_test.cpp deleted file mode 100644 index 65601b0cf5..0000000000 --- a/src/test/nodestore/Backend_test.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -namespace xrpl::NodeStore { - -// Tests the Backend interface -// -class Backend_test : public TestBase -{ -public: - void - testBackend(std::string const& type, std::uint64_t const seedValue, int numObjsToTest = 2000) - { - DummyScheduler scheduler; - - testcase("Backend type=" + type); - - Section params; - beast::TempDir const tempDir; - params.set(Keys::kType, type); - params.set(Keys::kPath, tempDir.path()); - - beast::xor_shift_engine rng(seedValue); - - // Create a batch - auto batch = createPredictableBatch(numObjsToTest, rng()); - - using beast::Severity; - test::SuiteJournal journal("Backend_test", *this); - - { - // Open the backend - std::unique_ptr backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - backend->open(); - - // Write the batch - storeBatch(*backend, batch); - - { - // Read it back in - Batch copy; - fetchCopyOfBatch(*backend, ©, batch); - BEAST_EXPECT(areBatchesEqual(batch, copy)); - } - - { - // Reorder and read the copy again - std::shuffle(batch.begin(), batch.end(), rng); - Batch copy; - fetchCopyOfBatch(*backend, ©, batch); - BEAST_EXPECT(areBatchesEqual(batch, copy)); - } - } - - { - // Re-open the backend - std::unique_ptr backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - backend->open(); - - // Read it back in - Batch copy; - fetchCopyOfBatch(*backend, ©, batch); - // Canonicalize the source and destination batches - std::ranges::sort(batch, LessThan{}); - std::ranges::sort(copy, LessThan{}); - BEAST_EXPECT(areBatchesEqual(batch, copy)); - } - } - - //-------------------------------------------------------------------------- - - void - run() override - { - std::uint64_t const seedValue = 50; - - testBackend("nudb", seedValue); - -#if XRPL_ROCKSDB_AVAILABLE - testBackend("rocksdb", seedValue); -#endif - -#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS - testBackend("sqlite", seedValue); -#endif - } -}; - -BEAST_DEFINE_TESTSUITE(Backend, nodestore, xrpl); - -} // namespace xrpl::NodeStore diff --git a/src/test/nodestore/Basics_test.cpp b/src/test/nodestore/Basics_test.cpp deleted file mode 100644 index 7d77b18630..0000000000 --- a/src/test/nodestore/Basics_test.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include - -#include -#include -#include -#include - -#include -#include - -namespace xrpl::NodeStore { - -// Tests predictable batches, and NodeObject blob encoding -// -class NodeStoreBasic_test : public TestBase -{ -public: - // Make sure predictable object generation works! - void - testBatches(std::uint64_t const seedValue) - { - testcase("batch"); - - auto batch1 = createPredictableBatch(kNumObjectsToTest, seedValue); - - auto batch2 = createPredictableBatch(kNumObjectsToTest, seedValue); - - BEAST_EXPECT(areBatchesEqual(batch1, batch2)); - - auto batch3 = createPredictableBatch(kNumObjectsToTest, seedValue + 1); - - BEAST_EXPECT(!areBatchesEqual(batch1, batch3)); - } - - // Checks encoding/decoding blobs - void - testBlobs(std::uint64_t const seedValue) - { - testcase("encoding"); - - auto batch = createPredictableBatch(kNumObjectsToTest, seedValue); - - for (auto const& expected : batch) - { - EncodedBlob const encoded(expected); - - DecodedBlob decoded(encoded.getKey(), encoded.getData(), encoded.getSize()); - - BEAST_EXPECT(decoded.wasOk()); - - if (decoded.wasOk()) - { - std::shared_ptr const object(decoded.createObject()); - - BEAST_EXPECT(isSame(expected, object)); - } - } - } - - void - run() override - { - std::uint64_t const seedValue = 50; - - testBatches(seedValue); - - testBlobs(seedValue); - } -}; - -BEAST_DEFINE_TESTSUITE(NodeStoreBasic, nodestore, xrpl); - -} // namespace xrpl::NodeStore diff --git a/src/test/nodestore/Database_test.cpp b/src/test/nodestore/DatabaseConfig_test.cpp similarity index 59% rename from src/test/nodestore/Database_test.cpp rename to src/test/nodestore/DatabaseConfig_test.cpp index bb8ec7d4fd..1f7f0f67bd 100644 --- a/src/test/nodestore/Database_test.cpp +++ b/src/test/nodestore/DatabaseConfig_test.cpp @@ -1,44 +1,21 @@ #include #include #include -#include -#include #include -#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include #include -#include -#include #include -namespace xrpl::NodeStore { +namespace xrpl::node_store { -class Database_test : public TestBase +class DatabaseConfig_test : public beast::unit_test::Suite { - test::SuiteJournal journal_; - public: - Database_test() : journal_("Database_test", *this) - { - } - void testConfig() { @@ -73,8 +50,8 @@ public: Env env = [&]() { auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSafetyLevel, "high"); + auto& section = p->section("sqlite"); + section.set("safety_level", "high"); } p->ledgerHistory = 100'000'000; @@ -102,8 +79,8 @@ public: Env env = [&]() { auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSafetyLevel, "low"); + auto& section = p->section("sqlite"); + section.set("safety_level", "low"); } p->ledgerHistory = 100'000'000; @@ -131,10 +108,10 @@ public: Env env = [&]() { auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kJournalMode, "off"); - section.set(Keys::kSynchronous, "extra"); - section.set(Keys::kTempStore, "default"); + auto& section = p->section("sqlite"); + section.set("journal_mode", "off"); + section.set("synchronous", "extra"); + section.set("temp_store", "default"); } return Env( @@ -145,7 +122,7 @@ public: }(); // No warning, even though higher risk settings were used because - // LEDGER_HISTORY is small + // ledgerHistory is small BEAST_EXPECT(!found); auto const s = setupDatabaseCon(env.app().config()); if (BEAST_EXPECT(s.globalPragma->size() == 3)) @@ -163,10 +140,10 @@ public: Env env = [&]() { auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kJournalMode, "off"); - section.set(Keys::kSynchronous, "extra"); - section.set(Keys::kTempStore, "default"); + auto& section = p->section("sqlite"); + section.set("journal_mode", "off"); + section.set("synchronous", "extra"); + section.set("temp_store", "default"); } p->ledgerHistory = 50'000'000; @@ -178,7 +155,7 @@ public: }(); // No warning, even though higher risk settings were used because - // LEDGER_HISTORY is small + // ledgerHistory is small BEAST_EXPECT(found); auto const s = setupDatabaseCon(env.app().config()); if (BEAST_EXPECT(s.globalPragma->size() == 3)) @@ -199,11 +176,11 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSafetyLevel, "low"); - section.set(Keys::kJournalMode, "off"); - section.set(Keys::kSynchronous, "extra"); - section.set(Keys::kTempStore, "default"); + auto& section = p->section("sqlite"); + section.set("safety_level", "low"); + section.set("journal_mode", "off"); + section.set("synchronous", "extra"); + section.set("temp_store", "default"); } try @@ -230,9 +207,9 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSafetyLevel, "high"); - section.set(Keys::kJournalMode, "off"); + auto& section = p->section("sqlite"); + section.set("safety_level", "high"); + section.set("journal_mode", "off"); } try @@ -259,9 +236,9 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSafetyLevel, "low"); - section.set(Keys::kSynchronous, "extra"); + auto& section = p->section("sqlite"); + section.set("safety_level", "low"); + section.set("synchronous", "extra"); } try @@ -288,9 +265,9 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSafetyLevel, "high"); - section.set(Keys::kTempStore, "default"); + auto& section = p->section("sqlite"); + section.set("safety_level", "high"); + section.set("temp_store", "default"); } try @@ -317,8 +294,8 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSafetyLevel, "slow"); + auto& section = p->section("sqlite"); + section.set("safety_level", "slow"); } try @@ -345,8 +322,8 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kJournalMode, "fast"); + auto& section = p->section("sqlite"); + section.set("journal_mode", "fast"); } try @@ -373,8 +350,8 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kSynchronous, "instant"); + auto& section = p->section("sqlite"); + section.set("synchronous", "instant"); } try @@ -401,8 +378,8 @@ public: auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kTempStore, "network"); + auto& section = p->section("sqlite"); + section.set("temp_store", "network"); } try @@ -436,9 +413,9 @@ public: Env env = [&]() { auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kPageSize, "512"); - section.set(Keys::kJournalSizeLimit, "2582080"); + auto& section = p->section("sqlite"); + section.set("page_size", "512"); + section.set("journal_size_limit", "2582080"); } return Env(*this, std::move(p)); }(); @@ -457,8 +434,8 @@ public: bool found = false; auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kPageSize, "256"); + auto& section = p->section("sqlite"); + section.set("page_size", "256"); } try { @@ -480,8 +457,8 @@ public: bool found = false; auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kPageSize, "131072"); + auto& section = p->section("sqlite"); + section.set("page_size", "131072"); } try { @@ -503,8 +480,8 @@ public: bool found = false; auto p = test::jtx::envconfig(); { - auto& section = p->section(Sections::kSqlite); - section.set(Keys::kPageSize, "513"); + auto& section = p->section("sqlite"); + section.set("page_size", "513"); } try { @@ -522,208 +499,13 @@ public: } } - //-------------------------------------------------------------------------- - - void - testImport( - std::string const& destBackendType, - std::string const& srcBackendType, - std::int64_t seedValue) - { - DummyScheduler scheduler; - - beast::TempDir const nodeDb; - Section srcParams; - srcParams.set(Keys::kType, srcBackendType); - srcParams.set(Keys::kPath, nodeDb.path()); - - // Create a batch - auto batch = createPredictableBatch(kNumObjectsToTest, seedValue); - - // Write to source db - { - std::unique_ptr src = - Manager::instance().makeDatabase(megabytes(4), scheduler, 2, srcParams, journal_); - storeBatch(*src, batch); - } - - Batch copy; - - { - // Re-open the db - std::unique_ptr src = - Manager::instance().makeDatabase(megabytes(4), scheduler, 2, srcParams, journal_); - - // Set up the destination database - beast::TempDir const destDb; - Section destParams; - destParams.set(Keys::kType, destBackendType); - destParams.set(Keys::kPath, destDb.path()); - - std::unique_ptr dest = - Manager::instance().makeDatabase(megabytes(4), scheduler, 2, destParams, journal_); - - testcase("import into '" + destBackendType + "' from '" + srcBackendType + "'"); - - // Do the import - dest->importDatabase(*src); - - // Get the results of the import - fetchCopyOfBatch(*dest, ©, batch); - } - - // Canonicalize the source and destination batches - std::ranges::sort(batch, LessThan{}); - std::ranges::sort(copy, LessThan{}); - BEAST_EXPECT(areBatchesEqual(batch, copy)); - } - - //-------------------------------------------------------------------------- - - void - testNodeStore( - std::string const& type, - bool const testPersistence, - std::int64_t const seedValue, - int numObjsToTest = 2000) - { - DummyScheduler scheduler; - - std::string const s = "NodeStore backend '" + type + "'"; - - testcase(s); - - beast::TempDir const nodeDb; - Section nodeParams; - nodeParams.set(Keys::kType, type); - nodeParams.set(Keys::kPath, nodeDb.path()); - - beast::xor_shift_engine rng(seedValue); - - // Create a batch - auto batch = createPredictableBatch(numObjsToTest, rng()); - - { - // Open the database - std::unique_ptr db = - Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal_); - - // Write the batch - storeBatch(*db, batch); - - { - // Read it back in - Batch copy; - fetchCopyOfBatch(*db, ©, batch); - BEAST_EXPECT(areBatchesEqual(batch, copy)); - } - - { - // Reorder and read the copy again - std::shuffle(batch.begin(), batch.end(), rng); - Batch copy; - fetchCopyOfBatch(*db, ©, batch); - BEAST_EXPECT(areBatchesEqual(batch, copy)); - } - } - - if (testPersistence) - { - // Re-open the database without the ephemeral DB - std::unique_ptr db = - Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal_); - - // Read it back in - Batch copy; - fetchCopyOfBatch(*db, ©, batch); - - // Canonicalize the source and destination batches - std::ranges::sort(batch, LessThan{}); - std::ranges::sort(copy, LessThan{}); - BEAST_EXPECT(areBatchesEqual(batch, copy)); - } - - if (type == "memory") - { - // Verify default earliest ledger sequence - { - std::unique_ptr db = Manager::instance().makeDatabase( - megabytes(4), scheduler, 2, nodeParams, journal_); - BEAST_EXPECT(db->earliestLedgerSeq() == kXrpLedgerEarliestSeq); - } - - // Set an invalid earliest ledger sequence - try - { - nodeParams.set(Keys::kEarliestSeq, "0"); - std::unique_ptr const db = Manager::instance().makeDatabase( - megabytes(4), scheduler, 2, nodeParams, journal_); - } - catch (std::runtime_error const& e) - { - BEAST_EXPECT(std::strcmp(e.what(), "Invalid earliest_seq") == 0); - } - - { - // Set a valid earliest ledger sequence - nodeParams.set(Keys::kEarliestSeq, "1"); - std::unique_ptr db = Manager::instance().makeDatabase( - megabytes(4), scheduler, 2, nodeParams, journal_); - - // Verify database uses the earliest ledger sequence setting - BEAST_EXPECT(db->earliestLedgerSeq() == 1); - } - - // Create another database that attempts to set the value again - try - { - // Set to default earliest ledger sequence - nodeParams.set(Keys::kEarliestSeq, std::to_string(kXrpLedgerEarliestSeq)); - std::unique_ptr const db2 = Manager::instance().makeDatabase( - megabytes(4), scheduler, 2, nodeParams, journal_); - } - catch (std::runtime_error const& e) - { - BEAST_EXPECT(std::strcmp(e.what(), "earliest_seq set more than once") == 0); - } - } - } - - //-------------------------------------------------------------------------- - void run() override { - std::int64_t const seedValue = 50; - testConfig(); - - testNodeStore("memory", false, seedValue); - - // Persistent backend tests - { - testNodeStore("nudb", true, seedValue); - -#if XRPL_ROCKSDB_AVAILABLE - testNodeStore("rocksdb", true, seedValue); -#endif - } - - // Import tests - { - testImport("nudb", "nudb", seedValue); - -#if XRPL_ROCKSDB_AVAILABLE - testImport("rocksdb", "rocksdb", seedValue); -#endif - -#if XRPL_ENABLE_SQLITE_BACKEND_TESTS - testImport("sqlite", "sqlite", seedValue); -#endif - } } }; -BEAST_DEFINE_TESTSUITE(Database, nodestore, xrpl); +BEAST_DEFINE_TESTSUITE(DatabaseConfig, nodestore, xrpl); -} // namespace xrpl::NodeStore +} // namespace xrpl::node_store diff --git a/src/test/nodestore/NuDBFactory_test.cpp b/src/test/nodestore/NuDBFactory_test.cpp deleted file mode 100644 index d0675b3893..0000000000 --- a/src/test/nodestore/NuDBFactory_test.cpp +++ /dev/null @@ -1,443 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace xrpl::NodeStore { - -class NuDBFactory_test : public TestBase -{ -private: - // Helper function to create a Section with specified parameters - static Section - createSection(std::string const& path, std::string const& blockSize = "") - { - Section params; - params.set(Keys::kType, "nudb"); - params.set(Keys::kPath, path); - if (!blockSize.empty()) - params.set(Keys::kNudbBlockSize, blockSize); - return params; - } - - // Helper function to create a backend and test basic functionality - bool - testBackendFunctionality(Section const& params, std::size_t expectedBlocksize) - { - try - { - DummyScheduler scheduler; - test::SuiteJournal journal("NuDBFactory_test", *this); - - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - - if (!BEAST_EXPECT(backend)) - return false; - - if (!BEAST_EXPECT(backend->getBlockSize() == expectedBlocksize)) - return false; - - backend->open(); - - if (!BEAST_EXPECT(backend->isOpen())) - return false; - - // Test basic store/fetch functionality - auto batch = createPredictableBatch(10, 12345); - storeBatch(*backend, batch); - - Batch copy; - fetchCopyOfBatch(*backend, ©, batch); - - backend->close(); - - return areBatchesEqual(batch, copy); - } - catch (...) - { - return false; - } - } - - // Helper function to test log messages - void - testLogMessage(Section const& params, beast::Severity level, std::string const& expectedMessage) - { - test::StreamSink sink(level); - beast::Journal const journal(sink); - - DummyScheduler scheduler; - auto backend = Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - - std::string const logOutput = sink.messages().str(); - BEAST_EXPECT(logOutput.contains(expectedMessage)); - } - - // Helper function to test power of two validation - void - testPowerOfTwoValidation(std::string const& size, bool shouldWork) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), size); - - test::StreamSink sink(beast::Severity::Warning); - beast::Journal const journal(sink); - - DummyScheduler scheduler; - auto backend = Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - - std::string const logOutput = sink.messages().str(); - bool const hasWarning = logOutput.contains("Invalid nudb_block_size"); - - BEAST_EXPECT(hasWarning == !shouldWork); - } - -public: - void - testDefaultBlockSize() - { - testcase("Default block size (no nudb_block_size specified)"); - - beast::TempDir const tempDir; - auto params = createSection(tempDir.path()); - - // Should work with default 4096 block size - BEAST_EXPECT(testBackendFunctionality(params, 4096)); - } - - void - testValidBlockSizes() - { - testcase("Valid block sizes"); - - std::vector const validSizes = {4096, 8192, 16384, 32768}; - - for (auto const& size : validSizes) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), to_string(size)); - - BEAST_EXPECT(testBackendFunctionality(params, size)); - } - // Empty value is ignored by the config parser, so uses the - // default - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), ""); - - BEAST_EXPECT(testBackendFunctionality(params, 4096)); - } - - void - testInvalidBlockSizes() - { - testcase("Invalid block sizes"); - - std::vector const invalidSizes = { - "2048", // Too small - "1024", // Too small - "65536", // Too large - "131072", // Too large - "5000", // Not power of 2 - "6000", // Not power of 2 - "10000", // Not power of 2 - "0", // Zero - "-1", // Negative - "abc", // Non-numeric - "4k", // Invalid format - "4096.5" // Decimal - }; - - for (auto const& size : invalidSizes) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), size); - - // Fails - BEAST_EXPECT(!testBackendFunctionality(params, 4096)); - } - - // Test whitespace cases separately since lexical_cast may handle them - std::vector const whitespaceInvalidSizes = { - "4096 ", // Trailing space - might be handled by lexical_cast - " 4096" // Leading space - might be handled by lexical_cast - }; - - for (auto const& size : whitespaceInvalidSizes) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), size); - - // Fails - BEAST_EXPECT(!testBackendFunctionality(params, 4096)); - } - } - - void - testLogMessages() - { - testcase("Log message verification"); - - // Test valid custom block size logging - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), "8192"); - - testLogMessage(params, beast::Severity::Info, "Using custom NuDB block size: 8192"); - } - - // Test invalid block size failure - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), "5000"); - - test::StreamSink sink(beast::Severity::Warning); - beast::Journal const journal(sink); - - DummyScheduler scheduler; - try - { - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - fail(); - } - catch (std::exception const& e) - { - std::string const logOutput{e.what()}; - BEAST_EXPECT(logOutput.contains("Invalid nudb_block_size: 5000")); - BEAST_EXPECT(logOutput.contains("Must be power of 2 between 4096 and 32768")); - } - } - - // Test non-numeric value failure - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), "invalid"); - - test::StreamSink sink(beast::Severity::Warning); - beast::Journal const journal(sink); - - DummyScheduler scheduler; - try - { - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - - fail(); - } - catch (std::exception const& e) - { - std::string const logOutput{e.what()}; - BEAST_EXPECT(logOutput.contains("Invalid nudb_block_size value: invalid")); - } - } - } - - void - testPowerOfTwoValidation() - { - testcase("Power of 2 validation logic"); - - // Test edge cases around valid range - std::vector> const testCases = { - {"4095", false}, // Just below minimum - {"4096", true}, // Minimum valid - {"4097", false}, // Just above minimum, not power of 2 - {"8192", true}, // Valid power of 2 - {"8193", false}, // Just above valid power of 2 - {"16384", true}, // Valid power of 2 - {"32768", true}, // Maximum valid - {"32769", false}, // Just above maximum - {"65536", false} // Power of 2 but too large - }; - - for (auto const& [size, shouldWork] : testCases) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), size); - - // We test the validation logic by catching exceptions for invalid - // values - test::StreamSink sink(beast::Severity::Warning); - beast::Journal const journal(sink); - - DummyScheduler scheduler; - try - { - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - BEAST_EXPECT(shouldWork); - } - catch (std::exception const& e) - { - std::string const logOutput{e.what()}; - BEAST_EXPECT(logOutput.contains("Invalid nudb_block_size")); - } - } - } - - void - testBothConstructorVariants() - { - testcase("Both constructor variants work with custom block size"); - - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), "16384"); - - DummyScheduler scheduler; - test::SuiteJournal journal("NuDBFactory_test", *this); - - // Test first constructor (without nudb::context) - { - auto backend1 = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - BEAST_EXPECT(backend1 != nullptr); - BEAST_EXPECT(testBackendFunctionality(params, 16384)); - } - - // Test second constructor (with nudb::context) - // Note: This would require access to nudb::context, which might not be - // easily testable without more complex setup. For now, we test that - // the factory can create backends with the first constructor. - } - - void - testConfigurationParsing() - { - testcase("Configuration parsing edge cases"); - - // Test that whitespace is handled correctly - std::vector const validFormats = { - "8192" // Basic valid format - }; - - // Test whitespace handling separately since lexical_cast behavior may - // vary - std::vector const whitespaceFormats = { - " 8192", // Leading space - may or may not be handled by - // lexical_cast - "8192 " // Trailing space - may or may not be handled by - // lexical_cast - }; - - // Test basic valid format - for (auto const& format : validFormats) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), format); - - test::StreamSink sink(beast::Severity::Info); - beast::Journal const journal(sink); - - DummyScheduler scheduler; - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - - // Should log success message for valid values - std::string const logOutput = sink.messages().str(); - bool const hasSuccessMessage = logOutput.contains("Using custom NuDB block size"); - BEAST_EXPECT(hasSuccessMessage); - } - - // Test whitespace formats - these should work if lexical_cast handles - // them - for (auto const& format : whitespaceFormats) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), format); - - // Use a lower threshold to capture both info and warning messages - test::StreamSink sink(beast::Severity::Debug); - beast::Journal const journal(sink); - - DummyScheduler scheduler; - try - { - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - fail(); - } - catch (...) - { - // Fails - BEAST_EXPECT(!testBackendFunctionality(params, 8192)); - } - } - } - - void - testDataPersistence() - { - testcase("Data persistence with different block sizes"); - - std::vector const blockSizes = {"4096", "8192", "16384", "32768"}; - - for (auto const& size : blockSizes) - { - beast::TempDir const tempDir; - auto params = createSection(tempDir.path(), size); - - DummyScheduler scheduler; - test::SuiteJournal journal("NuDBFactory_test", *this); - - // Create test data - auto batch = createPredictableBatch(50, 54321); - - // Store data - { - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - backend->open(); - storeBatch(*backend, batch); - backend->close(); - } - - // Retrieve data in new backend instance - { - auto backend = - Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); - backend->open(); - - Batch copy; - fetchCopyOfBatch(*backend, ©, batch); - - BEAST_EXPECT(areBatchesEqual(batch, copy)); - backend->close(); - } - } - } - - void - run() override - { - testDefaultBlockSize(); - testValidBlockSizes(); - testInvalidBlockSizes(); - testLogMessages(); - testPowerOfTwoValidation(); - testBothConstructorVariants(); - testConfigurationParsing(); - testDataPersistence(); - } -}; - -BEAST_DEFINE_TESTSUITE(NuDBFactory, xrpl_core, xrpl); - -} // namespace xrpl::NodeStore diff --git a/src/test/nodestore/TestBase.h b/src/test/nodestore/TestBase.h deleted file mode 100644 index 235e76501f..0000000000 --- a/src/test/nodestore/TestBase.h +++ /dev/null @@ -1,202 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -namespace xrpl::NodeStore { - -/** - * Binary function that satisfies the strict-weak-ordering requirement. - * - * This compares the hashes of both objects and returns true if - * the first hash is considered to go before the second. - * - * @see std::sort - */ -struct LessThan -{ - bool - operator()(std::shared_ptr const& lhs, std::shared_ptr const& rhs) - const noexcept - { - return lhs->getHash() < rhs->getHash(); - } -}; - -/** - * Returns `true` if objects are identical. - */ -inline bool -isSame(std::shared_ptr const& lhs, std::shared_ptr const& rhs) -{ - return (lhs->getType() == rhs->getType()) && (lhs->getHash() == rhs->getHash()) && - (lhs->getData() == rhs->getData()); -} - -// Some common code for the unit tests -// -class TestBase : public beast::unit_test::Suite -{ -public: - // Tunable parameters - // - static std::size_t const kMinPayloadBytes = 1; - static std::size_t const kMaxPayloadBytes = 2000; - static int const kNumObjectsToTest = 2000; - -public: - // Create a predictable batch of objects - static Batch - createPredictableBatch(int numObjects, std::uint64_t seed) - { - Batch batch; - batch.reserve(numObjects); - - beast::xor_shift_engine rng(seed); - - for (int i = 0; i < numObjects; ++i) - { - NodeObjectType const type = [&] { - switch (randInt(rng, 3)) - { - case 0: - return NodeObjectType::Ledger; - case 1: - return NodeObjectType::AccountNode; - case 2: - return NodeObjectType::TransactionNode; - case 3: - default: - return NodeObjectType::Unknown; - } - }(); - - uint256 hash; - beast::rngfill(hash.begin(), hash.size(), rng); - - Blob blob(randInt(rng, kMinPayloadBytes, kMaxPayloadBytes)); - beast::rngfill(blob.data(), blob.size(), rng); - - batch.push_back(NodeObject::createObject(type, std::move(blob), hash)); - } - - return batch; - } - - // Compare two batches for equality - static bool - areBatchesEqual(Batch const& lhs, Batch const& rhs) - { - bool result = true; - - if (lhs.size() == rhs.size()) - { - for (int i = 0; i < lhs.size(); ++i) - { - if (!isSame(lhs[i], rhs[i])) - { - result = false; - break; - } - } - } - else - { - result = false; - } - - return result; - } - - // Store a batch in a backend - static void - storeBatch(Backend& backend, Batch const& batch) - { - for (auto const& object : batch) - { - backend.store(object); - } - } - - // Get a copy of a batch in a backend - void - fetchCopyOfBatch(Backend& backend, Batch* pCopy, Batch const& batch) - { - pCopy->clear(); - pCopy->reserve(batch.size()); - - for (auto const& expected : batch) - { - std::shared_ptr object; - - Status const status = backend.fetch(expected->getHash(), &object); - - BEAST_EXPECT(status == Status::Ok); - - if (status == Status::Ok) - { - BEAST_EXPECT(object != nullptr); - - pCopy->push_back(object); - } - } - } - - void - fetchMissing(Backend& backend, Batch const& batch) - { - for (auto const& expected : batch) - { - std::shared_ptr object; - - Status const status = backend.fetch(expected->getHash(), &object); - - BEAST_EXPECT(status == Status::NotFound); - } - } - - // Store all objects in a batch - static void - storeBatch(Database& db, Batch const& batch) - { - for (auto const& object : batch) - { - Blob data(object->getData()); - - db.store(object->getType(), std::move(data), object->getHash(), db.earliestLedgerSeq()); - } - } - - // Fetch all the hashes in one batch, into another batch. - static void - fetchCopyOfBatch(Database& db, Batch* pCopy, Batch const& batch) - { - pCopy->clear(); - pCopy->reserve(batch.size()); - - for (auto const& expected : batch) - { - std::shared_ptr const object = db.fetchNodeObject(expected->getHash(), 0); - - if (object != nullptr) - pCopy->push_back(object); - } - } -}; - -} // namespace xrpl::NodeStore diff --git a/src/test/nodestore/import_test.cpp b/src/test/nodestore/import_test.cpp index d8c4a96713..c30d77029d 100644 --- a/src/test/nodestore/import_test.cpp +++ b/src/test/nodestore/import_test.cpp @@ -195,7 +195,7 @@ fmtdur(std::chrono::duration const& d) } // namespace detail -namespace NodeStore { +namespace node_store { //------------------------------------------------------------------------------ @@ -552,5 +552,5 @@ BEAST_DEFINE_TESTSUITE_MANUAL(import, nodestore, xrpl); //------------------------------------------------------------------------------ -} // namespace NodeStore +} // namespace node_store } // namespace xrpl diff --git a/src/test/nodestore/varint_test.cpp b/src/test/nodestore/varint_test.cpp deleted file mode 100644 index 68e88d831a..0000000000 --- a/src/test/nodestore/varint_test.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include - -#include -#include -#include -#include - -namespace xrpl::NodeStore::tests { - -class varint_test : public beast::unit_test::Suite -{ -public: - void - testVarints(std::vector vv) - { - testcase("encode, decode"); - for (auto const v : vv) - { - std::array::kMax> vi{}; - auto const n0 = writeVarint(vi.data(), v); - expect(n0 > 0, "write error"); - expect(n0 == sizeVarint(v), "size error"); - std::size_t v1 = 0; - auto const n1 = readVarint(vi.data(), n0, v1); - expect(n1 == n0, "read error"); - expect(v == v1, "wrong value"); - } - } - - void - run() override - { - testVarints( - {0, - 1, - 2, - 126, - 127, - 128, - 253, - 254, - 255, - 16127, - 16128, - 16129, - 0xff, - 0xffff, - 0xffffffff, - 0xffffffffffffUL, - 0xffffffffffffffffUL}); - } -}; - -BEAST_DEFINE_TESTSUITE(varint, nodestore, xrpl); - -} // namespace xrpl::NodeStore::tests diff --git a/src/test/overlay/ProtocolVersion_test.cpp b/src/test/overlay/ProtocolVersion_test.cpp index 2fc8e4447d..e31a574502 100644 --- a/src/test/overlay/ProtocolVersion_test.cpp +++ b/src/test/overlay/ProtocolVersion_test.cpp @@ -63,8 +63,8 @@ public: negotiateProtocolVersion("RTXP/1.2, XRPL/2.0, XRPL/2.1") == makeProtocol(2, 1)); BEAST_EXPECT(negotiateProtocolVersion("XRPL/2.2") == makeProtocol(2, 2)); BEAST_EXPECT( - negotiateProtocolVersion("RTXP/1.2, XRPL/2.2, XRPL/2.3, XRPL/999.999") == - makeProtocol(2, 2)); + negotiateProtocolVersion("RTXP/1.2, XRPL/2.3, XRPL/2.4, XRPL/999.999") == + makeProtocol(2, 3)); BEAST_EXPECT(negotiateProtocolVersion("XRPL/999.999, WebSocket/1.0") == std::nullopt); BEAST_EXPECT(negotiateProtocolVersion("") == std::nullopt); } diff --git a/src/test/overlay/TMGetObjectByHash_test.cpp b/src/test/overlay/TMGetObjectByHash_test.cpp index f52220a90c..6c9105164a 100644 --- a/src/test/overlay/TMGetObjectByHash_test.cpp +++ b/src/test/overlay/TMGetObjectByHash_test.cpp @@ -44,7 +44,7 @@ using namespace jtx; * Test for TMGetObjectByHash reply size limiting. * * This verifies the fix that limits TMGetObjectByHash replies to - * Tuning::hardMaxReplyNodes to prevent excessive memory usage and + * tuning::hardMaxReplyNodes to prevent excessive memory usage and * potential DoS attacks from peers requesting large numbers of objects. */ class TMGetObjectByHash_test : public beast::unit_test::Suite @@ -61,11 +61,11 @@ class TMGetObjectByHash_test : public beast::unit_test::Suite public: PeerTest( Application& app, - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type&& request, PublicKey const& publicKey, ProtocolVersion protocol, - Resource::Consumer consumer, + resource::Consumer consumer, std::unique_ptr&& streamPtr, OverlayImpl& overlay) : PeerImp( @@ -133,8 +133,8 @@ class TMGetObjectByHash_test : public beast::unit_test::Suite auto streamPtr = std::make_unique(socket_type(env.app().getIOContext()), *context_); - beast::IP::Endpoint const local(boost::asio::ip::make_address("172.1.1.1"), 51235); - beast::IP::Endpoint const remote(boost::asio::ip::make_address("172.1.1.2"), 51235); + beast::ip::Endpoint const local(boost::asio::ip::make_address("172.1.1.1"), 51235); + beast::ip::Endpoint const remote(boost::asio::ip::make_address("172.1.1.2"), 51235); PublicKey const key(std::get<0>(randomKeyPair(KeyType::Ed25519))); auto consumer = overlay.resourceManager().newInboundEndpoint(remote); @@ -227,7 +227,7 @@ class TMGetObjectByHash_test : public beast::unit_test::Suite void run() override { - int const limit = static_cast(Tuning::kHardMaxReplyNodes); + int const limit = static_cast(tuning::kHardMaxReplyNodes); testReplyLimit(limit + 1, limit); testReplyLimit(limit, limit); testReplyLimit(limit - 1, limit - 1); diff --git a/src/test/overlay/compression_test.cpp b/src/test/overlay/compression_test.cpp index 60cc69a14f..a583a3aeab 100644 --- a/src/test/overlay/compression_test.cpp +++ b/src/test/overlay/compression_test.cpp @@ -413,7 +413,7 @@ public: return env; }; auto handshake = [&](int outboundEnable, int inboundEnable) { - beast::IP::Address const addr = boost::asio::ip::make_address("172.1.1.100"); + beast::ip::Address const addr = boost::asio::ip::make_address("172.1.1.100"); auto env = getEnv(outboundEnable); auto request = xrpl::makeRequest( diff --git a/src/test/overlay/reduce_relay_test.cpp b/src/test/overlay/reduce_relay_test.cpp index 2f42313037..77920007de 100644 --- a/src/test/overlay/reduce_relay_test.cpp +++ b/src/test/overlay/reduce_relay_test.cpp @@ -92,13 +92,13 @@ public: send(std::shared_ptr const& m) override { } - [[nodiscard]] beast::IP::Endpoint + [[nodiscard]] beast::ip::Endpoint getRemoteAddress() const override { return {}; } void - charge(Resource::Charge const& fee, std::string const& context = {}) override + charge(resource::Charge const& fee, std::string const& context = {}) override { } [[nodiscard]] bool @@ -1610,7 +1610,7 @@ vp_base_squelch_max_selected_peers=2 env_.app().config().compression = c.compression; }; auto handshake = [&](int outboundEnable, int inboundEnable) { - beast::IP::Address const addr = boost::asio::ip::make_address("172.1.1.100"); + beast::ip::Address const addr = boost::asio::ip::make_address("172.1.1.100"); setEnv(outboundEnable); auto request = xrpl::makeRequest( diff --git a/src/test/overlay/tx_reduce_relay_test.cpp b/src/test/overlay/tx_reduce_relay_test.cpp index 43f6ef2506..8626d3e19c 100644 --- a/src/test/overlay/tx_reduce_relay_test.cpp +++ b/src/test/overlay/tx_reduce_relay_test.cpp @@ -121,11 +121,11 @@ private: public: PeerTest( Application& app, - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type&& request, PublicKey const& publicKey, ProtocolVersion protocol, - Resource::Consumer consumer, + resource::Consumer consumer, std::unique_ptr&& streamPtr, OverlayImpl& overlay) : PeerImp( @@ -192,9 +192,9 @@ private: auto streamPtr = std::make_unique( socket_type(std::forward(env.app().getIOContext())), *context_); - beast::IP::Endpoint const local( + beast::ip::Endpoint const local( boost::asio::ip::make_address("172.1.1." + std::to_string(lid_))); - beast::IP::Endpoint const remote( + beast::ip::Endpoint const remote( boost::asio::ip::make_address("172.1.1." + std::to_string(rid_))); PublicKey const key(std::get<0>(randomKeyPair(KeyType::Ed25519))); auto consumer = overlay.resourceManager().newInboundEndpoint(remote); diff --git a/src/test/protocol/ApiVersion_test.cpp b/src/test/protocol/ApiVersion_test.cpp deleted file mode 100644 index c41fa6f6c0..0000000000 --- a/src/test/protocol/ApiVersion_test.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include - -namespace xrpl::test { -struct ApiVersion_test : beast::unit_test::Suite -{ - void - run() override - { - { - testcase("API versions invariants"); - - static_assert(RPC::kApiMinimumSupportedVersion <= RPC::kApiMaximumSupportedVersion); - static_assert(RPC::kApiMinimumSupportedVersion <= RPC::kApiMaximumValidVersion); - static_assert(RPC::kApiMaximumSupportedVersion <= RPC::kApiMaximumValidVersion); - static_assert(RPC::kApiBetaVersion <= RPC::kApiMaximumValidVersion); - - BEAST_EXPECT(true); - } - - { - // Update when we change versions - testcase("API versions"); - - static_assert(RPC::kApiMinimumSupportedVersion >= 1); - static_assert(RPC::kApiMinimumSupportedVersion < 2); - static_assert(RPC::kApiMaximumSupportedVersion >= 2); - static_assert(RPC::kApiMaximumSupportedVersion < 3); - static_assert(RPC::kApiMaximumValidVersion >= 3); - static_assert(RPC::kApiMaximumValidVersion < 4); - static_assert(RPC::kApiBetaVersion >= 3); - static_assert(RPC::kApiBetaVersion < 4); - - BEAST_EXPECT(true); - } - } -}; - -BEAST_DEFINE_TESTSUITE(ApiVersion, protocol, xrpl); - -} // namespace xrpl::test diff --git a/src/test/protocol/BuildInfo_test.cpp b/src/test/protocol/BuildInfo_test.cpp index 1741f45938..a669e3e292 100644 --- a/src/test/protocol/BuildInfo_test.cpp +++ b/src/test/protocol/BuildInfo_test.cpp @@ -11,7 +11,7 @@ public: { testcase("EncodeSoftwareVersion"); - auto encodedVersion = BuildInfo::encodeSoftwareVersion("1.2.3-b7"); + auto encodedVersion = build_info::encodeSoftwareVersion("1.2.3-b7"); // the first two bytes identify the particular implementation, 0x183B BEAST_EXPECT((encodedVersion & 0xFFFF'0000'0000'0000LLU) == 0x183B'0000'0000'0000LLU); @@ -25,15 +25,15 @@ public: // 01 if a beta BEAST_EXPECT((encodedVersion & 0x0000'0000'00C0'0000LLU) >> 22 == 0b01); // 10 if an RC - encodedVersion = BuildInfo::encodeSoftwareVersion("1.2.4-rc7"); + encodedVersion = build_info::encodeSoftwareVersion("1.2.4-rc7"); BEAST_EXPECT((encodedVersion & 0x0000'0000'00C0'0000LLU) >> 22 == 0b10); // 11 if neither an RC nor a beta - encodedVersion = BuildInfo::encodeSoftwareVersion("1.2.5"); + encodedVersion = build_info::encodeSoftwareVersion("1.2.5"); BEAST_EXPECT((encodedVersion & 0x0000'0000'00C0'0000LLU) >> 22 == 0b11); } // the next six bits: rc/beta number (1-63) - encodedVersion = BuildInfo::encodeSoftwareVersion("1.2.6-b63"); + encodedVersion = build_info::encodeSoftwareVersion("1.2.6-b63"); BEAST_EXPECT((encodedVersion & 0x0000'0000'003F'0000LLU) >> 16 == 63); // the last two bytes are zeros @@ -41,14 +41,14 @@ public: // Test some version strings with wrong formats: // no rc/beta number - encodedVersion = BuildInfo::encodeSoftwareVersion("1.2.3-b"); + encodedVersion = build_info::encodeSoftwareVersion("1.2.3-b"); BEAST_EXPECT((encodedVersion & 0x0000'0000'00FF'0000LLU) == 0); // rc/beta number out of range - encodedVersion = BuildInfo::encodeSoftwareVersion("1.2.3-b64"); + encodedVersion = build_info::encodeSoftwareVersion("1.2.3-b64"); BEAST_EXPECT((encodedVersion & 0x0000'0000'00FF'0000LLU) == 0); // Check that the rc/beta number of a release is 0: - encodedVersion = BuildInfo::encodeSoftwareVersion("1.2.6"); + encodedVersion = build_info::encodeSoftwareVersion("1.2.6"); BEAST_EXPECT((encodedVersion & 0x0000'0000'003F'0000LLU) == 0); } @@ -57,9 +57,9 @@ public: { testcase("IsXrpldVersion"); auto vFF = 0xFFFF'FFFF'FFFF'FFFFLLU; - BEAST_EXPECT(!BuildInfo::isXrpldVersion(vFF)); + BEAST_EXPECT(!build_info::isXrpldVersion(vFF)); auto vXrpld = 0x183B'0000'0000'0000LLU; - BEAST_EXPECT(BuildInfo::isXrpldVersion(vXrpld)); + BEAST_EXPECT(build_info::isXrpldVersion(vXrpld)); } void @@ -67,16 +67,16 @@ public: { testcase("IsNewerVersion"); auto vFF = 0xFFFF'FFFF'FFFF'FFFFLLU; - BEAST_EXPECT(!BuildInfo::isNewerVersion(vFF)); + BEAST_EXPECT(!build_info::isNewerVersion(vFF)); - auto v159 = BuildInfo::encodeSoftwareVersion("1.5.9"); - BEAST_EXPECT(!BuildInfo::isNewerVersion(v159)); + auto v159 = build_info::encodeSoftwareVersion("1.5.9"); + BEAST_EXPECT(!build_info::isNewerVersion(v159)); - auto vCurrent = BuildInfo::getEncodedVersion(); - BEAST_EXPECT(!BuildInfo::isNewerVersion(vCurrent)); + auto vCurrent = build_info::getEncodedVersion(); + BEAST_EXPECT(!build_info::isNewerVersion(vCurrent)); - auto vMax = BuildInfo::encodeSoftwareVersion("255.255.255"); - BEAST_EXPECT(BuildInfo::isNewerVersion(vMax)); + auto vMax = build_info::encodeSoftwareVersion("255.255.255"); + BEAST_EXPECT(build_info::isNewerVersion(vMax)); } void diff --git a/src/test/protocol/InnerObjectFormats_test.cpp b/src/test/protocol/InnerObjectFormats_test.cpp index 5154153ecf..73a283da39 100644 --- a/src/test/protocol/InnerObjectFormats_test.cpp +++ b/src/test/protocol/InnerObjectFormats_test.cpp @@ -5,7 +5,7 @@ #include #include // json::Reader #include -#include // RPC::containsError +#include // rpc::containsError #include // STParsedJSONObject #include @@ -13,7 +13,7 @@ namespace xrpl { -namespace InnerObjectFormatsUnitTestDetail { +namespace inner_object_formats_unit_test_detail { struct TestJSONTxt { @@ -149,7 +149,7 @@ static TestJSONTxt const kTestArray[] = { }; -} // namespace InnerObjectFormatsUnitTestDetail +} // namespace inner_object_formats_unit_test_detail class InnerObjectFormatsParsedJSON_test : public beast::unit_test::Suite { @@ -157,7 +157,7 @@ public: void run() override { - using namespace InnerObjectFormatsUnitTestDetail; + using namespace inner_object_formats_unit_test_detail; // Instantiate a jtx::Env so debugLog writes are exercised. test::jtx::Env const env(*this); @@ -166,7 +166,7 @@ public: { json::Value req; json::Reader().parse(test.txt, req); - if (RPC::containsError(req)) + if (rpc::containsError(req)) { Throw( "Internal InnerObjectFormatsParsedJSON error. Bad JSON."); diff --git a/src/test/protocol/MultiApiJson_test.cpp b/src/test/protocol/MultiApiJson_test.cpp index c6f844a206..2f0d4cb3ec 100644 --- a/src/test/protocol/MultiApiJson_test.cpp +++ b/src/test/protocol/MultiApiJson_test.cpp @@ -62,35 +62,35 @@ struct MultiApiJson_test : beast::unit_test::Suite // Some static data for test inputs static int const kPrimes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; - static_assert(std::size(kPrimes) > RPC::kApiMaximumValidVersion); + static_assert(std::size(kPrimes) > rpc::kApiMaximumValidVersion); MultiApiJson<1, 3> s1{}; static_assert( - s1.kSize == RPC::kApiMaximumValidVersion + 1 - RPC::kApiMinimumSupportedVersion); + s1.kSize == rpc::kApiMaximumValidVersion + 1 - rpc::kApiMinimumSupportedVersion); int productAllVersions = 1; - for (unsigned i = RPC::kApiMinimumSupportedVersion; i <= RPC::kApiMaximumValidVersion; + for (unsigned i = rpc::kApiMinimumSupportedVersion; i <= rpc::kApiMaximumValidVersion; ++i) { - auto const index = i - RPC::kApiMinimumSupportedVersion; + auto const index = i - rpc::kApiMinimumSupportedVersion; BEAST_EXPECT(index == s1.index(i)); BEAST_EXPECT(s1.valid(i)); s1.val[index] = makeJson("value", kPrimes[i]); productAllVersions *= kPrimes[i]; } BEAST_EXPECT(!s1.valid(0)); - BEAST_EXPECT(!s1.valid(RPC::kApiMaximumValidVersion + 1)); + BEAST_EXPECT(!s1.valid(rpc::kApiMaximumValidVersion + 1)); BEAST_EXPECT(!s1.valid( - std::numeric_limits::max())); + std::numeric_limits::max())); int result = 1; - static_assert(RPC::kApiMinimumSupportedVersion + 1 <= RPC::kApiMaximumValidVersion); - forApiVersions( + static_assert(rpc::kApiMinimumSupportedVersion + 1 <= rpc::kApiMaximumValidVersion); + forApiVersions( std::as_const(s1).visit(), [this](json::Value const& json, unsigned int version, int* result) { BEAST_EXPECT( - version >= RPC::kApiMinimumSupportedVersion && - version <= RPC::kApiMinimumSupportedVersion + 1); + version >= rpc::kApiMinimumSupportedVersion && + version <= rpc::kApiMinimumSupportedVersion + 1); if (BEAST_EXPECT(json.isMember("value"))) { *result *= json["value"].asInt(); @@ -99,8 +99,8 @@ struct MultiApiJson_test : beast::unit_test::Suite &result); BEAST_EXPECT( result == - kPrimes[RPC::kApiMinimumSupportedVersion] * - kPrimes[RPC::kApiMinimumSupportedVersion + 1]); + kPrimes[rpc::kApiMinimumSupportedVersion] * + kPrimes[rpc::kApiMinimumSupportedVersion + 1]); // Check all the values with mutable data forAllApiVersions(s1.visit(), [&s1, this](json::Value& json, auto version) { @@ -116,8 +116,8 @@ struct MultiApiJson_test : beast::unit_test::Suite std::as_const(s1).visit(), [this](json::Value const& json, unsigned int version, int* result) { BEAST_EXPECT( - version >= RPC::kApiMinimumSupportedVersion && - version <= RPC::kApiMaximumValidVersion); + version >= rpc::kApiMinimumSupportedVersion && + version <= rpc::kApiMaximumValidVersion); if (BEAST_EXPECT(json.isMember("value"))) { *result *= json["value"].asInt(); diff --git a/src/test/protocol/Serializer_test.cpp b/src/test/protocol/Serializer_test.cpp deleted file mode 100644 index b490e0476b..0000000000 --- a/src/test/protocol/Serializer_test.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include - -#include -#include -#include - -namespace xrpl { - -struct Serializer_test : public beast::unit_test::Suite -{ - void - run() override - { - { - std::initializer_list const values = { - std::numeric_limits::min(), - -1, - 0, - 1, - std::numeric_limits::max()}; - for (std::int32_t const value : values) - { - Serializer s; - s.add32(value); - BEAST_EXPECT(s.size() == 4); - SerialIter sit(s.slice()); - BEAST_EXPECT(sit.geti32() == value); - } - } - { - std::initializer_list const values = { - std::numeric_limits::min(), - -1, - 0, - 1, - std::numeric_limits::max()}; - for (std::int64_t const value : values) - { - Serializer s; - s.add64(value); - BEAST_EXPECT(s.size() == 8); - SerialIter sit(s.slice()); - BEAST_EXPECT(sit.geti64() == value); - } - } - } -}; - -BEAST_DEFINE_TESTSUITE(Serializer, protocol, xrpl); - -} // namespace xrpl diff --git a/src/test/rpc/AccountLines_test.cpp b/src/test/rpc/AccountLines_test.cpp index 8d55c5e19d..cb20de9bf5 100644 --- a/src/test/rpc/AccountLines_test.cpp +++ b/src/test/rpc/AccountLines_test.cpp @@ -34,7 +34,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { class AccountLines_test : public beast::unit_test::Suite { @@ -51,7 +51,7 @@ public: auto const lines = env.rpc("json", "account_lines", "{ }"); BEAST_EXPECT( lines[jss::result][jss::error_message] == - RPC::missingFieldError(jss::account)[jss::error_message]); + rpc::missingFieldError(jss::account)[jss::error_message]); } { // account_lines with a malformed account. @@ -60,7 +60,7 @@ public: auto const lines = env.rpc("json", "account_lines", to_string(params)); BEAST_EXPECT( lines[jss::result][jss::error_message] == - RPC::makeError(RpcActMalformed)[jss::error_message]); + rpc::makeError(RpcActMalformed)[jss::error_message]); } { // test account non-string @@ -87,7 +87,7 @@ public: auto const lines = env.rpc("json", "account_lines", to_string(params)); BEAST_EXPECT( lines[jss::result][jss::error_message] == - RPC::makeError(RpcActNotFound)[jss::error_message]); + rpc::makeError(RpcActNotFound)[jss::error_message]); } env.fund(XRP(10000), alice); env.close(); @@ -250,7 +250,7 @@ public: auto const lines = env.rpc("json", "account_lines", to_string(params)); BEAST_EXPECT( lines[jss::result][jss::error_message] == - RPC::makeError(RpcActMalformed)[jss::error_message]); + rpc::makeError(RpcActMalformed)[jss::error_message]); } { // A negative limit should fail. @@ -260,7 +260,7 @@ public: auto const lines = env.rpc("json", "account_lines", to_string(params)); BEAST_EXPECT( lines[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); } { // Limit the response to 1 trust line. @@ -297,7 +297,7 @@ public: auto const linesD = env.rpc("json", "account_lines", to_string(paramsD)); BEAST_EXPECT( linesD[jss::result][jss::error_message] == - RPC::makeError(RpcInvalidParams)[jss::error_message]); + rpc::makeError(RpcInvalidParams)[jss::error_message]); } { // A non-string marker should also fail. @@ -307,7 +307,7 @@ public: auto const lines = env.rpc("json", "account_lines", to_string(params)); BEAST_EXPECT( lines[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::marker, "string")); + rpc::expectedFieldMessage(jss::marker, "string")); } { // Check that the flags we expect from alice to gw2 are present. @@ -496,7 +496,7 @@ public: auto const linesEnd = env.rpc("json", "account_lines", to_string(linesEndParams)); BEAST_EXPECT( linesEnd[jss::result][jss::error_message] == - RPC::makeError(RpcInvalidParams)[jss::error_message]); + rpc::makeError(RpcInvalidParams)[jss::error_message]); } void @@ -728,7 +728,7 @@ public: auto const lines = env.rpc("json2", to_string(request)); BEAST_EXPECT( lines[jss::error][jss::message] == - RPC::missingFieldError(jss::account)[jss::error_message]); + rpc::missingFieldError(jss::account)[jss::error_message]); BEAST_EXPECT(lines.isMember(jss::jsonrpc) && lines[jss::jsonrpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::ripplerpc) && lines[jss::ripplerpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::id) && lines[jss::id] == 5); @@ -746,7 +746,7 @@ public: auto const lines = env.rpc("json2", to_string(request)); BEAST_EXPECT( lines[jss::error][jss::message] == - RPC::makeError(RpcActMalformed)[jss::error_message]); + rpc::makeError(RpcActMalformed)[jss::error_message]); BEAST_EXPECT(lines.isMember(jss::jsonrpc) && lines[jss::jsonrpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::ripplerpc) && lines[jss::ripplerpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::id) && lines[jss::id] == 5); @@ -765,7 +765,7 @@ public: auto const lines = env.rpc("json2", to_string(request)); BEAST_EXPECT( lines[jss::error][jss::message] == - RPC::makeError(RpcActNotFound)[jss::error_message]); + rpc::makeError(RpcActNotFound)[jss::error_message]); BEAST_EXPECT(lines.isMember(jss::jsonrpc) && lines[jss::jsonrpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::ripplerpc) && lines[jss::ripplerpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::id) && lines[jss::id] == 5); @@ -998,7 +998,7 @@ public: auto const lines = env.rpc("json2", to_string(request)); BEAST_EXPECT( lines[jss::error][jss::message] == - RPC::makeError(RpcActMalformed)[jss::error_message]); + rpc::makeError(RpcActMalformed)[jss::error_message]); BEAST_EXPECT(lines.isMember(jss::jsonrpc) && lines[jss::jsonrpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::ripplerpc) && lines[jss::ripplerpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::id) && lines[jss::id] == 5); @@ -1017,7 +1017,7 @@ public: auto const lines = env.rpc("json2", to_string(request)); BEAST_EXPECT( lines[jss::error][jss::message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); BEAST_EXPECT(lines.isMember(jss::jsonrpc) && lines[jss::jsonrpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::ripplerpc) && lines[jss::ripplerpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::id) && lines[jss::id] == 5); @@ -1090,7 +1090,7 @@ public: auto const linesD = env.rpc("json2", to_string(requestD)); BEAST_EXPECT( linesD[jss::error][jss::message] == - RPC::makeError(RpcInvalidParams)[jss::error_message]); + rpc::makeError(RpcInvalidParams)[jss::error_message]); BEAST_EXPECT(linesD.isMember(jss::jsonrpc) && linesD[jss::jsonrpc] == "2.0"); BEAST_EXPECT(linesD.isMember(jss::ripplerpc) && linesD[jss::ripplerpc] == "2.0"); BEAST_EXPECT(linesD.isMember(jss::id) && linesD[jss::id] == 5); @@ -1109,7 +1109,7 @@ public: auto const lines = env.rpc("json2", to_string(request)); BEAST_EXPECT( lines[jss::error][jss::message] == - RPC::expectedFieldMessage(jss::marker, "string")); + rpc::expectedFieldMessage(jss::marker, "string")); BEAST_EXPECT(lines.isMember(jss::jsonrpc) && lines[jss::jsonrpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::ripplerpc) && lines[jss::ripplerpc] == "2.0"); BEAST_EXPECT(lines.isMember(jss::id) && lines[jss::id] == 5); @@ -1266,7 +1266,7 @@ public: auto const linesEnd = env.rpc("json2", to_string(linesEndRequest)); BEAST_EXPECT( linesEnd[jss::error][jss::message] == - RPC::makeError(RpcInvalidParams)[jss::error_message]); + rpc::makeError(RpcInvalidParams)[jss::error_message]); BEAST_EXPECT(linesEnd.isMember(jss::jsonrpc) && linesEnd[jss::jsonrpc] == "2.0"); BEAST_EXPECT(linesEnd.isMember(jss::ripplerpc) && linesEnd[jss::ripplerpc] == "2.0"); BEAST_EXPECT(linesEnd.isMember(jss::id) && linesEnd[jss::id] == 5); @@ -1286,4 +1286,4 @@ public: BEAST_DEFINE_TESTSUITE(AccountLines, rpc, xrpl); -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/test/rpc/AccountTx_test.cpp b/src/test/rpc/AccountTx_test.cpp index f1fbc2871b..b144a6dc84 100644 --- a/src/test/rpc/AccountTx_test.cpp +++ b/src/test/rpc/AccountTx_test.cpp @@ -199,7 +199,7 @@ class AccountTx_test : public beast::unit_test::Suite auto isErr = [](json::Value const& j, ErrorCodeI code) { return j.isMember(jss::result) && j[jss::result].isMember(jss::error) && - j[jss::result][jss::error] == RPC::getErrorInfo(code).token; + j[jss::result][jss::error] == rpc::getErrorInfo(code).token; }; json::Value jParams; @@ -425,56 +425,56 @@ class AccountTx_test : public beast::unit_test::Suite p[jss::limit] = 1.2; BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = "10" should fail (string instead of integer) p[jss::limit] = "10"; BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = true should fail (boolean instead of integer) p[jss::limit] = true; BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = false should fail (boolean instead of integer) p[jss::limit] = false; BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = -1 should fail (negative number) p[jss::limit] = -1; BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = [] should fail (array instead of integer) p[jss::limit] = json::Value(json::ValueType::Array); BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = {} should fail (object instead of integer) p[jss::limit] = json::Value(json::ValueType::Object); BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = "malformed" should fail (malformed string) p[jss::limit] = "malformed"; BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = ["limit"] should fail (array with string) p[jss::limit] = json::Value(json::ValueType::Array); p[jss::limit].append("limit"); BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = {"limit": 10} should fail (object with // property) @@ -482,7 +482,7 @@ class AccountTx_test : public beast::unit_test::Suite p[jss::limit][jss::limit] = 10; BEAST_EXPECT( env.rpc("json", "account_tx", to_string(p))[jss::result][jss::error_message] == - RPC::expectedFieldMessage(jss::limit, "unsigned integer")); + rpc::expectedFieldMessage(jss::limit, "unsigned integer")); // Test case: limit = 10 should succeed (valid integer) p[jss::limit] = 10; diff --git a/src/test/rpc/Book_test.cpp b/src/test/rpc/Book_test.cpp index 83f7b64b4b..646cf190ff 100644 --- a/src/test/rpc/Book_test.cpp +++ b/src/test/rpc/Book_test.cpp @@ -1548,7 +1548,7 @@ public: auto usd = gw["USD"]; - for (auto i = 0; i <= RPC::Tuning::kBookOffers.rmax; i++) + for (auto i = 0; i <= rpc::tuning::kBookOffers.rmax; i++) env(offer(gw, XRP(50 + (1 * i)), usd(1.0 + (0.1 * i)))); if (asAdmin) @@ -1565,15 +1565,15 @@ public: BEAST_EXPECT(jrr[jss::offers].size() == (asAdmin ? 1u : 0u)); // NOTE - a marker field is not returned for this method - jvParams[jss::limit] = RPC::Tuning::kBookOffers.rmax + 1; + jvParams[jss::limit] = rpc::tuning::kBookOffers.rmax + 1; jrr = env.rpc("json", "book_offers", to_string(jvParams))[jss::result]; BEAST_EXPECT(jrr[jss::offers].isArray()); - BEAST_EXPECT(jrr[jss::offers].size() == (asAdmin ? RPC::Tuning::kBookOffers.rmax + 1 : 0u)); + BEAST_EXPECT(jrr[jss::offers].size() == (asAdmin ? rpc::tuning::kBookOffers.rmax + 1 : 0u)); jvParams[jss::limit] = json::ValueType::Null; jrr = env.rpc("json", "book_offers", to_string(jvParams))[jss::result]; BEAST_EXPECT(jrr[jss::offers].isArray()); - BEAST_EXPECT(jrr[jss::offers].size() == (asAdmin ? RPC::Tuning::kBookOffers.rDefault : 0u)); + BEAST_EXPECT(jrr[jss::offers].size() == (asAdmin ? rpc::tuning::kBookOffers.rDefault : 0u)); } void diff --git a/src/test/rpc/Handler_test.cpp b/src/test/rpc/Handler_test.cpp index e900b92fc3..be78864cac 100644 --- a/src/test/rpc/Handler_test.cpp +++ b/src/test/rpc/Handler_test.cpp @@ -88,7 +88,7 @@ class Handler_test : public beast::unit_test::Suite std::random_device dev; std::ranlux48 prng(dev()); - std::vector names = test::jtx::makeVector(xrpl::RPC::getHandlerNames()); + std::vector names = test::jtx::makeVector(xrpl::rpc::getHandlerNames()); std::uniform_int_distribution distr{0, names.size() - 1}; @@ -96,7 +96,7 @@ class Handler_test : public beast::unit_test::Suite auto const [mean, stdev, n] = time( 1'000'000, [&](std::size_t i) { - auto const d = RPC::getHandler(1, false, names[i]); + auto const d = rpc::getHandler(1, false, names[i]); dummy = dummy + i + (int)d->role; }, [&]() -> std::size_t { return distr(prng); }); diff --git a/src/test/rpc/JSONRPC_test.cpp b/src/test/rpc/JSONRPC_test.cpp index e18974e7e7..efba4075c7 100644 --- a/src/test/rpc/JSONRPC_test.cpp +++ b/src/test/rpc/JSONRPC_test.cpp @@ -36,7 +36,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { struct TxnTestData { @@ -2248,7 +2248,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( req[jss::tx_json].isMember(jss::Fee) && req[jss::tx_json][jss::Fee] == baseFee); } @@ -2268,7 +2268,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( req[jss::tx_json].isMember(jss::Fee) && req[jss::tx_json][jss::Fee] == baseFee); } @@ -2285,7 +2285,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); BEAST_EXPECT(!req[jss::tx_json].isMember(jss::Fee)); } @@ -2306,7 +2306,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); BEAST_EXPECT(!req[jss::tx_json].isMember(jss::Fee)); } @@ -2325,7 +2325,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); BEAST_EXPECT(!req[jss::tx_json].isMember(jss::Fee)); } @@ -2344,7 +2344,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); BEAST_EXPECT(!req[jss::tx_json].isMember(jss::Fee)); } @@ -2400,7 +2400,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT(req[jss::tx_json].isMember(jss::Fee) && req[jss::tx_json][jss::Fee] == 10); } @@ -2422,7 +2422,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT(req[jss::tx_json].isMember(jss::Fee) && req[jss::tx_json][jss::Fee] == 10); } @@ -2450,7 +2450,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( req[jss::tx_json].isMember(jss::Fee) && req[jss::tx_json][jss::Fee] == 8889); } @@ -2473,7 +2473,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); BEAST_EXPECT(!req[jss::tx_json].isMember(jss::Fee)); } @@ -2496,7 +2496,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); BEAST_EXPECT(!req[jss::tx_json].isMember(jss::Fee)); } @@ -2519,7 +2519,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( req[jss::tx_json].isMember(jss::Fee) && req[jss::tx_json][jss::Fee] == 8889); } @@ -2542,7 +2542,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); } { @@ -2563,7 +2563,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); } { @@ -2585,7 +2585,7 @@ public: env.app().getTxQ(), env.app()); - BEAST_EXPECT(RPC::containsError(result)); + BEAST_EXPECT(rpc::containsError(result)); } env.close(); @@ -2598,7 +2598,7 @@ public: auto rpcResult = env.rpc("json", "sign", to_string(toSign)); auto result = rpcResult[jss::result]; - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( result[jss::tx_json].isMember(jss::Fee) && result[jss::tx_json][jss::Fee] == "10"); BEAST_EXPECT( @@ -2624,7 +2624,7 @@ public: auto rpcResult = env.rpc("json", "sign", to_string(toSign)); auto result = rpcResult[jss::result]; - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( result[jss::tx_json].isMember(jss::Fee) && result[jss::tx_json][jss::Fee] == "7813"); @@ -2651,7 +2651,7 @@ public: auto rpcResult = env.rpc("json", "sign", to_string(toSign)); auto result = rpcResult[jss::result]; - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( result[jss::tx_json].isMember(jss::Fee) && result[jss::tx_json][jss::Fee] == "47"); BEAST_EXPECT( @@ -2682,7 +2682,7 @@ public: auto rpcResult = env.rpc("json", "sign", to_string(toSign)); auto result = rpcResult[jss::result]; - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( result[jss::tx_json].isMember(jss::Fee) && result[jss::tx_json][jss::Fee] == "6806"); @@ -2711,7 +2711,7 @@ public: auto rpcResult = env.rpc("json", "sign", to_string(toSign)); auto result = rpcResult[jss::result]; - BEAST_EXPECT(!RPC::containsError(result)); + BEAST_EXPECT(!rpc::containsError(result)); BEAST_EXPECT( result[jss::tx_json].isMember(jss::NetworkID) && result[jss::tx_json][jss::NetworkID] == 1025); @@ -2791,7 +2791,7 @@ public: { json::Value req; json::Reader().parse(txnTest.json, req); - if (RPC::containsError(req)) + if (rpc::containsError(req)) Throw("Internal JSONRPC_test error. Bad test JSON."); static Role const kTestedRoles[] = { @@ -2815,7 +2815,7 @@ public: } std::string errStr; - if (RPC::containsError(result)) + if (rpc::containsError(result)) errStr = result["error_message"].asString(); if (errStr == txnTest.expMsg[get<3>(testFunc)]) @@ -2848,4 +2848,4 @@ public: BEAST_DEFINE_TESTSUITE(JSONRPC, rpc, xrpl); -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/test/rpc/KeyGeneration_test.cpp b/src/test/rpc/KeyGeneration_test.cpp index aafe6f75a5..2b056fc6d2 100644 --- a/src/test/rpc/KeyGeneration_test.cpp +++ b/src/test/rpc/KeyGeneration_test.cpp @@ -15,7 +15,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { struct KeyStrings { @@ -800,4 +800,4 @@ public: BEAST_DEFINE_TESTSUITE(WalletPropose, rpc, xrpl); -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/test/rpc/LedgerEntry_test.cpp b/src/test/rpc/LedgerEntry_test.cpp index 7adb5a4518..b8301d5656 100644 --- a/src/test/rpc/LedgerEntry_test.cpp +++ b/src/test/rpc/LedgerEntry_test.cpp @@ -349,7 +349,7 @@ class LedgerEntry_test : public beast::unit_test::Suite json::Value const jrr = env.rpc( apiVersion, "json", "ledger_entry", to_string(correctRequest))[jss::result]; auto const expectedErrMsg = - RPC::expectedFieldMessage(fieldName, getTypeName(typeID)); + rpc::expectedFieldMessage(fieldName, getTypeName(typeID)); checkErrorValue(jrr, expectedError, expectedErrMsg, location); }; @@ -383,13 +383,13 @@ class LedgerEntry_test : public beast::unit_test::Suite json::Value const jrr = env.rpc( apiVersion, "json", "ledger_entry", to_string(correctRequest))[jss::result]; checkErrorValue( - jrr, "malformedRequest", RPC::missingFieldMessage(fieldName.cStr()), location); + jrr, "malformedRequest", rpc::missingFieldMessage(fieldName.cStr()), location); correctRequest[parentFieldName][fieldName] = json::ValueType::Null; json::Value const jrr2 = env.rpc( apiVersion, "json", "ledger_entry", to_string(correctRequest))[jss::result]; checkErrorValue( - jrr2, "malformedRequest", RPC::missingFieldMessage(fieldName.cStr()), location); + jrr2, "malformedRequest", rpc::missingFieldMessage(fieldName.cStr()), location); } auto tryField = [&](json::Value fieldValue) -> void { correctRequest[parentFieldName][fieldName] = fieldValue; @@ -399,7 +399,7 @@ class LedgerEntry_test : public beast::unit_test::Suite checkErrorValue( jrr, expectedError, - RPC::expectedFieldMessage(fieldName, getTypeName(typeID)), + rpc::expectedFieldMessage(fieldName, getTypeName(typeID)), location); }; @@ -1083,8 +1083,8 @@ class LedgerEntry_test : public beast::unit_test::Suite json::Value const jrr = env.rpc("json", "ledger_entry", to_string(jvParams))[jss::result]; auto const expectedErrMsg = fieldValue.isNull() - ? RPC::missingFieldMessage(jss::issuer.cStr()) - : RPC::expectedFieldMessage(jss::issuer, "AccountID"); + ? rpc::missingFieldMessage(jss::issuer.cStr()) + : rpc::expectedFieldMessage(jss::issuer, "AccountID"); checkErrorValue(jrr, "malformedAuthorizedCredentials", expectedErrMsg); }; @@ -1114,7 +1114,7 @@ class LedgerEntry_test : public beast::unit_test::Suite checkErrorValue( jrr[jss::result], "malformedAuthorizedCredentials", - RPC::expectedFieldMessage(jss::authorized_credentials, "array")); + rpc::expectedFieldMessage(jss::authorized_credentials, "array")); } { @@ -1134,8 +1134,8 @@ class LedgerEntry_test : public beast::unit_test::Suite json::Value const jrr = env.rpc("json", "ledger_entry", to_string(jvParams))[jss::result]; auto const expectedErrMsg = fieldValue.isNull() - ? RPC::missingFieldMessage(jss::credential_type.cStr()) - : RPC::expectedFieldMessage(jss::credential_type, "hex string"); + ? rpc::missingFieldMessage(jss::credential_type.cStr()) + : rpc::expectedFieldMessage(jss::credential_type, "hex string"); checkErrorValue(jrr, "malformedAuthorizedCredentials", expectedErrMsg); }; @@ -1836,7 +1836,7 @@ class LedgerEntry_test : public beast::unit_test::Suite checkErrorValue( jrr, "malformedAddress", - RPC::expectedFieldMessage(jss::accounts, "array of Accounts")); + rpc::expectedFieldMessage(jss::accounts, "array of Accounts")); } { @@ -1851,7 +1851,7 @@ class LedgerEntry_test : public beast::unit_test::Suite checkErrorValue( jrr, "malformedAddress", - RPC::expectedFieldMessage(jss::accounts, "array of Accounts")); + rpc::expectedFieldMessage(jss::accounts, "array of Accounts")); } }; diff --git a/src/test/rpc/LedgerRPC_test.cpp b/src/test/rpc/LedgerRPC_test.cpp index 3a2c957691..af93108ff2 100644 --- a/src/test/rpc/LedgerRPC_test.cpp +++ b/src/test/rpc/LedgerRPC_test.cpp @@ -158,7 +158,7 @@ class LedgerRPC_test : public beast::unit_test::Suite { // Request a ledger with a very large (double) sequence. auto const ret = env.rpc("json", "ledger", "{ \"ledger_index\" : 2e15 }"); - BEAST_EXPECT(RPC::containsError(ret)); + BEAST_EXPECT(rpc::containsError(ret)); BEAST_EXPECT(ret[jss::error_message] == "Invalid parameters."); } diff --git a/src/test/rpc/LedgerRequest_test.cpp b/src/test/rpc/LedgerRequest_test.cpp index 93feee9497..98bde4e5a5 100644 --- a/src/test/rpc/LedgerRequest_test.cpp +++ b/src/test/rpc/LedgerRequest_test.cpp @@ -15,7 +15,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { class LedgerRequest_test : public beast::unit_test::Suite { @@ -43,28 +43,28 @@ public: // arbitrary text is converted to 0. auto const result = env.rpc("ledger_request", "arbitrary_text"); BEAST_EXPECT( - RPC::containsError(result[jss::result]) && + rpc::containsError(result[jss::result]) && result[jss::result][jss::error_message] == "Ledger index too small"); } { auto const result = env.rpc("ledger_request", "-1"); BEAST_EXPECT( - RPC::containsError(result[jss::result]) && + rpc::containsError(result[jss::result]) && result[jss::result][jss::error_message] == "Ledger index too small"); } { auto const result = env.rpc("ledger_request", "0"); BEAST_EXPECT( - RPC::containsError(result[jss::result]) && + rpc::containsError(result[jss::result]) && result[jss::result][jss::error_message] == "Ledger index too small"); } { auto const result = env.rpc("ledger_request", "1"); BEAST_EXPECT( - !RPC::containsError(result[jss::result]) && + !rpc::containsError(result[jss::result]) && result[jss::result][jss::ledger_index] == 1 && result[jss::result].isMember(jss::ledger)); BEAST_EXPECT( @@ -75,7 +75,7 @@ public: { auto const result = env.rpc("ledger_request", "2"); BEAST_EXPECT( - !RPC::containsError(result[jss::result]) && + !rpc::containsError(result[jss::result]) && result[jss::result][jss::ledger_index] == 2 && result[jss::result].isMember(jss::ledger)); BEAST_EXPECT( @@ -86,7 +86,7 @@ public: { auto const result = env.rpc("ledger_request", "3"); BEAST_EXPECT( - !RPC::containsError(result[jss::result]) && + !rpc::containsError(result[jss::result]) && result[jss::result][jss::ledger_index] == 3 && result[jss::result].isMember(jss::ledger)); BEAST_EXPECT( @@ -98,7 +98,7 @@ public: { auto const r = env.rpc("ledger_request", ledgerHash); BEAST_EXPECT( - !RPC::containsError(r[jss::result]) && r[jss::result][jss::ledger_index] == 3 && + !rpc::containsError(r[jss::result]) && r[jss::result][jss::ledger_index] == 3 && r[jss::result].isMember(jss::ledger)); BEAST_EXPECT( r[jss::result][jss::ledger].isMember(jss::ledger_hash) && @@ -112,7 +112,7 @@ public: auto const result = env.rpc("ledger_request", ledgerHash); BEAST_EXPECT( - RPC::containsError(result[jss::result]) && + rpc::containsError(result[jss::result]) && result[jss::result][jss::error_message] == "Invalid field 'ledger_hash', not hex string."); } @@ -123,21 +123,21 @@ public: auto const result = env.rpc("ledger_request", ledgerHash); BEAST_EXPECT( - !RPC::containsError(result[jss::result]) && + !rpc::containsError(result[jss::result]) && result[jss::result][jss::have_header] == false); } { auto const result = env.rpc("ledger_request", "4"); BEAST_EXPECT( - RPC::containsError(result[jss::result]) && + rpc::containsError(result[jss::result]) && result[jss::result][jss::error_message] == "Ledger index too large"); } { auto const result = env.rpc("ledger_request", "5"); BEAST_EXPECT( - RPC::containsError(result[jss::result]) && + rpc::containsError(result[jss::result]) && result[jss::result][jss::error_message] == "Ledger index too large"); } } @@ -357,4 +357,4 @@ public: BEAST_DEFINE_TESTSUITE(LedgerRequest, rpc, xrpl); -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/test/rpc/NoRippleCheck_test.cpp b/src/test/rpc/NoRippleCheck_test.cpp index 9c17d291a1..6e30f944c7 100644 --- a/src/test/rpc/NoRippleCheck_test.cpp +++ b/src/test/rpc/NoRippleCheck_test.cpp @@ -286,9 +286,9 @@ class NoRippleCheckLimits_test : public beast::unit_test::Suite // be better if we could add this functionality to Env somehow // or otherwise disable endpoint charging for certain test // cases. - using namespace xrpl::Resource; + using namespace xrpl::resource; using namespace std::chrono; - using namespace beast::IP; + using namespace beast::ip; auto c = env.app().getResourceManager().newInboundEndpoint( Endpoint::fromString(test::getEnvLocalhostAddr())); @@ -301,7 +301,7 @@ class NoRippleCheckLimits_test : public beast::unit_test::Suite } }; - for (auto i = 0; i < xrpl::RPC::Tuning::kNoRippleCheck.rmax + 5; ++i) + for (auto i = 0; i < xrpl::rpc::tuning::kNoRippleCheck.rmax + 5; ++i) { if (!admin) checkBalance(); diff --git a/src/test/rpc/RPCCall_test.cpp b/src/test/rpc/RPCCall_test.cpp index 4b5ab1f230..ef3213008c 100644 --- a/src/test/rpc/RPCCall_test.cpp +++ b/src/test/rpc/RPCCall_test.cpp @@ -5855,8 +5855,8 @@ public: { testcase << "RPCCall API version " << apiVersion; if (!BEAST_EXPECT( - apiVersion >= RPC::kApiMinimumSupportedVersion && - apiVersion <= RPC::kApiMaximumValidVersion)) + apiVersion >= rpc::kApiMinimumSupportedVersion && + apiVersion <= rpc::kApiMaximumValidVersion)) return; test::jtx::Env const env(*this, makeNetworkConfig(11111)); // Used only for its Journal. @@ -5870,8 +5870,8 @@ public: std::vector const args{rpcCallTest.args.begin(), rpcCallTest.args.end()}; char const* const expVersioned = - (apiVersion - RPC::kApiMinimumSupportedVersion) < rpcCallTest.exp.size() - ? rpcCallTest.exp[apiVersion - RPC::kApiMinimumSupportedVersion] + (apiVersion - rpc::kApiMinimumSupportedVersion) < rpcCallTest.exp.size() + ? rpcCallTest.exp[apiVersion - rpc::kApiMinimumSupportedVersion] : rpcCallTest.exp.back(); // Note that, over the long term, kNone of these tests should diff --git a/src/test/rpc/RPCHelpers_test.cpp b/src/test/rpc/RPCHelpers_test.cpp index 1458c0aa80..25368235a3 100644 --- a/src/test/rpc/RPCHelpers_test.cpp +++ b/src/test/rpc/RPCHelpers_test.cpp @@ -19,50 +19,50 @@ public: // Test no type. json::Value tx = json::ValueType::Object; - auto result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status::kOK); + auto result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status::kOK); BEAST_EXPECT(result.second == 0); // Test empty type. tx[jss::type] = ""; - result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status{RpcInvalidParams}); + result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams}); BEAST_EXPECT(result.second == 0); // Test type using canonical name in mixedcase. tx[jss::type] = "MPTokenIssuance"; - result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status::kOK); + result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status::kOK); BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE); // Test type using canonical name in lowercase. tx[jss::type] = "mptokenissuance"; - result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status::kOK); + result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status::kOK); BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE); // Test type using RPC name with exact match. tx[jss::type] = "mpt_issuance"; - result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status::kOK); + result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status::kOK); BEAST_EXPECT(result.second == ltMPTOKEN_ISSUANCE); // Test type using RPC name with inexact match. tx[jss::type] = "MPT_Issuance"; - result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status{RpcInvalidParams}); + result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams}); BEAST_EXPECT(result.second == 0); // Test invalid type. tx[jss::type] = 1234; - result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status{RpcInvalidParams}); + result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams}); BEAST_EXPECT(result.second == 0); // Test unknown type. tx[jss::type] = "unknown"; - result = RPC::chooseLedgerEntryType(tx); - BEAST_EXPECT(result.first == RPC::Status{RpcInvalidParams}); + result = rpc::chooseLedgerEntryType(tx); + BEAST_EXPECT(result.first == rpc::Status{RpcInvalidParams}); BEAST_EXPECT(result.second == 0); } diff --git a/src/test/rpc/Status_test.cpp b/src/test/rpc/Status_test.cpp index c4f8544980..aaf696e9af 100644 --- a/src/test/rpc/Status_test.cpp +++ b/src/test/rpc/Status_test.cpp @@ -12,7 +12,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { class codeString_test : public beast::unit_test::Suite { @@ -202,6 +202,6 @@ public: } }; -BEAST_DEFINE_TESTSUITE(fillJson, rpc, RPC); +BEAST_DEFINE_TESTSUITE(fillJson, rpc, xrpl); -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/test/rpc/TransactionEntry_test.cpp b/src/test/rpc/TransactionEntry_test.cpp index 38a95e84f8..b57c615b71 100644 --- a/src/test/rpc/TransactionEntry_test.cpp +++ b/src/test/rpc/TransactionEntry_test.cpp @@ -183,7 +183,7 @@ class TransactionEntry_test : public beast::unit_test::Suite { json::Value expected; json::Reader().parse(expectedJson, expected); - if (RPC::containsError(expected)) + if (rpc::containsError(expected)) Throw("Internal JSONRPC_test error. Bad test JSON."); for (auto memberIt = expected.begin(); memberIt != expected.end(); memberIt++) diff --git a/src/test/rpc/Transaction_test.cpp b/src/test/rpc/Transaction_test.cpp index 4dae475b63..2921c63c17 100644 --- a/src/test/rpc/Transaction_test.cpp +++ b/src/test/rpc/Transaction_test.cpp @@ -62,9 +62,9 @@ class Transaction_test : public beast::unit_test::Suite char const* command = jss::tx.cStr(); char const* binary = jss::binary.cStr(); - char const* notFound = RPC::getErrorInfo(RpcTxnNotFound).token; - char const* invalid = RPC::getErrorInfo(RpcInvalidLgrRange).token; - char const* excessive = RPC::getErrorInfo(RpcExcessiveLgrRange).token; + char const* notFound = rpc::getErrorInfo(RpcTxnNotFound).token; + char const* invalid = rpc::getErrorInfo(RpcInvalidLgrRange).token; + char const* excessive = rpc::getErrorInfo(RpcExcessiveLgrRange).token; Env env{*this, features}; auto const alice = Account("alice"); @@ -301,9 +301,9 @@ class Transaction_test : public beast::unit_test::Suite char const* command = jss::tx.cStr(); char const* binary = jss::binary.cStr(); - char const* notFound = RPC::getErrorInfo(RpcTxnNotFound).token; - char const* invalid = RPC::getErrorInfo(RpcInvalidLgrRange).token; - char const* excessive = RPC::getErrorInfo(RpcExcessiveLgrRange).token; + char const* notFound = rpc::getErrorInfo(RpcTxnNotFound).token; + char const* invalid = rpc::getErrorInfo(RpcInvalidLgrRange).token; + char const* excessive = rpc::getErrorInfo(RpcExcessiveLgrRange).token; Env env{*this, makeNetworkConfig(11111)}; uint32_t const netID = env.app().getNetworkIDService().getNetworkID(); @@ -333,7 +333,7 @@ class Transaction_test : public beast::unit_test::Suite auto const result = env.rpc( command, // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - *RPC::encodeCTID(startLegSeq + i, txnIdx, netID), + *rpc::encodeCTID(startLegSeq + i, txnIdx, netID), binary, to_string(startLegSeq), to_string(endLegSeq)); @@ -345,7 +345,7 @@ class Transaction_test : public beast::unit_test::Suite auto const tx = env.jt(noop(alice), Seq(env.seq(alice))).stx; // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - auto const ctid = *RPC::encodeCTID(endLegSeq, tx->getSeqValue(), netID); + auto const ctid = *rpc::encodeCTID(endLegSeq, tx->getSeqValue(), netID); for (int deltaEndSeq = 0; deltaEndSeq < 2; ++deltaEndSeq) { auto const result = env.rpc( @@ -374,7 +374,7 @@ class Transaction_test : public beast::unit_test::Suite auto const result = env.rpc( command, // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - *RPC::encodeCTID(startLegSeq + i, txnIdx, netID), + *rpc::encodeCTID(startLegSeq + i, txnIdx, netID), binary, to_string(endLegSeq + 1), to_string(endLegSeq + 100)); @@ -434,7 +434,7 @@ class Transaction_test : public beast::unit_test::Suite auto const result = env.rpc( command, // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - *RPC::encodeCTID(endLegSeq, txnIdx, netID), + *rpc::encodeCTID(endLegSeq, txnIdx, netID), to_string(startLegSeq), to_string(deletedLedger - 1)); @@ -527,75 +527,75 @@ class Transaction_test : public beast::unit_test::Suite // Test case 1: Valid input values auto const expected11 = std::optional("CFFFFFFFFFFFFFFF"); - BEAST_EXPECT(RPC::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0xFFFFU) == expected11); + BEAST_EXPECT(rpc::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0xFFFFU) == expected11); auto const expected12 = std::optional("C000000000000000"); - BEAST_EXPECT(RPC::encodeCTID(0, 0, 0) == expected12); + BEAST_EXPECT(rpc::encodeCTID(0, 0, 0) == expected12); auto const expected13 = std::optional("C000000100020003"); - BEAST_EXPECT(RPC::encodeCTID(1U, 2U, 3U) == expected13); + BEAST_EXPECT(rpc::encodeCTID(1U, 2U, 3U) == expected13); auto const expected14 = std::optional("C0CA2AA7326FFFFF"); - BEAST_EXPECT(RPC::encodeCTID(13249191UL, 12911U, 65535U) == expected14); + BEAST_EXPECT(rpc::encodeCTID(13249191UL, 12911U, 65535U) == expected14); // Test case 2: ledger_seq greater than 0xFFFFFFF - BEAST_EXPECT(!RPC::encodeCTID(0x1000'0000UL, 0xFFFFU, 0xFFFFU)); + BEAST_EXPECT(!rpc::encodeCTID(0x1000'0000UL, 0xFFFFU, 0xFFFFU)); // Test case 3: txn_index greater than 0xFFFF - BEAST_EXPECT(!RPC::encodeCTID(0x0FFF'FFFF, 0x1'0000, 0xFFFF)); + BEAST_EXPECT(!rpc::encodeCTID(0x0FFF'FFFF, 0x1'0000, 0xFFFF)); // Test case 4: network_id greater than 0xFFFF - BEAST_EXPECT(!RPC::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0x1'0000U)); + BEAST_EXPECT(!rpc::encodeCTID(0x0FFF'FFFFUL, 0xFFFFU, 0x1'0000U)); // Test case 5: Valid input values auto const expected51 = std::optional>(std::make_tuple(0, 0, 0)); - BEAST_EXPECT(RPC::decodeCTID("C000000000000000") == expected51); + BEAST_EXPECT(rpc::decodeCTID("C000000000000000") == expected51); auto const expected52 = std::optional>(std::make_tuple(1U, 2U, 3U)); - BEAST_EXPECT(RPC::decodeCTID("C000000100020003") == expected52); + BEAST_EXPECT(rpc::decodeCTID("C000000100020003") == expected52); auto const expected53 = std::optional>( std::make_tuple(13249191UL, 12911U, 49221U)); - BEAST_EXPECT(RPC::decodeCTID("C0CA2AA7326FC045") == expected53); + BEAST_EXPECT(rpc::decodeCTID("C0CA2AA7326FC045") == expected53); // Test case 6: ctid not a string or big int - BEAST_EXPECT(!RPC::decodeCTID(0xCFF)); + BEAST_EXPECT(!rpc::decodeCTID(0xCFF)); // Test case 7: ctid not a hexadecimal string - BEAST_EXPECT(!RPC::decodeCTID("C003FFFFFFFFFFFG")); + BEAST_EXPECT(!rpc::decodeCTID("C003FFFFFFFFFFFG")); // Test case 8: ctid not exactly 16 nibbles - BEAST_EXPECT(!RPC::decodeCTID("C003FFFFFFFFFFF")); + BEAST_EXPECT(!rpc::decodeCTID("C003FFFFFFFFFFF")); // Test case 9: ctid too large to be a valid CTID value - BEAST_EXPECT(!RPC::decodeCTID("CFFFFFFFFFFFFFFFF")); + BEAST_EXPECT(!rpc::decodeCTID("CFFFFFFFFFFFFFFFF")); // Test case 10: ctid doesn't start with a C nibble - BEAST_EXPECT(!RPC::decodeCTID("FFFFFFFFFFFFFFFF")); + BEAST_EXPECT(!rpc::decodeCTID("FFFFFFFFFFFFFFFF")); // Test case 11: Valid input values BEAST_EXPECT( - (RPC::decodeCTID(0xCFFF'FFFF'FFFF'FFFFULL) == + (rpc::decodeCTID(0xCFFF'FFFF'FFFF'FFFFULL) == std::optional>( std::make_tuple(0x0FFF'FFFFUL, 0xFFFFU, 0xFFFFU)))); BEAST_EXPECT( - (RPC::decodeCTID(0xC000'0000'0000'0000ULL) == + (rpc::decodeCTID(0xC000'0000'0000'0000ULL) == std::optional>(std::make_tuple(0, 0, 0)))); BEAST_EXPECT( - (RPC::decodeCTID(0xC000'0001'0002'0003ULL) == + (rpc::decodeCTID(0xC000'0001'0002'0003ULL) == std::optional>(std::make_tuple(1U, 2U, 3U)))); BEAST_EXPECT( - (RPC::decodeCTID(0xC0CA'2AA7'326F'C045ULL) == + (rpc::decodeCTID(0xC0CA'2AA7'326F'C045ULL) == std::optional>( std::make_tuple(1324'9191UL, 12911U, 49221U)))); // Test case 12: ctid not exactly 16 nibbles - BEAST_EXPECT(!RPC::decodeCTID(0xC003'FFFF'FFFF'FFF)); + BEAST_EXPECT(!rpc::decodeCTID(0xC003'FFFF'FFFF'FFF)); // Test case 13: ctid too large to be a valid CTID value // this test case is not possible in c++ because it would overflow the // type, left in for completeness - // BEAST_EXPECT(!RPC::decodeCTID(0xCFFFFFFFFFFFFFFFFULL)); + // BEAST_EXPECT(!rpc::decodeCTID(0xCFFFFFFFFFFFFFFFFULL)); // Test case 14: ctid doesn't start with a C nibble - BEAST_EXPECT(!RPC::decodeCTID(0xFFFF'FFFF'FFFF'FFFFULL)); + BEAST_EXPECT(!rpc::decodeCTID(0xFFFF'FFFF'FFFF'FFFFULL)); } void @@ -619,7 +619,7 @@ class Transaction_test : public beast::unit_test::Suite env(pay(alice, bob, XRP(10))); env.close(); - auto const ctid = RPC::encodeCTID(startLegSeq, 0, netID); + auto const ctid = rpc::encodeCTID(startLegSeq, 0, netID); if (netID > 0xFFFF) { // Concise transaction IDs do not support a network ID > 0xFFFF. @@ -650,7 +650,7 @@ class Transaction_test : public beast::unit_test::Suite env.close(); // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - std::string const ctid = *RPC::encodeCTID(startLegSeq, 0, netID); + std::string const ctid = *rpc::encodeCTID(startLegSeq, 0, netID); auto isUpper = [](char c) { return std::isupper(c) != 0; }; // Verify that there are at least two upper case letters in ctid and @@ -705,7 +705,7 @@ class Transaction_test : public beast::unit_test::Suite BEAST_EXPECT(jrr.isMember(jss::ctid) == (netID <= 0xFFFF)); if (jrr.isMember(jss::ctid)) { - auto const ctid = RPC::encodeCTID(ledgerSeq, 0, netID); + auto const ctid = rpc::encodeCTID(ledgerSeq, 0, netID); BEAST_EXPECT( jrr[jss::ctid] == *ctid); // NOLINT(bugprone-unchecked-optional-access) } @@ -725,7 +725,7 @@ class Transaction_test : public beast::unit_test::Suite env.close(); // NOLINTNEXTLINE(bugprone-unchecked-optional-access) - auto const ctid = *RPC::encodeCTID(startLegSeq, 0, netID + 1); + auto const ctid = *rpc::encodeCTID(startLegSeq, 0, netID + 1); json::Value jsonTx; jsonTx[jss::binary] = false; jsonTx[jss::ctid] = ctid; diff --git a/src/test/rpc/Version_test.cpp b/src/test/rpc/Version_test.cpp index 71830c2219..b5c1abc160 100644 --- a/src/test/rpc/Version_test.cpp +++ b/src/test/rpc/Version_test.cpp @@ -32,7 +32,7 @@ class Version_test : public beast::unit_test::Suite auto jrr = env.rpc( "json", "version", - "{\"api_version\": " + std::to_string(RPC::kApiMaximumSupportedVersion) + + "{\"api_version\": " + std::to_string(rpc::kApiMaximumSupportedVersion) + "}")[jss::result]; BEAST_EXPECT(isCorrectReply(jrr)); @@ -62,7 +62,7 @@ class Version_test : public beast::unit_test::Suite auto re = env.rpc( "json", "version", - "{\"api_version\": " + std::to_string(RPC::kApiMinimumSupportedVersion - 1) + "}"); + "{\"api_version\": " + std::to_string(rpc::kApiMinimumSupportedVersion - 1) + "}"); BEAST_EXPECT(badVersion(re)); BEAST_EXPECT(env.app().config().betaRpcApi); @@ -71,7 +71,7 @@ class Version_test : public beast::unit_test::Suite "version", "{\"api_version\": " + std::to_string( - std::max(RPC::kApiMaximumSupportedVersion.value, RPC::kApiBetaVersion.value) + + std::max(rpc::kApiMaximumSupportedVersion.value, rpc::kApiBetaVersion.value) + 1) + "}"); BEAST_EXPECT(badVersion(re)); @@ -86,38 +86,38 @@ class Version_test : public beast::unit_test::Suite testcase("test getAPIVersionNumber function"); unsigned int const versionIfUnspecified = - RPC::kApiVersionIfUnspecified < RPC::kApiMinimumSupportedVersion - ? RPC::kApiInvalidVersion - : RPC::kApiVersionIfUnspecified; + rpc::kApiVersionIfUnspecified < rpc::kApiMinimumSupportedVersion + ? rpc::kApiInvalidVersion + : rpc::kApiVersionIfUnspecified; json::Value const jArray = json::Value(json::ValueType::Array); json::Value const jNull = json::Value(json::ValueType::Null); - BEAST_EXPECT(RPC::getAPIVersionNumber(jArray, false) == versionIfUnspecified); - BEAST_EXPECT(RPC::getAPIVersionNumber(jNull, false) == versionIfUnspecified); + BEAST_EXPECT(rpc::getAPIVersionNumber(jArray, false) == versionIfUnspecified); + BEAST_EXPECT(rpc::getAPIVersionNumber(jNull, false) == versionIfUnspecified); json::Value jObject = json::Value(json::ValueType::Object); - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == versionIfUnspecified); - jObject[jss::api_version] = RPC::kApiVersionIfUnspecified.value; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == versionIfUnspecified); + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == versionIfUnspecified); + jObject[jss::api_version] = rpc::kApiVersionIfUnspecified.value; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == versionIfUnspecified); - jObject[jss::api_version] = RPC::kApiMinimumSupportedVersion.value; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == RPC::kApiMinimumSupportedVersion); - jObject[jss::api_version] = RPC::kApiMaximumSupportedVersion.value; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == RPC::kApiMaximumSupportedVersion); + jObject[jss::api_version] = rpc::kApiMinimumSupportedVersion.value; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == rpc::kApiMinimumSupportedVersion); + jObject[jss::api_version] = rpc::kApiMaximumSupportedVersion.value; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == rpc::kApiMaximumSupportedVersion); - jObject[jss::api_version] = RPC::kApiMinimumSupportedVersion - 1; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == RPC::kApiInvalidVersion); - jObject[jss::api_version] = RPC::kApiMaximumSupportedVersion + 1; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == RPC::kApiInvalidVersion); - jObject[jss::api_version] = RPC::kApiBetaVersion.value; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, true) == RPC::kApiBetaVersion); - jObject[jss::api_version] = RPC::kApiBetaVersion + 1; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, true) == RPC::kApiInvalidVersion); + jObject[jss::api_version] = rpc::kApiMinimumSupportedVersion - 1; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == rpc::kApiInvalidVersion); + jObject[jss::api_version] = rpc::kApiMaximumSupportedVersion + 1; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == rpc::kApiInvalidVersion); + jObject[jss::api_version] = rpc::kApiBetaVersion.value; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, true) == rpc::kApiBetaVersion); + jObject[jss::api_version] = rpc::kApiBetaVersion + 1; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, true) == rpc::kApiInvalidVersion); - jObject[jss::api_version] = RPC::kApiInvalidVersion.value; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == RPC::kApiInvalidVersion); + jObject[jss::api_version] = rpc::kApiInvalidVersion.value; + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == rpc::kApiInvalidVersion); jObject[jss::api_version] = "a"; - BEAST_EXPECT(RPC::getAPIVersionNumber(jObject, false) == RPC::kApiInvalidVersion); + BEAST_EXPECT(rpc::getAPIVersionNumber(jObject, false) == rpc::kApiInvalidVersion); } void @@ -141,7 +141,7 @@ class Version_test : public beast::unit_test::Suite "\"method\": \"version\", " "\"params\": { " "\"api_version\": " + - std::to_string(RPC::kApiMaximumSupportedVersion) + "}}"; + std::to_string(rpc::kApiMaximumSupportedVersion) + "}}"; auto re = env.rpc("json2", '[' + withoutApiVerion + ", " + withApiVerion + ']'); if (!BEAST_EXPECT(re.isArray())) @@ -176,7 +176,7 @@ class Version_test : public beast::unit_test::Suite "\"params\": { " "\"api_version\": " + std::to_string( - std::max(RPC::kApiMaximumSupportedVersion.value, RPC::kApiBetaVersion.value) + 1) + + std::max(rpc::kApiMaximumSupportedVersion.value, rpc::kApiBetaVersion.value) + 1) + "}}"; auto re = env.rpc("json2", '[' + withoutApiVerion + ", " + withWrongApiVerion + ']'); @@ -226,15 +226,15 @@ class Version_test : public beast::unit_test::Suite auto jrr = env.rpc( "json", "version", - "{\"api_version\": " + std::to_string(RPC::kApiBetaVersion) + "}")[jss::result]; + "{\"api_version\": " + std::to_string(rpc::kApiBetaVersion) + "}")[jss::result]; if (!BEAST_EXPECT(jrr.isMember(jss::version))) return; if (!BEAST_EXPECT(jrr[jss::version].isMember(jss::first)) && jrr[jss::version].isMember(jss::last)) return; - BEAST_EXPECT(jrr[jss::version][jss::first] == RPC::kApiMinimumSupportedVersion.value); - BEAST_EXPECT(jrr[jss::version][jss::last] == RPC::kApiBetaVersion.value); + BEAST_EXPECT(jrr[jss::version][jss::first] == rpc::kApiMinimumSupportedVersion.value); + BEAST_EXPECT(jrr[jss::version][jss::last] == rpc::kApiBetaVersion.value); } public: diff --git a/src/test/server/ServerStatus_test.cpp b/src/test/server/ServerStatus_test.cpp index 60ea622616..5adf6a08f5 100644 --- a/src/test/server/ServerStatus_test.cpp +++ b/src/test/server/ServerStatus_test.cpp @@ -558,10 +558,12 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En using namespace test::jtx; using namespace boost::asio; using namespace boost::beast::http; - Env env{*this, envconfig([&](std::unique_ptr cfg) { + // Run the server with a single io thread so disconnectClient() below + // can deterministically drain the server's io_context (see its docs). + Env env{*this, singleThreadIo(envconfig([&](std::unique_ptr cfg) { (*cfg)[Sections::kPortRpc].set(Keys::kLimit, std::to_string(limit)); return cfg; - })}; + }))}; auto const section = env.app().config().section(Sections::kPortRpc); // NOLINTBEGIN(bugprone-unchecked-optional-access) @@ -580,16 +582,27 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En BEAST_EXPECT(!ec); std::vector> clients; - int connectionCount{1}; // starts at 1 because the Env already has one - // for JSONRPCCLient - // for nonzero limits, go one past the limit, although failures happen - // at the limit, so this really leads to the last two clients failing. - // for zero limit, pick an arbitrary nonzero number of clients - all - // should connect fine. + // Env owns a persistent JSON-RPC HTTP client connection to port_rpc as + // part of startup, which counts against this port's connection limit. + // This test wants a known starting occupancy of zero, so for nonzero + // limits it deterministically drops that hidden client and waits for + // the server to register the disconnect before opening its own clients. + // + // Starting from zero is important because the port limit rejects once + // the incremented connection count reaches the configured limit. With a + // zero baseline and N = limit + 1 test-owned clients, exactly the last + // two requests should be rejected. + if (limit != 0) + BEAST_EXPECT(env.disconnectClient()); + + // For nonzero limits, go one past the limit. The port rejects at the + // limit, not only above it, so this yields the last two clients + // failing. For zero limit, pick an arbitrary nonzero number of clients + // and expect them all to succeed. int const testTo = (limit == 0) ? 50 : limit + 1; - while (connectionCount < testTo) + while (static_cast(clients.size()) < testTo) { clients.emplace_back(ip::tcp::socket{ios}, boost::beast::multi_buffer{}); async_connect(clients.back().first, it, yield[ec]); @@ -597,19 +610,24 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En auto req = makeHTTPRequest(ip, port, to_string(jr), {}); async_write(clients.back().first, req, yield[ec]); BEAST_EXPECT(!ec); - ++connectionCount; } - int readCount = 0; + int successfulReads = 0; for (auto& [soc, buf] : clients) { boost::beast::http::response resp; async_read(soc, buf, resp, yield[ec]); - ++readCount; - // expect the reads to fail for the clients that connected at or - // above the limit. If limit is 0, all reads should succeed - BEAST_EXPECT((limit == 0 || readCount < limit - 1) ? (!ec) : bool(ec)); + if (!ec) + ++successfulReads; } + + // This test cares about the exact number of accepted requests, not which + // specific client observed the rejection. With a zero baseline (the + // hidden Env client dropped above), the server accepts until the + // connection count reaches the limit: all clients for limit 0, else + // limit - 1 of the limit + 1 clients (the last two are rejected). + int const expectedReads = (limit == 0) ? static_cast(clients.size()) : limit - 1; + BEAST_EXPECT(successfulReads == expectedReads); } void diff --git a/src/tests/libxrpl/CMakeLists.txt b/src/tests/libxrpl/CMakeLists.txt index df9abe5e18..f04065aff8 100644 --- a/src/tests/libxrpl/CMakeLists.txt +++ b/src/tests/libxrpl/CMakeLists.txt @@ -32,10 +32,13 @@ add_dependencies(xrpl_tests xrpl_crates) # supported on Windows. set(test_modules basics + beast consensus crypto json + nodestore peerfinder + protocol resource shamap tx diff --git a/src/tests/libxrpl/basics/IntrusiveShared.cpp b/src/tests/libxrpl/basics/IntrusiveShared.cpp index e798cd1ccc..b9f8930b7b 100644 --- a/src/tests/libxrpl/basics/IntrusiveShared.cpp +++ b/src/tests/libxrpl/basics/IntrusiveShared.cpp @@ -50,11 +50,11 @@ struct Barrier { std::mutex mtx; std::condition_variable cv; - int count; - int const initial; + std::size_t count; + std::size_t const initial; std::size_t generation{0}; - explicit Barrier(int n) : count(n), initial(n) + explicit Barrier(std::size_t n) : count(n), initial(n) { } @@ -217,7 +217,7 @@ TEST(IntrusiveSharedTest, basics) auto id = b->id; EXPECT_EQ(TIBase::getState(id), Alive); EXPECT_EQ(b->useCount(), 1); - for (int i = 0; i < 10; ++i) + for (auto i = 0uz; i < 10; ++i) { strong.push_back(b); } @@ -232,7 +232,7 @@ TEST(IntrusiveSharedTest, basics) id = b->id; EXPECT_EQ(TIBase::getState(id), Alive); EXPECT_EQ(b->useCount(), 1); - for (int i = 0; i < 10; ++i) + for (auto i = 0uz; i < 10; ++i) { weak.emplace_back(b); EXPECT_EQ(b->useCount(), 1); @@ -280,17 +280,17 @@ TEST(IntrusiveSharedTest, basics) TIBase::ResetStatesGuard const rsg{true}; using enum TrackedState; - using swu = SharedWeakUnion; - swu b = makeSharedIntrusive(); + using SharedWeak = SharedWeakUnion; + SharedWeak b = makeSharedIntrusive(); EXPECT_TRUE(b.isStrong() && b.useCount() == 1); auto id = b.get()->id; EXPECT_EQ(TIBase::getState(id), Alive); - swu w = b; + SharedWeak w = b; EXPECT_TRUE(TIBase::getState(id) == Alive); EXPECT_TRUE(w.isStrong() && b.useCount() == 2); w.convertToWeak(); EXPECT_TRUE(w.isWeak() && b.useCount() == 1); - swu s = w; + SharedWeak s = w; EXPECT_TRUE(s.isWeak() && b.useCount() == 1); s.convertToStrong(); EXPECT_TRUE(s.isStrong() && b.useCount() == 2); @@ -380,43 +380,57 @@ TEST(IntrusiveSharedTest, partial_delete) std::atomic destructorRan{false}; std::atomic partialDeleteRan{false}; std::latch partialDeleteStartedSyncPoint{2}; + strong->tracingCallback = [&](TrackedState cur, std::optional next) { using enum TrackedState; - if (next == DeletedStarted) + if (!next) + return; + + switch (*next) { - // strong goes out of scope while weak is still in scope - // This checks that partialDelete has run to completion - // before the destructor is called. A sleep is inserted - // inside the partial delete to make sure the destructor is - // given an opportunity to run during partial delete. - EXPECT_EQ(cur, PartiallyDeleted); - } - if (next == PartiallyDeletedStarted) - { - partialDeleteStartedSyncPoint.arrive_and_wait(); - using namespace std::chrono_literals; - // Sleep and let the weak pointer go out of scope, - // potentially triggering a destructor while partial delete - // is running. The test is to make sure that doesn't happen. - std::this_thread::sleep_for(800ms); - } - if (next == PartiallyDeleted) - { - EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load()); - } - if (next == Deleted) - { - EXPECT_FALSE(destructorRan.exchange(true)); + case DeletedStarted: + // strong goes out of scope while weak is still in scope + // This checks that partialDelete has run to completion + // before the destructor is called. A sleep is inserted + // inside the partial delete to make sure the destructor is + // given an opportunity to run during partial delete. + EXPECT_EQ(cur, PartiallyDeleted); + break; + + case PartiallyDeletedStarted: { + partialDeleteStartedSyncPoint.arrive_and_wait(); + using namespace std::chrono_literals; + // Sleep and let the weak pointer go out of scope, + // potentially triggering a destructor while partial delete + // is running. The test is to make sure that doesn't happen. + std::this_thread::sleep_for(800ms); + break; + } + + case PartiallyDeleted: + EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load()); + break; + + case Deleted: + EXPECT_FALSE(destructorRan.exchange(true)); + break; + + case Uninitialized: + case Alive: + break; } }; + std::thread t1{[&] { partialDeleteStartedSyncPoint.arrive_and_wait(); weak.reset(); // Trigger a full delete as soon as the partial // delete starts }}; + std::thread t2{[&] { strong.reset(); // Trigger a partial delete }}; + t1.join(); t2.join(); @@ -444,13 +458,24 @@ TEST(IntrusiveSharedTest, destructor) std::latch weakResetSyncPoint{2}; strong->tracingCallback = [&](TrackedState cur, std::optional next) { using enum TrackedState; - if (next == PartiallyDeleted) + if (!next) + return; + + switch (*next) { - EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load()); - } - if (next == Deleted) - { - EXPECT_FALSE(destructorRan.exchange(true)); + case PartiallyDeleted: + EXPECT_FALSE(partialDeleteRan.exchange(true) || destructorRan.load()); + break; + + case Deleted: + EXPECT_FALSE(destructorRan.exchange(true)); + break; + + case Uninitialized: + case Alive: + case PartiallyDeletedStarted: + case DeletedStarted: + break; } }; std::thread t1{[&] { @@ -492,25 +517,36 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_variant) auto tracingCallback = [&](TrackedState cur, std::optional next) { using enum TrackedState; auto [destructorRan, partialDeleteRan] = getDestructorState(); - if (next == PartiallyDeleted) + if (!next) + return; + + switch (*next) { - EXPECT_FALSE(partialDeleteRan || destructorRan); - setPartialDeleteRan(); - } - if (next == Deleted) - { - EXPECT_FALSE(destructorRan); - setDestructorRan(); + case PartiallyDeleted: + EXPECT_FALSE(partialDeleteRan || destructorRan); + setPartialDeleteRan(); + break; + + case Deleted: + EXPECT_FALSE(destructorRan); + setDestructorRan(); + break; + + case Uninitialized: + case Alive: + case PartiallyDeletedStarted: + case DeletedStarted: + break; } }; auto createVecOfPointers = [&](auto const& toClone, std::default_random_engine& eng) -> std::vector, WeakIntrusive>> { std::vector, WeakIntrusive>> result; - std::uniform_int_distribution<> toCreateDist(4, 64); + std::uniform_int_distribution toCreateDist(4, 64); std::uniform_int_distribution<> isStrongDist(0, 1); auto numToCreate = toCreateDist(eng); result.reserve(numToCreate); - for (int i = 0; i < numToCreate; ++i) + for (auto i = 0uz; i < numToCreate; ++i) { if (isStrongDist(eng)) { @@ -523,8 +559,8 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_variant) } return result; }; - constexpr int kLoopIters = 2 * 1024; - constexpr int kNumThreads = 16; + constexpr auto kLoopIters = 2uz * 1024; + constexpr auto kNumThreads = 16uz; std::vector> toClone; Barrier loopStartSyncPoint{kNumThreads}; Barrier postCreateToCloneSyncPoint{kNumThreads}; @@ -533,7 +569,7 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_variant) std::random_device rd; std::vector result; result.reserve(kNumThreads); - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) result.emplace_back(rd()); return result; }(); @@ -541,8 +577,8 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_variant) // cloneAndDestroy clones the strong pointer into a vector of mixed // strong and weak pointers and destroys them all at once. // threadId==0 is special. - auto cloneAndDestroy = [&](int threadId) { - for (int i = 0; i < kLoopIters; ++i) + auto cloneAndDestroy = [&](std::size_t threadId) { + for (auto i = 0uz; i < kLoopIters; ++i) { // ------ Sync Point ------ loopStartSyncPoint.arriveAndWait(); @@ -582,11 +618,11 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_variant) }; std::vector threads; threads.reserve(kNumThreads); - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) { threads.emplace_back(cloneAndDestroy, i); } - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) { threads[i].join(); } @@ -623,31 +659,42 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_union) auto tracingCallback = [&](TrackedState cur, std::optional next) { using enum TrackedState; auto [destructorRan, partialDeleteRan] = getDestructorState(); - if (next == PartiallyDeleted) + if (!next) + return; + + switch (*next) { - EXPECT_FALSE(partialDeleteRan || destructorRan); - setPartialDeleteRan(); - } - if (next == Deleted) - { - EXPECT_FALSE(destructorRan); - setDestructorRan(); + case PartiallyDeleted: + EXPECT_FALSE(partialDeleteRan || destructorRan); + setPartialDeleteRan(); + break; + + case Deleted: + EXPECT_FALSE(destructorRan); + setDestructorRan(); + break; + + case Uninitialized: + case Alive: + case PartiallyDeletedStarted: + case DeletedStarted: + break; } }; auto createVecOfPointers = [&](auto const& toClone, std::default_random_engine& eng) -> std::vector> { std::vector> result; - std::uniform_int_distribution<> toCreateDist(4, 64); + std::uniform_int_distribution toCreateDist(4, 64); auto numToCreate = toCreateDist(eng); result.reserve(numToCreate); - for (int i = 0; i < numToCreate; ++i) + for (auto i = 0uz; i < numToCreate; ++i) result.emplace_back(SharedIntrusive(toClone)); return result; }; - constexpr int kLoopIters = 2 * 1024; - constexpr int kFlipPointersLoopIters = 256; - constexpr int kNumThreads = 16; + constexpr auto kLoopIters = 2uz * 1024; + constexpr auto kFlipPointersLoopIters = 256uz; + constexpr auto kNumThreads = 16uz; std::vector> toClone; Barrier loopStartSyncPoint{kNumThreads}; Barrier postCreateToCloneSyncPoint{kNumThreads}; @@ -657,7 +704,7 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_union) std::random_device rd; std::vector result; result.reserve(kNumThreads); - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) result.emplace_back(rd()); return result; }(); @@ -666,8 +713,8 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_union) // mixed strong and weak pointers, runs a loop that randomly // changes strong pointers to weak pointers, and destroys them // all at once. - auto cloneAndDestroy = [&](int threadId) { - for (int i = 0; i < kLoopIters; ++i) + auto cloneAndDestroy = [&](std::size_t threadId) { + for (auto i = 0uz; i < kLoopIters; ++i) { // ------ Sync Point ------ loopStartSyncPoint.arriveAndWait(); @@ -702,7 +749,7 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_union) postCreateVecOfPointersSyncPoint.arriveAndWait(); std::uniform_int_distribution<> isStrongDist(0, 1); - for (int f = 0; f < kFlipPointersLoopIters; ++f) + for (auto f = 0uz; f < kFlipPointersLoopIters; ++f) { for (auto& p : v) { @@ -725,11 +772,11 @@ TEST(IntrusiveSharedTest, multithreaded_clear_mixed_union) }; std::vector threads; threads.reserve(kNumThreads); - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) { threads.emplace_back(cloneAndDestroy, i); } - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) { threads[i].join(); } @@ -761,21 +808,32 @@ TEST(IntrusiveSharedTest, multithreaded_locking_weak) auto tracingCallback = [&](TrackedState cur, std::optional next) { using enum TrackedState; auto [destructorRan, partialDeleteRan] = getDestructorState(); - if (next == PartiallyDeleted) + if (!next) + return; + + switch (*next) { - EXPECT_FALSE(partialDeleteRan || destructorRan); - setPartialDeleteRan(); - } - if (next == Deleted) - { - EXPECT_FALSE(destructorRan); - setDestructorRan(); + case PartiallyDeleted: + EXPECT_FALSE(partialDeleteRan || destructorRan); + setPartialDeleteRan(); + break; + + case Deleted: + EXPECT_FALSE(destructorRan); + setDestructorRan(); + break; + + case Uninitialized: + case Alive: + case PartiallyDeletedStarted: + case DeletedStarted: + break; } }; - constexpr int kLoopIters = 2 * 1024; - constexpr int kLockWeakLoopIters = 256; - constexpr int kNumThreads = 16; + constexpr auto kLoopIters = 2uz * 1024; + constexpr auto kLockWeakLoopIters = 256uz; + constexpr auto kNumThreads = 16uz; std::vector> toLock; Barrier loopStartSyncPoint{kNumThreads}; Barrier postCreateToLockSyncPoint{kNumThreads}; @@ -784,8 +842,8 @@ TEST(IntrusiveSharedTest, multithreaded_locking_weak) // lockAndDestroy creates weak pointers from the strong pointer // and runs a loop that locks the weak pointer. At the end of the loop // all the pointers are destroyed all at once. - auto lockAndDestroy = [&](int threadId) { - for (int i = 0; i < kLoopIters; ++i) + auto lockAndDestroy = [&](std::size_t threadId) { + for (auto i = 0uz; i < kLoopIters; ++i) { // ------ Sync Point ------ loopStartSyncPoint.arriveAndWait(); @@ -816,7 +874,7 @@ TEST(IntrusiveSharedTest, multithreaded_locking_weak) // Multiple threads all create a weak pointer from the same // strong pointer WeakIntrusive const weak{toLock[threadId]}; - for (int wi = 0; wi < kLockWeakLoopIters; ++wi) + for (auto wi = 0uz; wi < kLockWeakLoopIters; ++wi) { EXPECT_FALSE(weak.expired()); auto strong = weak.lock(); @@ -831,11 +889,11 @@ TEST(IntrusiveSharedTest, multithreaded_locking_weak) }; std::vector threads; threads.reserve(kNumThreads); - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) { threads.emplace_back(lockAndDestroy, i); } - for (int i = 0; i < kNumThreads; ++i) + for (auto i = 0uz; i < kNumThreads; ++i) { threads[i].join(); } diff --git a/src/tests/libxrpl/basics/MallocTrim.cpp b/src/tests/libxrpl/basics/MallocTrim.cpp index 6ac8957f0e..52151262b0 100644 --- a/src/tests/libxrpl/basics/MallocTrim.cpp +++ b/src/tests/libxrpl/basics/MallocTrim.cpp @@ -199,7 +199,7 @@ TEST(mallocTrim, repeated_calls) beast::Journal const journal{beast::Journal::getNullSink()}; // Call malloc_trim multiple times to ensure it's safe - for (int i = 0; i < 5; ++i) + for (auto i = 0uz; i < 5; ++i) { MallocTrimReport const report = mallocTrim("iteration_" + std::to_string(i), journal); diff --git a/src/tests/libxrpl/basics/Number.cpp b/src/tests/libxrpl/basics/Number.cpp index 36e1b4a700..32f93eb1f7 100644 --- a/src/tests/libxrpl/basics/Number.cpp +++ b/src/tests/libxrpl/basics/Number.cpp @@ -323,11 +323,11 @@ TEST(NumberTest, add) __LINE__, }, { - // Does not round. Mantissas are going to be > maxRep, so if + // Does not round. Mantissas are going to be > kMaxRep, so if // added together as uint64_t's, the result will overflow. // With addition using uint128_t, there's no problem. After // normalizing, the resulting mantissa ends up less than - // maxRep. + // kMaxRep. Number{false, 9'999'999'999'999'999'990ULL, 0, Number::Normalized{}}, Number{false, 9'999'999'999'999'999'990ULL, 0, Number::Normalized{}}, Number{false, 1'999'999'999'999'999'998ULL, 1, Number::Normalized{}}, @@ -1078,14 +1078,6 @@ TEST(NumberTest, root) EXPECT_EQ(result, z) << ss.str(); } }; - /* - auto tests = [&](auto const& cSmall, auto const& cLarge) { - test(cSmall); - if (scale != MantissaRange::mantissa_scale::small) - test(cLarge); - }; - */ - auto const cSmall = std::to_array( {{Number{2}, 2, Number{1414213562373095049, -18}}, {Number{2'000'000}, 2, Number{1414213562373095049, -15}}, @@ -1511,7 +1503,7 @@ TEST(NumberTest, to_string) NumberRoundModeGuard const mg(Number::RoundingMode::TowardsZero); auto const maxMantissa = Number::maxMantissa(); - EXPECT_EQ(maxMantissa, (9'999'999'999'999'999)); + EXPECT_EQ(maxMantissa, 9'999'999'999'999'999); test( Number{false, (maxMantissa * 1000) + 999, -3, Number::Normalized()}, "9999999999999999", @@ -1550,7 +1542,7 @@ TEST(NumberTest, to_string) NumberRoundModeGuard const mg(Number::RoundingMode::TowardsZero); auto const maxMantissa = Number::maxMantissa(); - EXPECT_EQ((maxMantissa), (9'999'999'999'999'999'999ULL)); + EXPECT_EQ(maxMantissa, 9'999'999'999'999'999'999ULL); test( Number{false, maxMantissa, 0, Number::Normalized{}}, "9999999999999999990", diff --git a/src/tests/libxrpl/basics/base58.cpp b/src/tests/libxrpl/basics/base58.cpp index d452453f76..d6b1d2c3f9 100644 --- a/src/tests/libxrpl/basics/base58.cpp +++ b/src/tests/libxrpl/basics/base58.cpp @@ -151,7 +151,7 @@ randomBigInt(std::uint8_t minSize = 1, std::uint8_t maxSize = 5) auto const numCoeff = numCoeffDist(eng); std::vector coeffs; coeffs.reserve(numCoeff); - for (int i = 0; i < numCoeff; ++i) + for (auto i = 0uz; i < numCoeff; ++i) { coeffs.push_back(dist(eng)); } @@ -167,7 +167,7 @@ TEST(Base58Test, multiprecision) auto eng = randEngine(); std::uniform_int_distribution dist; std::uniform_int_distribution dist1(1); - for (int i = 0; i < kIters; ++i) + for (auto i = 0uz; i < kIters; ++i) { std::uint64_t const d = dist(eng); if (d == 0u) @@ -185,7 +185,7 @@ TEST(Base58Test, multiprecision) EXPECT_EQ(refMod.convert_to(), mod); EXPECT_EQ(foundDiv, refDiv); } - for (int i = 0; i < kIters; ++i) + for (auto i = 0uz; i < kIters; ++i) { std::uint64_t const d = dist(eng); auto bigInt = multiprecision_utils::randomBigInt(/*minSize*/ 2); @@ -204,7 +204,7 @@ TEST(Base58Test, multiprecision) auto const foundAdd = multiprecision_utils::toBoostMP(bigInt); EXPECT_EQ(refAdd, foundAdd); } - for (int i = 0; i < kIters; ++i) + for (auto i = 0uz; i < kIters; ++i) { std::uint64_t const d = dist1(eng); // Force overflow @@ -221,7 +221,7 @@ TEST(Base58Test, multiprecision) auto const foundAdd = multiprecision_utils::toBoostMP(bigInt); EXPECT_NE(refAdd, foundAdd); } - for (int i = 0; i < kIters; ++i) + for (auto i = 0uz; i < kIters; ++i) { std::uint64_t const d = dist(eng); auto bigInt = multiprecision_utils::randomBigInt(/* minSize */ 2); @@ -239,7 +239,7 @@ TEST(Base58Test, multiprecision) auto const foundMul = multiprecision_utils::toBoostMP(bigInt); EXPECT_EQ(refMul, foundMul); } - for (int i = 0; i < kIters; ++i) + for (auto i = 0uz; i < kIters; ++i) { std::uint64_t const d = dist1(eng); // Force overflow @@ -265,7 +265,7 @@ TEST(Base58Test, fast_matches_ref) std::array b256ResultBuf[2]; std::array, 2> b256Result; - for (int i = 0; i < 2; ++i) + for (auto i = 0uz; i < 2; ++i) { std::span const outBuf{b58ResultBuf[i]}; if (i == 0) @@ -297,7 +297,7 @@ TEST(Base58Test, fast_matches_ref) } } - for (int i = 0; i < 2; ++i) + for (auto i = 0uz; i < 2; ++i) { std::span const outBuf{b256ResultBuf[i].data(), b256ResultBuf[i].size()}; if (i == 0) @@ -339,7 +339,7 @@ TEST(Base58Test, fast_matches_ref) std::array b256ResultBuf[2]; std::array, 2> b256Result; - for (int i = 0; i < 2; ++i) + for (auto i = 0uz; i < 2; ++i) { std::span const outBuf{b58ResultBuf[i].data(), b58ResultBuf[i].size()}; if (i == 0) @@ -370,7 +370,7 @@ TEST(Base58Test, fast_matches_ref) } } - for (int i = 0; i < 2; ++i) + for (auto i = 0uz; i < 2; ++i) { std::span const outBuf{b256ResultBuf[i].data(), b256ResultBuf[i].size()}; if (i == 0) @@ -425,7 +425,7 @@ TEST(Base58Test, fast_matches_ref) // test with random data constexpr std::size_t kIters = 100000; - for (int i = 0; i < kIters; ++i) + for (auto i = 0uz; i < kIters; ++i) { std::array b256DataBuf{}; auto const [tokType, b256Data] = randomB256TestData(b256DataBuf); diff --git a/src/tests/libxrpl/basics/base_uint.cpp b/src/tests/libxrpl/basics/base_uint.cpp index 174cc33aa0..10795f4563 100644 --- a/src/tests/libxrpl/basics/base_uint.cpp +++ b/src/tests/libxrpl/basics/base_uint.cpp @@ -59,65 +59,67 @@ struct BaseUintTest : public ::testing::Test static void testComparisons() { - { - static constexpr std::array, 6> kTestArgs{ - {{"0000000000000000", "0000000000000001"}, - {"0000000000000000", "ffffffffffffffff"}, - {"1234567812345678", "2345678923456789"}, - {"8000000000000000", "8000000000000001"}, - {"aaaaaaaaaaaaaaa9", "aaaaaaaaaaaaaaaa"}, - {"fffffffffffffffe", "ffffffffffffffff"}}}; + using HexPair = std::pair; - for (auto const& arg : kTestArgs) + { + static constexpr auto kTestArgs = std::to_array({ + {"0000000000000000", "0000000000000001"}, + {"0000000000000000", "ffffffffffffffff"}, + {"1234567812345678", "2345678923456789"}, + {"8000000000000000", "8000000000000001"}, + {"aaaaaaaaaaaaaaa9", "aaaaaaaaaaaaaaaa"}, + {"fffffffffffffffe", "ffffffffffffffff"}, + }); + + for (auto const& [smallerText, largerText] : kTestArgs) { - xrpl::BaseUInt<64> const u{arg.first}, v{arg.second}; + xrpl::BaseUInt<64> const smaller{smallerText}, larger{largerText}; // For code readability, we want to use general boolean // expectations instead of specific EXPECT_LT etc. - EXPECT_TRUE(u < v); - EXPECT_TRUE(u <= v); - EXPECT_TRUE(u != v); - EXPECT_FALSE(u == v); - EXPECT_FALSE(u > v); - EXPECT_FALSE(u >= v); - EXPECT_FALSE(v < u); - EXPECT_FALSE(v <= u); - EXPECT_TRUE(v != u); - EXPECT_FALSE(v == u); - EXPECT_TRUE(v > u); - EXPECT_TRUE(v >= u); - EXPECT_TRUE(u == u); - EXPECT_TRUE(v == v); + EXPECT_TRUE(smaller < larger); + EXPECT_TRUE(smaller <= larger); + EXPECT_TRUE(smaller != larger); + EXPECT_FALSE(smaller == larger); + EXPECT_FALSE(smaller > larger); + EXPECT_FALSE(smaller >= larger); + EXPECT_FALSE(larger < smaller); + EXPECT_FALSE(larger <= smaller); + EXPECT_TRUE(larger != smaller); + EXPECT_FALSE(larger == smaller); + EXPECT_TRUE(larger > smaller); + EXPECT_TRUE(larger >= smaller); + EXPECT_TRUE(smaller == smaller); + EXPECT_TRUE(larger == larger); } } { - static constexpr std::array, 6> kTestArgs{ - { - {"000000000000000000000000", "000000000000000000000001"}, - {"000000000000000000000000", "ffffffffffffffffffffffff"}, - {"0123456789ab0123456789ab", "123456789abc123456789abc"}, - {"555555555555555555555555", "55555555555a555555555555"}, - {"aaaaaaaaaaaaaaa9aaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaa"}, - {"fffffffffffffffffffffffe", "ffffffffffffffffffffffff"}, - }}; + static constexpr auto kTestArgs = std::to_array({ + {"000000000000000000000000", "000000000000000000000001"}, + {"000000000000000000000000", "ffffffffffffffffffffffff"}, + {"0123456789ab0123456789ab", "123456789abc123456789abc"}, + {"555555555555555555555555", "55555555555a555555555555"}, + {"aaaaaaaaaaaaaaa9aaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaa"}, + {"fffffffffffffffffffffffe", "ffffffffffffffffffffffff"}, + }); - for (auto const& arg : kTestArgs) + for (auto const& [smallerText, largerText] : kTestArgs) { - xrpl::BaseUInt<96> const u{arg.first}, v{arg.second}; - EXPECT_TRUE(u < v); - EXPECT_TRUE(u <= v); - EXPECT_TRUE(u != v); - EXPECT_FALSE(u == v); - EXPECT_FALSE(u > v); - EXPECT_FALSE(u >= v); - EXPECT_FALSE(v < u); - EXPECT_FALSE(v <= u); - EXPECT_TRUE(v != u); - EXPECT_FALSE(v == u); - EXPECT_TRUE(v > u); - EXPECT_TRUE(v >= u); - EXPECT_TRUE(u == u); - EXPECT_TRUE(v == v); + xrpl::BaseUInt<96> const smaller{smallerText}, larger{largerText}; + EXPECT_TRUE(smaller < larger); + EXPECT_TRUE(smaller <= larger); + EXPECT_TRUE(smaller != larger); + EXPECT_FALSE(smaller == larger); + EXPECT_FALSE(smaller > larger); + EXPECT_FALSE(smaller >= larger); + EXPECT_FALSE(larger < smaller); + EXPECT_FALSE(larger <= smaller); + EXPECT_TRUE(larger != smaller); + EXPECT_FALSE(larger == smaller); + EXPECT_TRUE(larger > smaller); + EXPECT_TRUE(larger >= smaller); + EXPECT_TRUE(smaller == smaller); + EXPECT_TRUE(larger == larger); } } } @@ -401,14 +403,14 @@ TEST_F(BaseUintTest, base_uint) { } }; - constexpr StrBaseUInt kTestCases[] = { + constexpr auto kTestCases = std::to_array({ "000000000000000000000000", "000000000000000000000001", "fedcba9876543210ABCDEF91", "19FEDCBA0123456789abcdef", "800000000000000000000000", "fFfFfFfFfFfFfFfFfFfFfFfF", - }; + }); for (StrBaseUInt const& t : kTestCases) { diff --git a/src/tests/libxrpl/basics/join.cpp b/src/tests/libxrpl/basics/join.cpp index 66c832678b..427f0b42bc 100644 --- a/src/tests/libxrpl/basics/join.cpp +++ b/src/tests/libxrpl/basics/join.cpp @@ -19,11 +19,11 @@ struct JoinTest : public ::testing::Test TEST_F(JoinTest, join) { - auto test = [](auto collectionanddelimiter, std::string expected) { + auto test = [](auto collectionAndDelimiter, std::string expected) { std::stringstream ss; // Put something else in the buffer before and after to ensure that // the << operator returns the stream correctly. - ss << "(" << collectionanddelimiter << ")"; + ss << "(" << collectionAndDelimiter << ")"; auto const str = ss.str(); EXPECT_EQ(str.substr(1, str.length() - 2), expected); EXPECT_EQ(str.front(), '('); diff --git a/src/tests/libxrpl/beast/LexicalCast.cpp b/src/tests/libxrpl/beast/LexicalCast.cpp new file mode 100644 index 0000000000..d18af4e1cd --- /dev/null +++ b/src/tests/libxrpl/beast/LexicalCast.cpp @@ -0,0 +1,339 @@ +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace beast { +namespace { + +template +[[nodiscard]] constexpr bool +parses(std::string_view text) +{ + T out{}; + return lexicalCastChecked(out, text); +} + +template +[[nodiscard]] constexpr T +parsed(std::string_view text) +{ + T out{}; + return lexicalCastChecked(out, text) ? out : T{}; +} + +template +constexpr T kMax = std::numeric_limits::max(); + +template +constexpr T kMin = std::numeric_limits::min(); + +template +constexpr T kUnderMax = kMax - 1; + +template +constexpr T kOverMin = kMin + 1; + +// Comfortably inside the range, not boundary values. +constexpr auto kNearMax32 = kMax - 5; +constexpr auto kNearMin32 = kMin + 4; +constexpr auto kUnderInt64Max = uint64_t{kMax} - 1; +constexpr auto kInRangeInt16 = int16_t{-5711}; + +// No wider integer type can hold these, so ToString cannot produce them. +constexpr auto kAboveUint64Max = "18446744073709551616"; +constexpr auto kBelowInt64Min = "-9223372036854775809"; + +// Out of range for every integer type we test. +constexpr auto kTwentyNines = "99999999999999999999"; +constexpr auto kNegativeTwentyNines = "-99999999999999999999"; + +// Arbitrary values chosen to sit well outside a type's range, not just over it. +constexpr auto kAboveUint16Max = "75821"; +constexpr auto kBelowInt16Min = "-75821"; +constexpr auto kAboveInt32Max = "5294967295"; +constexpr auto kAboveInt16Max = "66666"; + +constexpr auto kPositiveInt32 = int32_t{42}; +constexpr auto kNegativeInt32 = int32_t{-42}; + +constexpr auto kPositiveInt32Text = "+42"; +constexpr auto kNegativeInt32Text = "-42"; + +constexpr auto kNegativeOne = "-1"; +constexpr auto kNegativeZero = "-0"; +constexpr auto kBareZero = "0"; +constexpr auto kPositiveZero = "+0"; + +// Full-width digits one and zero, not ASCII ones. +constexpr std::string_view kFullWidthDigits = "\xef\xbc\x91\xef\xbc\x90"; + +// The decimal text of a value, usable in a constant expression. +template +struct ToString +{ + std::array buffer{}; + std::size_t length{}; + + constexpr explicit ToString(T value) + { + auto const result = std::to_chars(buffer.data(), buffer.data() + buffer.size(), value); + length = static_cast(result.ptr - buffer.data()); + } + + constexpr + operator std::string_view() const + { + return {buffer.data(), length}; + } +}; + +template +constexpr auto kMaxText = ToString{kMax}; + +template +constexpr auto kUnderMaxText = ToString{kUnderMax}; + +template +constexpr auto kOverMaxText = ToString{Wider{kMax} + 1}; + +template +constexpr auto kMinText = ToString{kMin}; + +template +constexpr auto kOverMinText = ToString{kOverMin}; + +template +constexpr auto kUnderMinText = ToString{Wider{kMin} - 1}; + +constexpr auto kOverUint32MaxText = ToString{uint64_t{kMax} + 5}; +constexpr auto kNegatedOverUint32MaxText = ToString{-(int64_t{kMax} + 5)}; + +// lexicalCastThrow deduces its input type, so the text has to be an explicit +// string_view rather than a ToString. +template +[[nodiscard]] constexpr T +castThrow(Value value) +{ + return lexicalCastThrow(std::string_view{ToString{value}}); +} + +template +[[nodiscard]] bool +roundTrips(std::string_view text) +{ + T out{}; + return lexicalCastChecked(out, text) && std::to_string(out) == text; +} + +template +void +expectRoundTrip(T value) +{ + SCOPED_TRACE(::testing::Message() << "value: " << value); + + auto const text = lexicalCast(value); + EXPECT_EQ(text, std::to_string(value)); + + auto decoded = static_cast(~value); // ensure decoded != value + EXPECT_TRUE(lexicalCastChecked(decoded, text)); + EXPECT_EQ(decoded, value); +} + +} // namespace + +// int/unsigned/short/unsigned short are covered by the list below — they are +// these exact types everywhere we build. +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); + +using IntegerTypes = ::testing::Types< // + int16_t, + uint16_t, + int32_t, + uint32_t, + int64_t, + uint64_t>; + +struct IntegerTypeNames +{ + template + static std::string + // NOLINTNEXTLINE(readability-identifier-naming) - required by gtest + GetName(int) + { + return (std::is_signed_v ? "int" : "uint") + std::to_string(sizeof(T) * 8) + "_t"; + } +}; + +template +class LexicalCastIntegers : public ::testing::Test +{ +}; + +TYPED_TEST_SUITE(LexicalCastIntegers, IntegerTypes, IntegerTypeNames); + +TYPED_TEST(LexicalCastIntegers, round_trips_random_values) +{ + static constexpr auto kSampleCount = 1000uz; + + xor_shift_engine r{50}; // seeded per test so a failure reproduces on its own + + for (auto i = 0uz; i < kSampleCount; ++i) + expectRoundTrip(static_cast(r())); +} + +TYPED_TEST(LexicalCastIntegers, round_trips_numeric_limits) +{ + expectRoundTrip(std::numeric_limits::min()); + expectRoundTrip(std::numeric_limits::max()); +} + +TEST(LexicalCast, round_trips_every_int16_value) +{ + for (int32_t i = kMin; i <= kMax; ++i) + { + auto const value = static_cast(i); + + // ASSERT, or a broken cast reports all 65536 iterations. + auto const text = lexicalCast(value); + ASSERT_EQ(text, std::to_string(value)); + ASSERT_EQ(lexicalCast(text), value); + } +} + +TEST(LexicalCast, rejects_overflow) +{ + static_assert(not parses(kOverUint32MaxText)); + static_assert(not parses(kTwentyNines)); + static_assert(not parses(kAboveUint16Max)); +} + +TEST(LexicalCast, rejects_underflow) +{ + static_assert(not parses(kNegativeOne)); + static_assert(not parses(kNegatedOverUint32MaxText)); + static_assert(not parses(kNegativeTwentyNines)); + static_assert(not parses(kBelowInt16Min)); +} + +TEST(LexicalCast, accepts_up_to_the_maximum) +{ + static_assert(parsed(kUnderMaxText) == kUnderMax); + static_assert(parsed(kMaxText) == kMax); + static_assert(not parses(kOverMaxText)); + + static_assert(parsed(kUnderMaxText) == kUnderMax); + static_assert(parsed(kMaxText) == kMax); + static_assert(not parses(kOverMaxText)); + + static_assert(parsed(kUnderMaxText) == kUnderMax); + static_assert(parsed(kMaxText) == kMax); + static_assert(not parses(kOverMaxText)); + + static_assert(parsed(kUnderMaxText) == kUnderMax); + static_assert(parsed(kMaxText) == kMax); + static_assert(not parses(kOverMaxText)); + + static_assert(parsed(kUnderMaxText) == kUnderMax); + static_assert(parsed(kMaxText) == kMax); + static_assert(not parses(kOverMaxText)); + + static_assert(parsed(kUnderMaxText) == kUnderMax); + static_assert(parsed(kMaxText) == kMax); + static_assert(not parses(kAboveUint64Max)); +} + +TEST(LexicalCast, accepts_down_to_the_minimum) +{ + static_assert(parsed(kOverMinText) == kOverMin); + static_assert(parsed(kMinText) == kMin); + static_assert(not parses(kUnderMinText)); + + static_assert(parsed(kOverMinText) == kOverMin); + static_assert(parsed(kMinText) == kMin); + static_assert(not parses(kUnderMinText)); + + static_assert(parsed(kOverMinText) == kOverMin); + static_assert(parsed(kMinText) == kMin); + static_assert(not parses(kBelowInt64Min)); +} + +TEST(LexicalCast, limits_round_trip_through_to_string) +{ + EXPECT_TRUE(roundTrips(kMaxText)); + EXPECT_TRUE(roundTrips(kMaxText)); + EXPECT_TRUE(roundTrips(kMinText)); + EXPECT_TRUE(roundTrips(kMaxText)); + EXPECT_TRUE(roundTrips(kMinText)); + EXPECT_TRUE(roundTrips(kMaxText)); + EXPECT_TRUE(roundTrips(kMinText)); +} + +TEST(LexicalCast, accepts_signed_zero_in_every_form) +{ + static_assert(parsed(kNegativeZero) == 0); + static_assert(parsed(kBareZero) == 0); + static_assert(parsed(kPositiveZero) == 0); +} + +TEST(LexicalCast, rejects_negative_zero_when_unsigned) +{ + static_assert(not parses(kNegativeZero)); + static_assert(parsed(kBareZero) == 0); + static_assert(parsed(kPositiveZero) == 0); +} + +TEST(LexicalCast, accepts_char_pointer_and_std_string_input) +{ + int32_t fromLiteral = 0; + EXPECT_TRUE(lexicalCastChecked(fromLiteral, kPositiveInt32Text)); + EXPECT_EQ(fromLiteral, kPositiveInt32); + + int32_t fromString = 0; + EXPECT_TRUE(lexicalCastChecked(fromString, std::string{kNegativeInt32Text})); + EXPECT_EQ(fromString, kNegativeInt32); +} + +TEST(LexicalCast, throwing_cast_returns_in_range_values) +{ + static_assert(castThrow(kUnderInt64Max) == kUnderInt64Max); + static_assert(castThrow(kNearMax32) == kNearMax32); + static_assert(castThrow(kNearMin32) == kNearMin32); + static_assert(castThrow(kInRangeInt16) == kInRangeInt16); +} + +TEST(LexicalCast, throwing_cast_throws_on_out_of_range) +{ + EXPECT_THROW(lexicalCastThrow(kTwentyNines), BadLexicalCast); + + // kNearMax32 with digits appended, so each is further past uint32_t's range. + for (auto const scale : {10, 100, 1000}) + { + auto const tooBig = ToString{uint64_t{kNearMax32} * scale}; + EXPECT_THROW(lexicalCastThrow(std::string_view{tooBig}), BadLexicalCast); + } + + EXPECT_THROW(lexicalCastThrow(kAboveInt32Max), BadLexicalCast); + EXPECT_THROW(lexicalCastThrow(kAboveInt16Max), BadLexicalCast); +} + +// Full-width digits, not ASCII ones. +TEST(LexicalCast, throwing_cast_throws_on_utf8_digits) +{ + EXPECT_THROW(lexicalCastThrow(kFullWidthDigits), BadLexicalCast); +} + +} // namespace beast diff --git a/src/tests/libxrpl/beast/SemanticVersion.cpp b/src/tests/libxrpl/beast/SemanticVersion.cpp new file mode 100644 index 0000000000..21b33c9476 --- /dev/null +++ b/src/tests/libxrpl/beast/SemanticVersion.cpp @@ -0,0 +1,333 @@ +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace beast { +namespace { + +using IdentifierList = SemanticVersion::IdentifierList; + +// Version strings are not valid C++ identifiers, so squash their punctuation to +// turn one into a gtest parameter name. +std::string +identifierFor(std::string_view version) +{ + std::string name{version}; + std::ranges::replace_if( + name, [](char c) { return !std::isalnum(c, std::locale::classic()); }, '_'); + if (!name.empty() && std::isdigit(name.front(), std::locale::classic())) + name.insert(0, "v_"); + return name; +} + +// Pre-release and metadata suffixes, each applied to a "major.minor.patch" base. +// The valid ones leave a well-formed base well-formed; the invalid ones make any +// base malformed. +constexpr auto kValidPreRelease = + std::to_array({"", "-1", "-a", "-a1", "-a1.b1", "-ab.cd", "--"}); +constexpr auto kInvalidPreRelease = + std::to_array({"+", "!", "-", "-!", "-.", "-a.!", "-0.a"}); +constexpr auto kValidMetaData = std::to_array({"", "+a", "+1", "+a.b", "+ab.cd"}); +constexpr auto kInvalidMetaData = + std::to_array({"!", "+", "++", "+!", "+.", "+a.!"}); + +// Assembles base + preRelease + metaData and checks whether it parses. A version +// we accept must also round-trip through print(). +void +expectParse( + std::string_view base, + std::string_view preRelease, + std::string_view metaData, + bool shouldPass) +{ + auto const input = std::string{base}.append(preRelease).append(metaData); + SCOPED_TRACE(::testing::Message() << '"' << input << '"'); + + SemanticVersion v; + + if (shouldPass) + { + EXPECT_TRUE(v.parse(input)); + EXPECT_EQ(v.print(), input); + } + else + { + EXPECT_FALSE(v.parse(input)); + } +} + +struct ParseCase +{ + std::string_view testName; + std::string_view base; + bool shouldPass; +}; + +std::string +parseCaseName(::testing::TestParamInfo const& info) +{ + return std::string{info.param.testName}; +} + +constexpr auto kParseCases = std::to_array({ + {.testName = "zeroes", .base = "0.0.0", .shouldPass = true}, + {.testName = "simple", .base = "1.2.3", .shouldPass = true}, + {.testName = "max_int", .base = "2147483647.2147483647.2147483647", .shouldPass = true}, + + // negative values + {.testName = "negative_major", .base = "-1.2.3", .shouldPass = false}, + {.testName = "negative_minor", .base = "1.-2.3", .shouldPass = false}, + {.testName = "negative_patch", .base = "1.2.-3", .shouldPass = false}, + + // missing parts + {.testName = "empty", .base = "", .shouldPass = false}, + {.testName = "major_only", .base = "1", .shouldPass = false}, + {.testName = "major_then_dot", .base = "1.", .shouldPass = false}, + {.testName = "major_and_minor", .base = "1.2", .shouldPass = false}, + {.testName = "major_minor_then_dot", .base = "1.2.", .shouldPass = false}, + {.testName = "missing_major", .base = ".2.3", .shouldPass = false}, + + // whitespace + {.testName = "leading_space", .base = " 1.2.3", .shouldPass = false}, + {.testName = "space_after_major", .base = "1 .2.3", .shouldPass = false}, + {.testName = "space_after_minor", .base = "1.2 .3", .shouldPass = false}, + {.testName = "trailing_space", .base = "1.2.3 ", .shouldPass = false}, + + // leading zeroes + {.testName = "leading_zero_in_major", .base = "01.2.3", .shouldPass = false}, + {.testName = "leading_zero_in_minor", .base = "1.02.3", .shouldPass = false}, + {.testName = "leading_zero_in_patch", .base = "1.2.03", .shouldPass = false}, +}); + +struct ValuesCase +{ + std::string_view testName; + std::string_view input; + int majorVersion; + int minorVersion; + int patchVersion; + IdentifierList preReleaseIdentifiers{}; // NOLINT(readability-redundant-member-init) + IdentifierList metaData{}; // NOLINT(readability-redundant-member-init) +}; + +std::string +valuesCaseName(::testing::TestParamInfo const& info) +{ + return std::string{info.param.testName}; +} + +std::vector const kValuesCases{ + { + .testName = "zero_major", + .input = "0.1.2", + .majorVersion = 0, + .minorVersion = 1, + .patchVersion = 2, + }, + { + .testName = "simple", + .input = "1.2.3", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + }, + { + .testName = "one_pre_release_identifier", + .input = "1.2.3-rc1", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + .preReleaseIdentifiers = {"rc1"}, + }, + { + .testName = "two_pre_release_identifiers", + .input = "1.2.3-rc1.debug", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + .preReleaseIdentifiers = {"rc1", "debug"}, + }, + { + .testName = "three_pre_release_identifiers", + .input = "1.2.3-rc1.debug.asm", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + .preReleaseIdentifiers = {"rc1", "debug", "asm"}, + }, + { + .testName = "one_metadata_identifier", + .input = "1.2.3+full", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + .metaData = {"full"}, + }, + { + .testName = "two_metadata_identifiers", + .input = "1.2.3+full.prod", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + .metaData = {"full", "prod"}, + }, + { + .testName = "three_metadata_identifiers", + .input = "1.2.3+full.prod.x86", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + .metaData = {"full", "prod", "x86"}, + }, + { + .testName = "pre_release_and_metadata", + .input = "1.2.3-rc1.debug.asm+full.prod.x86", + .majorVersion = 1, + .minorVersion = 2, + .patchVersion = 3, + .preReleaseIdentifiers = {"rc1", "debug", "asm"}, + .metaData = {"full", "prod", "x86"}, + }, +}; + +struct OrderCase +{ + std::string_view lesser; + std::string_view greater; +}; + +std::string +orderCaseName(::testing::TestParamInfo const& info) +{ + return identifierFor(info.param.lesser) + "_below_" + identifierFor(info.param.greater); +} + +constexpr auto kOrderCases = std::to_array({ + {.lesser = "1.0.0-alpha", .greater = "1.0.0-alpha.1"}, + {.lesser = "1.0.0-alpha.1", .greater = "1.0.0-alpha.beta"}, + {.lesser = "1.0.0-alpha.beta", .greater = "1.0.0-beta"}, + {.lesser = "1.0.0-beta", .greater = "1.0.0-beta.2"}, + {.lesser = "1.0.0-beta.2", .greater = "1.0.0-beta.11"}, + {.lesser = "1.0.0-beta.11", .greater = "1.0.0-rc.1"}, + {.lesser = "1.0.0-rc.1", .greater = "1.0.0"}, + {.lesser = "0.9.9", .greater = "1.0.0"}, +}); + +} // namespace + +class SemanticVersionParse : public ::testing::TestWithParam +{ +}; + +// Exercises the base string on its own and with every combination of appended +// pre-release identifiers and metadata. +TEST_P(SemanticVersionParse, pre_release_and_metadata_combinations) +{ + auto const& [testName, base, shouldPass] = GetParam(); + + for (auto const preRelease : kValidPreRelease) + { + for (auto const metaData : kValidMetaData) + expectParse(base, preRelease, metaData, shouldPass); + + for (auto const metaData : kInvalidMetaData) + expectParse(base, preRelease, metaData, false); + } + + // A malformed pre-release section poisons the whole string, whatever + // metadata follows it. + for (auto const preRelease : kInvalidPreRelease) + { + for (auto const metaData : kValidMetaData) + expectParse(base, preRelease, metaData, false); + + for (auto const metaData : kInvalidMetaData) + expectParse(base, preRelease, metaData, false); + } +} + +INSTANTIATE_TEST_SUITE_P( + Inputs, + SemanticVersionParse, + ::testing::ValuesIn(kParseCases), + parseCaseName); + +class SemanticVersionValues : public ::testing::TestWithParam +{ +}; + +TEST_P(SemanticVersionValues, decomposes_into_components) +{ + auto const& expected = GetParam(); + + SemanticVersion v; + EXPECT_TRUE(v.parse(expected.input)); + + EXPECT_EQ(v.majorVersion, expected.majorVersion); + EXPECT_EQ(v.minorVersion, expected.minorVersion); + EXPECT_EQ(v.patchVersion, expected.patchVersion); + + EXPECT_EQ(v.preReleaseIdentifiers, expected.preReleaseIdentifiers); + EXPECT_EQ(v.metaData, expected.metaData); +} + +INSTANTIATE_TEST_SUITE_P( + Inputs, + SemanticVersionValues, + ::testing::ValuesIn(kValuesCases), + valuesCaseName); + +class SemanticVersionOrder : public ::testing::TestWithParam +{ +}; + +TEST_P(SemanticVersionOrder, lesser_precedes_greater) +{ + auto const& [lesser, greater] = GetParam(); + + // Metadata takes no part in precedence, so attaching it to either side must + // leave the ordering untouched. + static constexpr auto kMetaData = std::to_array({"", "+meta"}); + + for (auto const lesserMetaData : kMetaData) + { + for (auto const greaterMetaData : kMetaData) + { + auto const lesserInput = std::string{lesser}.append(lesserMetaData); + auto const greaterInput = std::string{greater}.append(greaterMetaData); + SCOPED_TRACE( + ::testing::Message() << '"' << lesserInput << "\" < \"" << greaterInput << '"'); + + SemanticVersion lesserVersion; + SemanticVersion greaterVersion; + EXPECT_TRUE(lesserVersion.parse(lesserInput)); + EXPECT_TRUE(greaterVersion.parse(greaterInput)); + + EXPECT_EQ(compare(lesserVersion, lesserVersion), 0); + EXPECT_EQ(compare(greaterVersion, greaterVersion), 0); + EXPECT_LT(compare(lesserVersion, greaterVersion), 0); + EXPECT_GT(compare(greaterVersion, lesserVersion), 0); + + EXPECT_LT(lesserVersion, greaterVersion); + EXPECT_GT(greaterVersion, lesserVersion); + EXPECT_EQ(lesserVersion, lesserVersion); + EXPECT_EQ(greaterVersion, greaterVersion); + } + } +} + +INSTANTIATE_TEST_SUITE_P( + Pairs, + SemanticVersionOrder, + ::testing::ValuesIn(kOrderCases), + orderCaseName); + +} // namespace beast diff --git a/src/tests/libxrpl/beast/Zero.cpp b/src/tests/libxrpl/beast/Zero.cpp new file mode 100644 index 0000000000..2ac725509a --- /dev/null +++ b/src/tests/libxrpl/beast/Zero.cpp @@ -0,0 +1,92 @@ +#include + +#include + +namespace beast { + +struct AdlTester +{ +}; + +int +signum(AdlTester) +{ + return 0; +} + +namespace inner_adl_test { + +struct AdlTester2 +{ +}; + +int +signum(AdlTester2) +{ + return 0; +} + +} // namespace inner_adl_test + +namespace { + +struct IntegerWrapper +{ + int value; + + IntegerWrapper(int v) : value(v) + { + } + + [[nodiscard]] int + signum() const + { + return value; + } +}; + +void +testLhsZero(IntegerWrapper x) +{ + EXPECT_EQ(x >= kZero, x.signum() >= 0); + EXPECT_EQ(x > kZero, x.signum() > 0); + EXPECT_EQ(x == kZero, x.signum() == 0); + EXPECT_EQ(x != kZero, x.signum() != 0); + EXPECT_EQ(x < kZero, x.signum() < 0); + EXPECT_EQ(x <= kZero, x.signum() <= 0); +} + +void +testRhsZero(IntegerWrapper x) +{ + EXPECT_EQ(kZero >= x, 0 >= x.signum()); + EXPECT_EQ(kZero > x, 0 > x.signum()); + EXPECT_EQ(kZero == x, 0 == x.signum()); + EXPECT_EQ(kZero != x, 0 != x.signum()); + EXPECT_EQ(kZero < x, 0 < x.signum()); + EXPECT_EQ(kZero <= x, 0 <= x.signum()); +} + +} // namespace + +TEST(Zero, lhs) +{ + testLhsZero(-7); + testLhsZero(0); + testLhsZero(32); +} + +TEST(Zero, rhs) +{ + testRhsZero(-4); + testRhsZero(0); + testRhsZero(64); +} + +TEST(Zero, adl) +{ + EXPECT_TRUE(AdlTester{} == kZero); + EXPECT_TRUE(inner_adl_test::AdlTester2{} == kZero); +} + +} // namespace beast diff --git a/src/tests/libxrpl/consensus/CensorshipDetector.cpp b/src/tests/libxrpl/consensus/CensorshipDetector.cpp index aa6b2d086b..2c6b6ec731 100644 --- a/src/tests/libxrpl/consensus/CensorshipDetector.cpp +++ b/src/tests/libxrpl/consensus/CensorshipDetector.cpp @@ -69,7 +69,7 @@ TEST(CensorshipDetectorTest, censorship_detector) runRound(cdet, ++round, {23, 24, 25, 26}, {25, 27}, {23, 26}, {24}); runRound(cdet, ++round, {23, 26, 28}, {26, 28}, {23}, {}); - for (int i = 0; i != 10; ++i) + for (auto i = 0uz; i != 10; ++i) runRound(cdet, ++round, {23}, {}, {23}, {}); runRound(cdet, ++round, {23, 29}, {29}, {23}, {}); diff --git a/src/tests/libxrpl/csf/TrustGraph.h b/src/tests/libxrpl/csf/TrustGraph.h index d010b954e0..8a804fcd5b 100644 --- a/src/tests/libxrpl/csf/TrustGraph.h +++ b/src/tests/libxrpl/csf/TrustGraph.h @@ -118,9 +118,9 @@ public: std::vector res; // Loop over all pairs of uniqueUNLs - for (int i = 0; i < uniqueUNLs.size(); ++i) + for (auto i = 0uz; i < uniqueUNLs.size(); ++i) { - for (int j = (i + 1); j < uniqueUNLs.size(); ++j) + for (auto j = i + 1; j < uniqueUNLs.size(); ++j) { auto const& unlA = uniqueUNLs[i]; auto const& unlB = uniqueUNLs[j]; diff --git a/src/tests/libxrpl/csf/random.h b/src/tests/libxrpl/csf/random.h index 007bdecb1b..56838bb280 100644 --- a/src/tests/libxrpl/csf/random.h +++ b/src/tests/libxrpl/csf/random.h @@ -24,11 +24,12 @@ randomWeightedShuffle(std::vector v, std::vector w, G& g) { using std::swap; - for (int i = 0; i < v.size() - 1; ++i) + for (auto i = 0uz; i + 1 < v.size(); ++i) { - // pick a random item weighted by w - std::discrete_distribution<> dd(w.begin() + i, w.end()); // NOLINT(misc-const-correctness) - auto idx = dd(g); + // Pick a random item from the unplaced tail, weighted by w. + // NOLINTNEXTLINE(misc-const-correctness) + std::discrete_distribution dd(w.begin() + i, w.end()); + auto const idx = i + dd(g); std::swap(v[i], v[idx]); std::swap(w[i], w[idx]); } diff --git a/src/tests/libxrpl/helpers/CaptureSink.h b/src/tests/libxrpl/helpers/CaptureSink.h new file mode 100644 index 0000000000..9918d13f9e --- /dev/null +++ b/src/tests/libxrpl/helpers/CaptureSink.h @@ -0,0 +1,50 @@ +#pragma once + +#include + +#include +#include +#include + +namespace xrpl::test { + +class CaptureSink : public beast::Journal::Sink +{ + mutable std::mutex mutex_; + std::stringstream strm_; + +public: + explicit CaptureSink(beast::Severity threshold = beast::Severity::Debug) + : Sink{threshold, false} + { + } + + void + write(beast::Severity level, std::string const& text) override + { + if (level < threshold()) + return; + writeAlways(level, text); + } + + void + writeAlways(beast::Severity /*level*/, std::string const& text) override + { + // Journal sinks may be written to concurrently (e.g. from a backend's background workers), + // so serialize access to strm_. write() funnels into writeAlways(), so the lock lives here + // only: locking in both would self-deadlock on this non-recursive mutex. + std::scoped_lock const lock(mutex_); + strm_ << text << '\n'; + } + + [[nodiscard]] std::string + messages() const + { + // Returns a snapshot of the captured output. Takes the lock so the read is safe even if a + // writer is still active. + std::scoped_lock const lock(mutex_); + return strm_.str(); + } +}; + +} // namespace xrpl::test diff --git a/src/tests/libxrpl/helpers/TestFamily.h b/src/tests/libxrpl/helpers/TestFamily.h index 1a11d3bb68..8a599ab4da 100644 --- a/src/tests/libxrpl/helpers/TestFamily.h +++ b/src/tests/libxrpl/helpers/TestFamily.h @@ -29,11 +29,11 @@ namespace xrpl::test { class TestFamily : public Family { private: - std::unique_ptr db_; + std::unique_ptr db_; TestStopwatch clock_; std::shared_ptr fbCache_; std::shared_ptr tnCache_; - NodeStore::DummyScheduler scheduler_; + node_store::DummyScheduler scheduler_; beast::Journal j_; public: @@ -51,16 +51,16 @@ public: Section config; config.set(Keys::kType, "memory"); config.set(Keys::kPath, "TestFamily"); - db_ = NodeStore::Manager::instance().makeDatabase(megabytes(4), scheduler_, 1, config, j); + db_ = node_store::Manager::instance().makeDatabase(megabytes(4), scheduler_, 1, config, j); } - NodeStore::Database& + node_store::Database& db() override { return *db_; } - [[nodiscard]] NodeStore::Database const& + [[nodiscard]] node_store::Database const& db() const override { return *db_; diff --git a/src/tests/libxrpl/helpers/TestServiceRegistry.h b/src/tests/libxrpl/helpers/TestServiceRegistry.h index 5475b54dc6..e763c8bde4 100644 --- a/src/tests/libxrpl/helpers/TestServiceRegistry.h +++ b/src/tests/libxrpl/helpers/TestServiceRegistry.h @@ -213,14 +213,14 @@ public: throw std::logic_error("TestServiceRegistry::peerReservations() not implemented"); } - Resource::Manager& + resource::Manager& getResourceManager() override { throw std::logic_error("TestServiceRegistry::getResourceManager() not implemented"); } // Storage services - NodeStore::Database& + node_store::Database& getNodeStore() override { throw std::logic_error("TestServiceRegistry::getNodeStore() not implemented"); diff --git a/src/tests/libxrpl/nodestore/Backend.cpp b/src/tests/libxrpl/nodestore/Backend.cpp new file mode 100644 index 0000000000..eb78851429 --- /dev/null +++ b/src/tests/libxrpl/nodestore/Backend.cpp @@ -0,0 +1,182 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace xrpl::node_store { + +namespace { + +std::vector +backendTypes() +{ + std::vector types{"nudb"}; +#if XRPL_ROCKSDB_AVAILABLE + types.emplace_back("rocksdb"); +#endif +#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS + types.emplace_back("sqlite"); +#endif + return types; +} + +// Run work(i) for every i in [0, n) spread across numThreads threads, handing +// out indices via a shared atomic counter (mirrors the old Timing_test +// parallel-for so the N items are partitioned, not duplicated). +template +void +parallelFor(std::size_t n, std::size_t numThreads, Work work) +{ + std::atomic next{0}; + auto const runner = [&] { + for (std::size_t i = next++; i < n; i = next++) + work(i); + }; + + auto threads = std::views::iota(std::size_t{0}, numThreads) | + std::views::transform([&](std::size_t) { return std::thread{runner}; }) | + std::ranges::to(); + + std::ranges::for_each(threads, &std::thread::join); +} + +} // namespace + +class BackendTypeTest : public ::testing::TestWithParam +{ +protected: + void + SetUp() override + { + params_.set("type", GetParam()); + params_.set("path", tempDir_.path()); + + beast::xor_shift_engine rng(kSeedValue); + batch_ = createPredictableBatch(kNumObjects, rng()); + } + + std::unique_ptr + makeOpenBackend() + { + auto backend = Manager::instance().makeBackend(params_, megabytes(4), scheduler_, journal_); + backend->open(); + return backend; + } + + DummyScheduler scheduler_; + beast::TempDir const tempDir_; + beast::Journal const journal_{TestSink::instance()}; + Section params_; + Batch batch_; +}; + +TEST_P(BackendTypeTest, store_and_fetch) +{ + auto backend = makeOpenBackend(); + storeBatch(*backend, batch_); + + { + SCOPED_TRACE("read in original order"); + auto const copy = fetchCopyOfBatch(*backend, batch_); + EXPECT_EQ(batch_, copy); + } + + { + SCOPED_TRACE("read in shuffled order"); + beast::xor_shift_engine rng(kSeedValue); + std::shuffle(batch_.begin(), batch_.end(), rng); + auto const copy = fetchCopyOfBatch(*backend, batch_); + EXPECT_EQ(batch_, copy); + } +} + +TEST_P(BackendTypeTest, persists_after_reopen) +{ + { + auto backend = makeOpenBackend(); + storeBatch(*backend, batch_); + } + + // re-open a fresh backend instance over the same path + auto backend = makeOpenBackend(); + auto copy = fetchCopyOfBatch(*backend, batch_); + std::ranges::sort(batch_, LessThan{}); + std::ranges::sort(copy, LessThan{}); + EXPECT_EQ(batch_, copy); +} + +// missing-key path. Replaces the correctness half of Timing_test::doMissing +// (and the missing branch of doMixed): every fetch on an empty backend must +// report Status::NotFound. +TEST_P(BackendTypeTest, fetch_missing) +{ + auto backend = makeOpenBackend(); + // deliberately do NOT store batch_ — every key must be absent + fetchMissing(*backend, batch_); +} + +// concurrent store/fetch correctness. Replaces the correctness half of the +// multi-threaded Timing_test workloads (which only ran manually, never in CI): +// many threads store disjoint objects, then many threads fetch and verify each +// round-trips. Doubles as a thread-safety smoke test for the backend. +TEST_P(BackendTypeTest, concurrent_store_and_fetch) +{ + // The SQLite backend is not designed for concurrent writers (and the old + // Timing_test only exercised nudb/rocksdb under threads). + if (GetParam() == "sqlite") + GTEST_SKIP() << "sqlite backend is not exercised under concurrency"; + + for (auto const numThreads : {4uz, 8uz}) + { + SCOPED_TRACE("threads=" + std::to_string(numThreads)); + + auto backend = makeOpenBackend(); + + // concurrent stores of disjoint objects + parallelFor(batch_.size(), numThreads, [&](std::size_t i) { backend->store(batch_[i]); }); + + // concurrent fetches, each verifying its object round-trips. Worker + // threads only touch an atomic counter; the EXPECT runs on the main + // thread after join to avoid relying on cross-thread assertion support. + std::atomic mismatches{0}; + parallelFor(batch_.size(), numThreads, [&](std::size_t i) { + std::shared_ptr result; + if (backend->fetch(batch_[i]->getHash(), &result) != Status::Ok || !result || + !isSame(result, batch_[i])) + { + ++mismatches; + } + }); + EXPECT_EQ(mismatches.load(), 0u); + + backend->close(); + } +} + +INSTANTIATE_TEST_SUITE_P( + BackendTypes, + BackendTypeTest, + ::testing::ValuesIn(backendTypes()), + [](::testing::TestParamInfo const& info) { return info.param; }); + +} // namespace xrpl::node_store diff --git a/src/tests/libxrpl/nodestore/Basics.cpp b/src/tests/libxrpl/nodestore/Basics.cpp new file mode 100644 index 0000000000..5bb902af0d --- /dev/null +++ b/src/tests/libxrpl/nodestore/Basics.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +#include +#include + +#include +#include +#include + +namespace xrpl::node_store { + +TEST(NodeStoreBasics, predictable_batches) +{ + auto const batch1 = createPredictableBatch(kNumObjectsToTest, kSeedValue); + auto const batch2 = createPredictableBatch(kNumObjectsToTest, kSeedValue); + EXPECT_EQ(batch1, batch2); + + auto const batch3 = createPredictableBatch(kNumObjectsToTest, kSeedValue + 1); + EXPECT_NE(batch1, batch3); +} + +TEST(NodeStoreBasics, blob_encoding) +{ + auto const batch = createPredictableBatch(kNumObjectsToTest, kSeedValue); + for (std::size_t i = 0; i < batch.size(); ++i) + { + SCOPED_TRACE("blob index=" + std::to_string(i)); + EncodedBlob const encoded(batch[i]); + DecodedBlob decoded(encoded.getKey(), encoded.getData(), encoded.getSize()); + EXPECT_TRUE(decoded.wasOk()); + if (decoded.wasOk()) + { + std::shared_ptr const object(decoded.createObject()); + EXPECT_TRUE(isSame(batch[i], object)); + } + } +} + +} // namespace xrpl::node_store diff --git a/src/tests/libxrpl/nodestore/Codec.cpp b/src/tests/libxrpl/nodestore/Codec.cpp new file mode 100644 index 0000000000..f31878f3c5 --- /dev/null +++ b/src/tests/libxrpl/nodestore/Codec.cpp @@ -0,0 +1,146 @@ +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace xrpl; +using namespace xrpl::node_store; + +namespace { + +// v1 inner-node layout: 16 hashes of 32 bytes each +constexpr std::size_t kHashCount = 16; +constexpr std::size_t kHashSize = 32; + +std::vector +makeInnerNode(std::size_t nonEmptySlots) +{ + using namespace nudb::detail; + + static constexpr std::size_t kInnerNodeSize = 525; + + std::array hashes{}; + for (auto slot = 0uz; slot < nonEmptySlots; ++slot) + { + for (auto byte = 0uz; byte < kHashSize; ++byte) + { + std::size_t const offset = (slot * kHashSize) + byte; + hashes[offset] = static_cast((offset % 255) + 1); + } + } + + std::vector blob(kInnerNodeSize); + ostream os(blob.data(), blob.size()); + write(os, 0); // index + write(os, 0); // unused + write(os, static_cast(NodeObjectType::Unknown)); + write(os, static_cast(HashPrefix::InnerNode)); + write(os, hashes.data(), hashes.size()); + + return blob; +} + +std::uint8_t +codecType(std::pair const& compressed) +{ + return static_cast(compressed.first)[0]; +} + +} // namespace + +// All 16 hash slots populated - "full v1 inner node" +TEST(Codec, inner_node_full_roundtrip) +{ + static constexpr std::uint8_t kTypeInnerNodeFull = 3; + + auto const blob = makeInnerNode(kHashCount); + + nudb::detail::buffer compressBuf; + auto const compressed = nodeobjectCompress(blob.data(), blob.size(), compressBuf); + + EXPECT_EQ(codecType(compressed), kTypeInnerNodeFull); + EXPECT_EQ(compressed.second, sizeVarint(kTypeInnerNodeFull) + (kHashCount * kHashSize)); + + nudb::detail::buffer decompressBuf; + auto const restored = nodeobjectDecompress(compressed.first, compressed.second, decompressBuf); + + EXPECT_EQ(restored.second, blob.size()); + EXPECT_EQ(std::memcmp(restored.first, blob.data(), blob.size()), 0); +} + +// Some hash slots empty - "compressed v1 inner node" +TEST(Codec, inner_node_compressed_roundtrip) +{ + static constexpr std::uint8_t kTypeInnerNodeCompressed = 2; + static constexpr std::size_t kNonEmpty = 5; + auto const blob = makeInnerNode(kNonEmpty); + + nudb::detail::buffer compressBuf; + auto const compressed = nodeobjectCompress(blob.data(), blob.size(), compressBuf); + + EXPECT_EQ(codecType(compressed), kTypeInnerNodeCompressed); + EXPECT_EQ( + compressed.second, + sizeVarint(kTypeInnerNodeCompressed) + sizeof(std::uint16_t) + (kNonEmpty * kHashSize)); + EXPECT_LT(compressed.second, blob.size()); + + nudb::detail::buffer decompressBuf; + auto const restored = nodeobjectDecompress(compressed.first, compressed.second, decompressBuf); + + EXPECT_EQ(restored.second, blob.size()); + EXPECT_EQ(std::memcmp(restored.first, blob.data(), blob.size()), 0); +} + +// Anything that is not a v1 inner node - lz4 compressed +TEST(Codec, lz4_roundtrip) +{ + // A payload that is deliberately not a v1 inner node (any size other than 525), filled with a + // short repeating pattern so lz4 actually shrinks it. + static constexpr std::size_t kNonInnerNodeSize = 1000; + static constexpr std::size_t kBytePatternPeriod = 7; + static constexpr std::uint8_t kTypeLz4 = 1; + + std::vector blob(kNonInnerNodeSize); + for (auto i = 0uz; i < blob.size(); ++i) + blob[i] = static_cast(i % kBytePatternPeriod); + + nudb::detail::buffer compressBuf; + auto const compressed = nodeobjectCompress(blob.data(), blob.size(), compressBuf); + + EXPECT_EQ(codecType(compressed), kTypeLz4); + + nudb::detail::buffer decompressBuf; + auto const restored = nodeobjectDecompress(compressed.first, compressed.second, decompressBuf); + + EXPECT_EQ(restored.second, blob.size()); + EXPECT_EQ(std::memcmp(restored.first, blob.data(), blob.size()), 0); +} + +// An uncompressed blob is never produced by the compressor but must still decode: leading varint 0 +// followed by the raw payload. +TEST(Codec, uncompressed_passthrough) +{ + static constexpr std::uint8_t kTypeUncompressed = 0; + static constexpr auto payload = std::to_array({0xde, 0xad, 0xbe, 0xef, 0x2a}); + + std::vector blob; + blob.push_back(kTypeUncompressed); // leading varint type tag + blob.insert(blob.end(), payload.begin(), payload.end()); + + nudb::detail::buffer decompressBuf; + auto const restored = nodeobjectDecompress(blob.data(), blob.size(), decompressBuf); + + EXPECT_EQ(restored.second, payload.size()); + EXPECT_EQ(std::memcmp(restored.first, payload.data(), payload.size()), 0); +} diff --git a/src/tests/libxrpl/nodestore/Database.cpp b/src/tests/libxrpl/nodestore/Database.cpp new file mode 100644 index 0000000000..82012ed347 --- /dev/null +++ b/src/tests/libxrpl/nodestore/Database.cpp @@ -0,0 +1,248 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace xrpl::node_store { + +namespace { + +std::vector +allBackends() +{ +#if XRPL_ROCKSDB_AVAILABLE + return {"memory", "nudb", "rocksdb"}; +#else + return {"memory", "nudb"}; +#endif +} + +std::vector +persistentBackends() +{ + std::vector types{"nudb"}; +#if XRPL_ROCKSDB_AVAILABLE + types.emplace_back("rocksdb"); +#endif + return types; +} + +std::vector +importBackends() +{ + std::vector types{"nudb"}; +#if XRPL_ROCKSDB_AVAILABLE + types.emplace_back("rocksdb"); +#endif +#ifdef XRPL_ENABLE_SQLITE_BACKEND_TESTS + types.emplace_back("sqlite"); +#endif + return types; +} + +} // namespace + +// Shared setup for the parameterized Database tests: builds the node params, +// journal and a predictable batch per test, mirroring Backend.cpp's fixture. +class NodeStoreDatabaseTestBase : public ::testing::TestWithParam +{ +protected: + void + SetUp() override + { + nodeParams_.set("type", GetParam()); + nodeParams_.set("path", nodeDb_.path()); + + beast::xor_shift_engine rng(kSeedValue); + batch_ = createPredictableBatch(kNumObjects, rng()); + } + + std::unique_ptr + makeDatabase() + { + return Manager::instance().makeDatabase(megabytes(4), scheduler_, 2, nodeParams_, journal_); + } + + DummyScheduler scheduler_; + beast::TempDir const nodeDb_; + beast::Journal const journal_{TestSink::instance()}; + Section nodeParams_; + Batch batch_; +}; + +class NodeStoreDatabaseTest : public NodeStoreDatabaseTestBase +{ +}; + +class NodeStoreDatabasePersistenceTest : public NodeStoreDatabaseTestBase +{ +}; + +TEST_P(NodeStoreDatabaseTest, store_and_fetch) +{ + auto db = makeDatabase(); + + storeBatch(*db, batch_); + + { + SCOPED_TRACE("read in original order"); + auto const copy = fetchCopyOfBatch(*db, batch_); + EXPECT_EQ(batch_, copy); + } + + { + SCOPED_TRACE("read in shuffled order"); + beast::xor_shift_engine rng(kSeedValue); + std::shuffle(batch_.begin(), batch_.end(), rng); + auto const copy = fetchCopyOfBatch(*db, batch_); + EXPECT_EQ(batch_, copy); + } +} + +TEST_P(NodeStoreDatabasePersistenceTest, round_trip) +{ + { + auto db = makeDatabase(); + storeBatch(*db, batch_); + } + + // re-open without the ephemeral db + auto db = makeDatabase(); + + auto copy = fetchCopyOfBatch(*db, batch_); + std::ranges::sort(batch_, LessThan{}); + std::ranges::sort(copy, LessThan{}); + EXPECT_EQ(batch_, copy); +} + +// missing-key path at the Database layer. Mirrors Backend's fetch_missing — +// fetching keys that were never stored must return nullptr (NotFound). +TEST_P(NodeStoreDatabaseTest, fetch_missing) +{ + auto db = makeDatabase(); + + // never store: every key must be absent + fetchMissing(*db, batch_); +} + +INSTANTIATE_TEST_SUITE_P( + NodeStoreBackends, + NodeStoreDatabaseTest, + ::testing::ValuesIn(allBackends()), + [](::testing::TestParamInfo const& info) { return info.param; }); + +INSTANTIATE_TEST_SUITE_P( + PersistentBackends, + NodeStoreDatabasePersistenceTest, + ::testing::ValuesIn(persistentBackends()), + [](::testing::TestParamInfo const& info) { return info.param; }); + +TEST(NodeStoreDatabase, memory_earliest_seq) +{ + DummyScheduler scheduler; + beast::TempDir const nodeDb; + Section nodeParams; + nodeParams.set("type", "memory"); + nodeParams.set("path", nodeDb.path()); + + beast::Journal const journal(TestSink::instance()); + + // default earliest ledger sequence + { + auto db = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal); + EXPECT_EQ(db->earliestLedgerSeq(), kXrpLedgerEarliestSeq); + } + + // invalid earliest_seq value + { + nodeParams.set("earliest_seq", "0"); + try + { + auto db = + Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal); + FAIL() << "expected runtime_error for earliest_seq=0"; + } + catch (std::runtime_error const& e) + { + EXPECT_STREQ(e.what(), "Invalid earliest_seq"); + } + } + + // valid earliest_seq value + { + nodeParams.set("earliest_seq", "1"); + auto db = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, nodeParams, journal); + EXPECT_EQ(db->earliestLedgerSeq(), 1u); + } +} + +class DatabaseImportTest : public ::testing::TestWithParam +{ +}; + +TEST_P(DatabaseImportTest, same_backend) +{ + auto const type = GetParam(); + + DummyScheduler scheduler; + beast::Journal const journal(TestSink::instance()); + + beast::TempDir const srcDir; + Section srcParams; + srcParams.set("type", type); + srcParams.set("path", srcDir.path()); + + auto batch = createPredictableBatch(kNumObjects, kSeedValue); + + // write to source db + { + auto src = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, srcParams, journal); + storeBatch(*src, batch); + } + + Batch copy; + { + // re-open source and import into a fresh destination + auto src = Manager::instance().makeDatabase(megabytes(4), scheduler, 2, srcParams, journal); + + beast::TempDir const destDir; + Section destParams; + destParams.set("type", type); + destParams.set("path", destDir.path()); + + auto dest = + Manager::instance().makeDatabase(megabytes(4), scheduler, 2, destParams, journal); + + dest->importDatabase(*src); + copy = fetchCopyOfBatch(*dest, batch); + } + + std::ranges::sort(batch, LessThan{}); + std::ranges::sort(copy, LessThan{}); + EXPECT_EQ(batch, copy); +} + +INSTANTIATE_TEST_SUITE_P( + ImportBackends, + DatabaseImportTest, + ::testing::ValuesIn(importBackends()), + [](::testing::TestParamInfo const& info) { return info.param; }); + +} // namespace xrpl::node_store diff --git a/src/tests/libxrpl/nodestore/NuDBFactory.cpp b/src/tests/libxrpl/nodestore/NuDBFactory.cpp new file mode 100644 index 0000000000..c126984630 --- /dev/null +++ b/src/tests/libxrpl/nodestore/NuDBFactory.cpp @@ -0,0 +1,297 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace xrpl::node_store { + +namespace { + +Section +makeSection(std::string const& path, std::string const& blockSize = "") +{ + Section params; + params.set("type", "nudb"); + params.set("path", path); + if (!blockSize.empty()) + params.set("nudb_block_size", blockSize); + return params; +} + +void +runRoundTrip(Section const& params, std::size_t expectedBlocksize) +{ + DummyScheduler scheduler; + beast::Journal const journal(TestSink::instance()); + auto backend = Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + + ASSERT_TRUE(backend); + ASSERT_EQ(backend->getBlockSize(), expectedBlocksize); + backend->open(); + ASSERT_TRUE(backend->isOpen()); + + auto const batch = createPredictableBatch(10, 12345); + storeBatch(*backend, batch); + + auto const copy = fetchCopyOfBatch(*backend, batch); + + backend->close(); + EXPECT_EQ(batch, copy); +} + +} // namespace + +TEST(NuDBFactory, default_block_size) +{ + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path()); + ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, 4096)); +} + +TEST(NuDBFactory, valid_block_sizes) +{ + auto const kValidSizes = std::to_array({4096, 8192, 16384, 32768}); + for (auto const size : kValidSizes) + { + SCOPED_TRACE("size=" + std::to_string(size)); + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), std::to_string(size)); + ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, size)); + } + + // empty value is ignored by config parser; default (4096) is used + { + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), ""); + ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, 4096)); + } +} + +TEST(NuDBFactory, invalid_block_sizes) +{ + std::vector const kInvalidSizes = { + "2048", // too small + "1024", // too small + "65536", // too large + "131072", // too large + "5000", // not power of 2 + "6000", // not power of 2 + "10000", // not power of 2 + "0", // zero + "-1", // negative + "abc", // non-numeric + "4k", // invalid format + "4096.5"}; // decimal + + for (auto const& size : kInvalidSizes) + { + SCOPED_TRACE("size='" + size + "'"); + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), size); + EXPECT_THROW(runRoundTrip(params, 4096), std::exception); + } + + // whitespace handling — lexical_cast may or may not strip; treat as invalid + std::vector const kWhitespaceSizes = {"4096 ", " 4096"}; + for (auto const& size : kWhitespaceSizes) + { + SCOPED_TRACE("size='" + size + "'"); + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), size); + EXPECT_THROW(runRoundTrip(params, 4096), std::exception); + } +} + +TEST(NuDBFactory, log_messages) +{ + // valid custom block size emits info log + { + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), "8192"); + test::CaptureSink sink(beast::Severity::Info); + beast::Journal const journal(sink); + + DummyScheduler scheduler; + [[maybe_unused]] auto backend = + Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + + EXPECT_TRUE(sink.messages().contains("Using custom NuDB block size: 8192")); + } + + // invalid block size throws with informative message + { + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), "5000"); + test::CaptureSink sink(beast::Severity::Warning); + beast::Journal const journal(sink); + DummyScheduler scheduler; + try + { + auto backend = + Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + FAIL() << "expected exception for invalid block size 5000"; + } + catch (std::exception const& e) + { + std::string const what{e.what()}; + EXPECT_TRUE(what.contains("Invalid nudb_block_size: 5000")); + EXPECT_TRUE(what.contains("Must be power of 2 between 4096 and 32768")); + } + } + + // non-numeric value throws + { + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), "invalid"); + test::CaptureSink sink(beast::Severity::Warning); + beast::Journal const journal(sink); + DummyScheduler scheduler; + try + { + auto backend = + Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + FAIL() << "expected exception for non-numeric block size"; + } + catch (std::exception const& e) + { + std::string const what{e.what()}; + EXPECT_TRUE(what.contains("Invalid nudb_block_size value: invalid")); + } + } +} + +TEST(NuDBFactory, power_of_two_validation) +{ + std::vector> const kCASES = { + {"4095", false}, // just below minimum + {"4096", true}, // minimum valid + {"4097", false}, // not power of 2 + {"8192", true}, // valid power of 2 + {"8193", false}, // not power of 2 + {"16384", true}, // valid power of 2 + {"32768", true}, // maximum valid + {"32769", false}, // just above maximum + {"65536", false}}; // power of 2 but too large + + for (auto const& [size, shouldWork] : kCASES) + { + SCOPED_TRACE("size=" + size + " shouldWork=" + (shouldWork ? "true" : "false")); + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), size); + test::CaptureSink sink(beast::Severity::Warning); + beast::Journal const journal(sink); + DummyScheduler scheduler; + try + { + auto backend = + Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + EXPECT_TRUE(shouldWork); + } + catch (std::exception const& e) + { + // A throw is only expected for sizes that should NOT work; if a + // valid size throws, fail here instead of silently matching the + // message below (which would mask the regression). + EXPECT_FALSE(shouldWork); + std::string const what{e.what()}; + EXPECT_TRUE(what.contains("Invalid nudb_block_size")); + } + } +} + +TEST(NuDBFactory, both_constructor_variants) +{ + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), "16384"); + DummyScheduler scheduler; + beast::Journal const journal(TestSink::instance()); + + auto backend1 = Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + EXPECT_NE(backend1, nullptr); + ASSERT_NO_FATAL_FAILURE(runRoundTrip(params, 16384)); + + // Test second constructor (with nudb::context) + // Note: This would require access to nudb::context, which might not be + // easily testable without more complex setup. For now, we test that + // the factory can create backends with the first constructor. +} + +TEST(NuDBFactory, configuration_parsing) +{ + // basic valid format emits success log + { + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), "8192"); + test::CaptureSink sink(beast::Severity::Info); + beast::Journal const journal(sink); + DummyScheduler scheduler; + [[maybe_unused]] auto backend = + Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + EXPECT_TRUE(sink.messages().contains("Using custom NuDB block size")); + } + + // Test whitespace handling separately since lexical_cast behavior may vary + std::vector const kWhitespaceFormats = {" 8192", "8192 "}; + for (auto const& format : kWhitespaceFormats) + { + SCOPED_TRACE("format='" + format + "'"); + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), format); + test::CaptureSink sink(beast::Severity::Debug); + beast::Journal const journal(sink); + DummyScheduler scheduler; + EXPECT_ANY_THROW(Manager::instance().makeBackend(params, megabytes(4), scheduler, journal)); + } +} + +TEST(NuDBFactory, data_persistence) +{ + std::vector const kBlockSizes = {"4096", "8192", "16384", "32768"}; + for (auto const& size : kBlockSizes) + { + SCOPED_TRACE("size=" + size); + beast::TempDir const tempDir; + auto const params = makeSection(tempDir.path(), size); + DummyScheduler scheduler; + beast::Journal const journal(TestSink::instance()); + + // Create test data + auto const batch = createPredictableBatch(50, 54321); + + // Store data + { + auto backend = + Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + backend->open(); + storeBatch(*backend, batch); + backend->close(); + } + + // Retrieve data in new backend instance + { + auto backend = + Manager::instance().makeBackend(params, megabytes(4), scheduler, journal); + backend->open(); + auto const copy = fetchCopyOfBatch(*backend, batch); + EXPECT_EQ(batch, copy); + backend->close(); + } + } +} + +} // namespace xrpl::node_store diff --git a/src/tests/libxrpl/nodestore/TestBase.h b/src/tests/libxrpl/nodestore/TestBase.h new file mode 100644 index 0000000000..5a262ac7bb --- /dev/null +++ b/src/tests/libxrpl/nodestore/TestBase.h @@ -0,0 +1,169 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +namespace xrpl::node_store { + +constexpr std::size_t kMinPayloadBytes = 1; +constexpr std::size_t kMaxPayloadBytes = 2000; +constexpr int kNumObjectsToTest = 2000; +constexpr int kNumObjects = 2000; +constexpr std::uint64_t kSeedValue = 50; + +struct LessThan +{ + bool + operator()(std::shared_ptr const& lhs, std::shared_ptr const& rhs) + const noexcept + { + return lhs->getHash() < rhs->getHash(); + } +}; + +[[nodiscard]] inline bool +isSame(std::shared_ptr const& lhs, std::shared_ptr const& rhs) +{ + return (lhs->getType() == rhs->getType()) && (lhs->getHash() == rhs->getHash()) && + (lhs->getData() == rhs->getData()); +} + +[[nodiscard]] inline Batch +createPredictableBatch(std::size_t numObjects, std::uint64_t seed) +{ + Batch batch; + batch.reserve(numObjects); + + beast::xor_shift_engine rng(seed); + + for (auto i = 0uz; i < numObjects; ++i) + { + NodeObjectType const type = [&] { + switch (randInt(rng, 3)) + { + case 0: + return NodeObjectType::Ledger; + case 1: + return NodeObjectType::AccountNode; + case 2: + return NodeObjectType::TransactionNode; + case 3: + default: + return NodeObjectType::Unknown; + } + }(); + + uint256 hash; + beast::rngfill(hash.begin(), hash.size(), rng); + + Blob blob(randInt(rng, kMinPayloadBytes, kMaxPayloadBytes)); + beast::rngfill(blob.data(), blob.size(), rng); + + batch.emplace_back(NodeObject::createObject(type, std::move(blob), hash)); + } + + return batch; +} + +inline void +storeBatch(Backend& backend, Batch const& batch) +{ + for (auto const& obj : batch) + backend.store(obj); +} + +[[nodiscard]] inline Batch +fetchCopyOfBatch(Backend& backend, Batch const& batch) +{ + Batch copy; + copy.reserve(batch.size()); + + for (auto i = 0uz; i < batch.size(); ++i) + { + SCOPED_TRACE("fetchCopyOfBatch index=" + std::to_string(i)); + std::shared_ptr object; + Status const status = backend.fetch(batch[i]->getHash(), &object); + EXPECT_EQ(status, Status::Ok); + if (status == Status::Ok) + { + EXPECT_NE(object, nullptr); + copy.emplace_back(object); + } + } + return copy; +} + +inline void +fetchMissing(Backend& backend, Batch const& batch) +{ + for (auto i = 0uz; i < batch.size(); ++i) + { + SCOPED_TRACE("fetchMissing index=" + std::to_string(i)); + std::shared_ptr object; + Status const status = backend.fetch(batch[i]->getHash(), &object); + EXPECT_EQ(status, Status::NotFound); + } +} + +inline void +storeBatch(Database& db, Batch const& batch) +{ + for (auto const& obj : batch) + { + Blob data(obj->getData()); + db.store(obj->getType(), std::move(data), obj->getHash(), db.earliestLedgerSeq()); + } +} + +[[nodiscard]] inline Batch +fetchCopyOfBatch(Database& db, Batch const& batch) +{ + Batch copy; + copy.reserve(batch.size()); + + for (auto const& obj : batch) + { + std::shared_ptr const result = db.fetchNodeObject(obj->getHash(), 0); + if (result != nullptr) + copy.emplace_back(result); + } + return copy; +} + +inline void +fetchMissing(Database& db, Batch const& batch) +{ + for (auto i = 0uz; i < batch.size(); ++i) + { + SCOPED_TRACE("fetchMissing(Database) index=" + std::to_string(i)); + EXPECT_EQ(db.fetchNodeObject(batch[i]->getHash(), 0), nullptr); + } +} + +} // namespace xrpl::node_store + +namespace xrpl { + +[[nodiscard]] inline bool +operator==(node_store::Batch const& lhs, node_store::Batch const& rhs) +{ + return std::ranges::equal(lhs, rhs, node_store::isSame); +} + +} // namespace xrpl diff --git a/src/tests/libxrpl/nodestore/Varint.cpp b/src/tests/libxrpl/nodestore/Varint.cpp new file mode 100644 index 0000000000..3652fd5631 --- /dev/null +++ b/src/tests/libxrpl/nodestore/Varint.cpp @@ -0,0 +1,46 @@ +#include + +#include + +#include +#include +#include +#include + +using namespace xrpl::node_store; + +TEST(Varint, encode_decode) +{ + static constexpr auto kValues = std::to_array({ + 0, + 1, + 2, + 126, + 127, + 128, + 253, + 254, + 255, + 16127, + 16128, + 16129, + 0xff, + 0xffff, + 0xffffffff, + 0xffffffffffffUL, + std::numeric_limits::max(), + }); + + for (auto const value : kValues) + { + std::array::kMax> buffer{}; + auto const bytesWritten = writeVarint(buffer.data(), value); + EXPECT_GT(bytesWritten, 0u); + EXPECT_EQ(bytesWritten, sizeVarint(value)); + + std::size_t decoded = 0; + auto const bytesRead = readVarint(buffer.data(), bytesWritten, decoded); + EXPECT_EQ(bytesRead, bytesWritten); + EXPECT_EQ(value, decoded); + } +} diff --git a/src/tests/libxrpl/peerfinder/Livecache.cpp b/src/tests/libxrpl/peerfinder/Livecache.cpp index 464ec0e5da..298b09e04e 100644 --- a/src/tests/libxrpl/peerfinder/Livecache.cpp +++ b/src/tests/libxrpl/peerfinder/Livecache.cpp @@ -29,7 +29,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { namespace { class LivecacheTest : public ::testing::Test @@ -41,22 +41,22 @@ protected: return beast::Journal{TestSink::instance()}; } - static beast::IP::Endpoint + static beast::ip::Endpoint endpoint(std::uint16_t index, bool v4 = true) { auto const port = static_cast(10000 + index); if (v4) { - auto bytes = beast::IP::AddressV4::bytes_type{ + auto bytes = beast::ip::AddressV4::bytes_type{ {54, static_cast((index / 256) % 256), static_cast(index % 256), 1}}; - return beast::IP::Endpoint{beast::IP::Address{beast::IP::AddressV4{bytes}}, port}; + return beast::ip::Endpoint{beast::ip::Address{beast::ip::AddressV4{bytes}}, port}; } - auto bytes = beast::IP::AddressV6::bytes_type{ + auto bytes = beast::ip::AddressV6::bytes_type{ {0x20, 0x01, 0x0d, @@ -73,11 +73,11 @@ protected: static_cast((index / 256) % 256), static_cast(index % 256), 1}}; - return beast::IP::Endpoint{beast::IP::Address{beast::IP::AddressV6{bytes}}, port}; + return beast::ip::Endpoint{beast::ip::Address{beast::ip::AddressV6{bytes}}, port}; } void - addEndpoint(beast::IP::Endpoint const& ep, std::uint32_t hops = 0) + addEndpoint(beast::ip::Endpoint const& ep, std::uint32_t hops = 0) { cache_.insert(Endpoint{ep, hops}); } @@ -161,7 +161,7 @@ TEST_F(LivecacheTest, hop_iterators_support_const_reverse_and_move_back) TEST_F(LivecacheTest, on_write_reports_entries_and_expiration) { cache_.insert(Endpoint{endpoint(1), 1}); - cache_.insert(Endpoint{endpoint(2), Tuning::kMaxHops + 1}); + cache_.insert(Endpoint{endpoint(2), tuning::kMaxHops + 1}); JsonPropertyStream stream; { @@ -190,7 +190,7 @@ TEST_F(LivecacheTest, expire_removes_entries_after_ttl) cache_.expire(); EXPECT_EQ(cache_.size(), 1u); - clock_.advance(Tuning::kLiveCacheSecondsToLive - 1s); + clock_.advance(tuning::kLiveCacheSecondsToLive - 1s); cache_.expire(); EXPECT_EQ(cache_.size(), 1u); @@ -206,7 +206,7 @@ TEST_F(LivecacheTest, expire_removes_multiple_entries_after_ttl) cache_.insert(Endpoint{endpoint(1), 1}); cache_.insert(Endpoint{endpoint(2), 2}); - clock_.advance(Tuning::kLiveCacheSecondsToLive); + clock_.advance(tuning::kLiveCacheSecondsToLive); cache_.expire(); EXPECT_TRUE(cache_.empty()); } @@ -240,11 +240,11 @@ TEST_F(LivecacheTest, shuffle_preserves_bucket_contents) { for (auto i = 0; i < 100; ++i) { - addEndpoint(endpoint(static_cast(i)), xrpl::randInt(Tuning::kMaxHops + 1)); + addEndpoint(endpoint(static_cast(i)), xrpl::randInt(tuning::kMaxHops + 1)); } using AtHop = std::vector; - using AllHops = std::array; + using AllHops = std::array; auto const compareEndpoint = [](Endpoint const& lhs, Endpoint const& rhs) { return rhs.hops < lhs.hops || (rhs.hops == lhs.hops && rhs.address < lhs.address); @@ -291,4 +291,4 @@ TEST_F(LivecacheTest, shuffle_preserves_bucket_contents) EXPECT_FALSE(allBucketsKeptOriginalOrder); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/tests/libxrpl/peerfinder/PeerFinder.cpp b/src/tests/libxrpl/peerfinder/PeerFinder.cpp index 31fa59d1ce..3a52bbb5aa 100644 --- a/src/tests/libxrpl/peerfinder/PeerFinder.cpp +++ b/src/tests/libxrpl/peerfinder/PeerFinder.cpp @@ -38,7 +38,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { namespace { using ::testing::_; @@ -51,10 +51,10 @@ journal() return beast::Journal{TestSink::instance()}; } -beast::IP::Endpoint +beast::ip::Endpoint endpoint(std::string const& value) { - return beast::IP::Endpoint::fromString(value); + return beast::ip::Endpoint::fromString(value); } class MockStore : public Store @@ -86,7 +86,7 @@ public: }; Store::Entry -storeEntry(beast::IP::Endpoint const& endpoint, int valence) +storeEntry(beast::ip::Endpoint const& endpoint, int valence) { Store::Entry entry; entry.endpoint = endpoint; @@ -106,15 +106,15 @@ class MockChecker public: MOCK_METHOD(void, stop, ()); MOCK_METHOD(void, wait, ()); - MOCK_METHOD(void, recordAsyncConnect, (beast::IP::Endpoint const& ep)); + MOCK_METHOD(void, recordAsyncConnect, (beast::ip::Endpoint const& ep)); boost::system::error_code nextError; bool completeAsync = true; - std::vector asyncConnects; + std::vector asyncConnects; template void - asyncConnect(beast::IP::Endpoint const& ep, Handler&& handler) + asyncConnect(beast::ip::Endpoint const& ep, Handler&& handler) { asyncConnects.push_back(ep); recordAsyncConnect(ep); @@ -204,7 +204,7 @@ protected: }; int -savedValence(std::vector const& entries, beast::IP::Endpoint const& endpoint) +savedValence(std::vector const& entries, beast::ip::Endpoint const& endpoint) { for (auto const& entry : entries) { @@ -458,7 +458,7 @@ TEST(PeerFinderCounts, tracks_slot_states_and_capacity) EXPECT_EQ(counts.outboundSlotsFree(), 1); EXPECT_EQ(counts.totalActive(), 0); EXPECT_FALSE(counts.isConnectedToNetwork()); - EXPECT_EQ(counts.attemptsNeeded(), Tuning::kMaxConnectAttempts); + EXPECT_EQ(counts.attemptsNeeded(), tuning::kMaxConnectAttempts); EXPECT_EQ(counts.stateString(), "0/1 out, 0/1 in, 0 connecting, 0 closing"); SlotImp inbound(endpoint("65.0.0.1:10001"), endpoint("65.0.0.2:10002"), false, clock); @@ -483,7 +483,7 @@ TEST(PeerFinderCounts, tracks_slot_states_and_capacity) counts.add(outbound); EXPECT_EQ(counts.attempts(), 1); EXPECT_EQ(counts.connectCount(), 1); - EXPECT_EQ(counts.attemptsNeeded(), Tuning::kMaxConnectAttempts - 1); + EXPECT_EQ(counts.attemptsNeeded(), tuning::kMaxConnectAttempts - 1); counts.remove(outbound); outbound.state(Slot::State::Connected); @@ -535,7 +535,7 @@ TEST(PeerFinderCounts, tracks_slot_states_and_capacity) Counts saturatedAttempts; saturatedAttempts.onConfig(config); std::vector> attempts; - for (int i = 0; i < Tuning::kMaxConnectAttempts; ++i) + for (int i = 0; i < tuning::kMaxConnectAttempts; ++i) { attempts.push_back( std::make_unique( @@ -544,7 +544,7 @@ TEST(PeerFinderCounts, tracks_slot_states_and_capacity) clock)); saturatedAttempts.add(*attempts.back()); } - EXPECT_EQ(saturatedAttempts.attempts(), Tuning::kMaxConnectAttempts); + EXPECT_EQ(saturatedAttempts.attempts(), tuning::kMaxConnectAttempts); EXPECT_EQ(saturatedAttempts.attemptsNeeded(), 0u); Config disconnected; @@ -563,7 +563,7 @@ TEST(PeerFinderHandouts, filters_redirect_slot_and_connect_targets) EXPECT_EQ(redirects.slot(), slot); EXPECT_TRUE(redirects.list().empty()); EXPECT_FALSE(redirects.full()); - EXPECT_FALSE(redirects.tryInsert(Endpoint{endpoint("65.0.0.3:10003"), Tuning::kMaxHops + 1})); + EXPECT_FALSE(redirects.tryInsert(Endpoint{endpoint("65.0.0.3:10003"), tuning::kMaxHops + 1})); EXPECT_FALSE(redirects.tryInsert(Endpoint{endpoint("65.0.0.3:10003"), 0})); EXPECT_FALSE(redirects.tryInsert(Endpoint{remote.atPort(12000), 1})); EXPECT_TRUE(redirects.tryInsert(Endpoint{endpoint("65.0.0.3:10003"), 1})); @@ -574,7 +574,7 @@ TEST(PeerFinderHandouts, filters_redirect_slot_and_connect_targets) EXPECT_EQ(slotHandouts.slot(), slot); EXPECT_FALSE(slotHandouts.full()); EXPECT_FALSE( - slotHandouts.tryInsert(Endpoint{endpoint("65.0.0.4:10004"), Tuning::kMaxHops + 1})); + slotHandouts.tryInsert(Endpoint{endpoint("65.0.0.4:10004"), tuning::kMaxHops + 1})); EXPECT_FALSE(slotHandouts.tryInsert(Endpoint{remote.atPort(12001), 1})); auto const recent = endpoint("65.0.0.5:10005"); @@ -620,7 +620,7 @@ TEST(PeerFinderHandouts, distributes_livecache_entries) EXPECT_FALSE(targets.front().list().empty()); EXPECT_FALSE(targets.back().list().empty()); - for (std::uint32_t i = 0; i < Tuning::kNumberOfEndpoints; ++i) + for (std::uint32_t i = 0; i < tuning::kNumberOfEndpoints; ++i) targets.front().insert(Endpoint{endpoint("65.1.0." + std::to_string(i + 1) + ":12000"), 1}); handout(targets.begin(), targets.begin() + 1, cache.hops.begin(), cache.hops.end()); @@ -633,7 +633,7 @@ TEST_F(PeerFinderTest, preprocess_filters_invalid_duplicate_and_extra_self_endpo auto const remote = endpoint("65.0.0.2:10002"); auto const slot = std::make_shared(local, remote, false, clock_); Endpoints endpoints{ - Endpoint{endpoint("65.0.0.3:10003"), Tuning::kMaxHops + 1}, + Endpoint{endpoint("65.0.0.3:10003"), tuning::kMaxHops + 1}, Endpoint{endpoint("0.0.0.0:2459"), 0}, Endpoint{endpoint("0.0.0.0:2460"), 0}, Endpoint{endpoint("10.0.0.1:10004"), 1}, @@ -677,7 +677,7 @@ TEST_F(PeerFinderTest, on_endpoints_checks_neighbor_before_caching_it) EXPECT_TRUE(slot->canAccept); EXPECT_TRUE(logic_.livecache.empty()); - clock_.advance(Tuning::kSecondsPerMessage); + clock_.advance(tuning::kSecondsPerMessage); logic_.onEndpoints(slot, advertised); EXPECT_EQ(logic_.livecache.size(), 1u); EXPECT_EQ(logic_.bootcache.size(), 1u); @@ -711,7 +711,7 @@ TEST_F(PeerFinderTest, on_endpoints_skips_failed_neighbor_connectivity_checks) EXPECT_TRUE(slot->checked); EXPECT_FALSE(slot->canAccept); - clock_.advance(Tuning::kSecondsPerMessage); + clock_.advance(tuning::kSecondsPerMessage); logic_.onEndpoints(slot, advertised); EXPECT_TRUE(logic_.livecache.empty()); @@ -740,7 +740,7 @@ TEST_F(PeerFinderTest, on_endpoints_waits_for_pending_connectivity_check) logic_.onEndpoints(slot, advertised); EXPECT_TRUE(slot->connectivityCheckInProgress); - clock_.advance(Tuning::kSecondsPerMessage); + clock_.advance(tuning::kSecondsPerMessage); logic_.onEndpoints(slot, advertised); EXPECT_EQ(checker_.asyncConnects.size(), 1u); EXPECT_TRUE(logic_.livecache.empty()); @@ -950,7 +950,7 @@ TEST(PeerFinderBootcache, periodic_activity_saves_after_cooldown) cache.periodicActivity(); EXPECT_TRUE(store.saves.empty()); - clock.advance(Tuning::kBootcacheCooldownTime + 1s); + clock.advance(tuning::kBootcacheCooldownTime + 1s); cache.periodicActivity(); ASSERT_EQ(store.saves.size(), 1u); @@ -967,23 +967,23 @@ TEST(PeerFinderBootcache, prunes_when_cache_exceeds_limit) TestStopwatch clock; Bootcache cache(store, clock, journal()); - for (std::uint16_t i = 0; i <= Tuning::kBootcacheSize; ++i) + for (std::uint16_t i = 0; i <= tuning::kBootcacheSize; ++i) { EXPECT_TRUE(cache.insert(endpoint( "65.0." + std::to_string((i / 256) % 256) + "." + std::to_string(i % 256) + ":" + std::to_string(10000 + i)))); } - EXPECT_LE(cache.size(), Tuning::kBootcacheSize); + EXPECT_LE(cache.size(), tuning::kBootcacheSize); } TEST(PeerFinderEndpoint, clamps_hops_to_overflow_bucket) { auto const address = endpoint("65.0.0.1:10001"); - Endpoint const ep(address, Tuning::kMaxHops + 10); + Endpoint const ep(address, tuning::kMaxHops + 10); EXPECT_EQ(ep.address, address); - EXPECT_EQ(ep.hops, Tuning::kMaxHops + 1); + EXPECT_EQ(ep.hops, tuning::kMaxHops + 1); } TEST(PeerFinderSlotImp, tracks_state_and_recent_endpoints) @@ -1001,7 +1001,7 @@ TEST(PeerFinderSlotImp, tracks_state_and_recent_endpoints) EXPECT_FALSE(inbound.reserved()); EXPECT_EQ(inbound.state(), State::Accept); EXPECT_EQ(inbound.remoteEndpoint(), remote); - EXPECT_EQ(inbound.localEndpoint(), std::optional{local}); + EXPECT_EQ(inbound.localEndpoint(), std::optional{local}); EXPECT_FALSE(inbound.publicKey()); EXPECT_FALSE(inbound.listeningPort()); EXPECT_FALSE(inbound.checked); @@ -1018,7 +1018,7 @@ TEST(PeerFinderSlotImp, tracks_state_and_recent_endpoints) inbound.reserved(true); inbound.setListeningPort(2459); - EXPECT_EQ(inbound.localEndpoint(), std::optional{newLocal}); + EXPECT_EQ(inbound.localEndpoint(), std::optional{newLocal}); EXPECT_EQ(inbound.remoteEndpoint(), newRemote); EXPECT_EQ(inbound.publicKey(), std::optional{publicKey}); EXPECT_TRUE(inbound.reserved()); @@ -1055,7 +1055,7 @@ TEST(PeerFinderSlotImp, tracks_state_and_recent_endpoints) EXPECT_TRUE(outbound.recent.filter(recent, 1)); EXPECT_FALSE(outbound.recent.filter(recent, 0)); - clock.advance(Tuning::kLiveCacheSecondsToLive + 1s); + clock.advance(tuning::kLiveCacheSecondsToLive + 1s); outbound.expire(); EXPECT_FALSE(outbound.recent.filter(recent, 1)); } @@ -1112,7 +1112,7 @@ TEST(PeerFinderConfig, calculates_outbound_peers_and_clamps_ip_limits) { Config config; config.maxPeers = 1; - EXPECT_EQ(config.calcOutPeers(), Tuning::kMinOutCount); + EXPECT_EQ(config.calcOutPeers(), tuning::kMinOutCount); config.maxPeers = 100; EXPECT_EQ(config.calcOutPeers(), 15u); @@ -1267,4 +1267,4 @@ TEST(PeerFinderConfig, rejects_incomplete_or_out_of_range_peer_limits) } } // namespace -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/tests/libxrpl/protocol/ApiVersion.cpp b/src/tests/libxrpl/protocol/ApiVersion.cpp new file mode 100644 index 0000000000..5bb6a158cf --- /dev/null +++ b/src/tests/libxrpl/protocol/ApiVersion.cpp @@ -0,0 +1,26 @@ +#include + +#include + +using namespace xrpl; + +TEST(ApiVersion, invariants) +{ + static_assert(rpc::kApiMinimumSupportedVersion <= rpc::kApiMaximumSupportedVersion); + static_assert(rpc::kApiMinimumSupportedVersion <= rpc::kApiMaximumValidVersion); + static_assert(rpc::kApiMaximumSupportedVersion <= rpc::kApiMaximumValidVersion); + static_assert(rpc::kApiBetaVersion <= rpc::kApiMaximumValidVersion); +} + +// Update when we change versions +TEST(ApiVersion, versions) +{ + static_assert(rpc::kApiMinimumSupportedVersion >= 1); + static_assert(rpc::kApiMinimumSupportedVersion < 2); + static_assert(rpc::kApiMaximumSupportedVersion >= 2); + static_assert(rpc::kApiMaximumSupportedVersion < 3); + static_assert(rpc::kApiMaximumValidVersion >= 3); + static_assert(rpc::kApiMaximumValidVersion < 4); + static_assert(rpc::kApiBetaVersion >= 3); + static_assert(rpc::kApiBetaVersion < 4); +} diff --git a/src/tests/libxrpl/protocol/Serializer.cpp b/src/tests/libxrpl/protocol/Serializer.cpp new file mode 100644 index 0000000000..fc5742444a --- /dev/null +++ b/src/tests/libxrpl/protocol/Serializer.cpp @@ -0,0 +1,49 @@ +#include + +#include + +#include +#include +#include + +using namespace xrpl; + +TEST(Serializer, add32_roundtrip) +{ + static constexpr auto kValues = std::to_array({ + std::numeric_limits::min(), + -1, + 0, + 1, + std::numeric_limits::max(), + }); + + for (std::int32_t const value : kValues) + { + Serializer s; + s.add32(value); + EXPECT_EQ(s.size(), 4); + SerialIter sit(s.slice()); + EXPECT_EQ(sit.geti32(), value); + } +} + +TEST(Serializer, add64_roundtrip) +{ + static constexpr auto kValues = std::to_array({ + std::numeric_limits::min(), + -1, + 0, + 1, + std::numeric_limits::max(), + }); + + for (std::int64_t const value : kValues) + { + Serializer s; + s.add64(value); + EXPECT_EQ(s.size(), 8); + SerialIter sit(s.slice()); + EXPECT_EQ(sit.geti64(), value); + } +} diff --git a/src/tests/libxrpl/protocol_autogen/ledger_entries/VaultTests.cpp b/src/tests/libxrpl/protocol_autogen/ledger_entries/VaultTests.cpp index 2697924d37..f55d01f606 100644 --- a/src/tests/libxrpl/protocol_autogen/ledger_entries/VaultTests.cpp +++ b/src/tests/libxrpl/protocol_autogen/ledger_entries/VaultTests.cpp @@ -35,6 +35,7 @@ TEST(VaultTests, BuilderSettersRoundTrip) auto const shareMPTIDValue = canonical_UINT192(); auto const withdrawalPolicyValue = canonical_UINT8(); auto const scaleValue = canonical_UINT8(); + auto const lEVersionValue = canonical_UINT8(); VaultBuilder builder{ previousTxnIDValue, @@ -54,6 +55,7 @@ TEST(VaultTests, BuilderSettersRoundTrip) builder.setAssetsMaximum(assetsMaximumValue); builder.setLossUnrealized(lossUnrealizedValue); builder.setScale(scaleValue); + builder.setLEVersion(lEVersionValue); builder.setLedgerIndex(index); builder.setFlags(0x1u); @@ -166,6 +168,14 @@ TEST(VaultTests, BuilderSettersRoundTrip) EXPECT_TRUE(entry.hasScale()); } + { + auto const& expected = lEVersionValue; + auto const actualOpt = entry.getLEVersion(); + ASSERT_TRUE(actualOpt.has_value()); + expectEqualField(expected, *actualOpt, "sfLEVersion"); + EXPECT_TRUE(entry.hasLEVersion()); + } + EXPECT_TRUE(entry.hasLedgerIndex()); auto const ledgerIndex = entry.getLedgerIndex(); ASSERT_TRUE(ledgerIndex.has_value()); @@ -194,6 +204,7 @@ TEST(VaultTests, BuilderFromSleRoundTrip) auto const shareMPTIDValue = canonical_UINT192(); auto const withdrawalPolicyValue = canonical_UINT8(); auto const scaleValue = canonical_UINT8(); + auto const lEVersionValue = canonical_UINT8(); auto sle = std::make_shared(Vault::entryType, index); @@ -212,6 +223,7 @@ TEST(VaultTests, BuilderFromSleRoundTrip) sle->at(sfShareMPTID) = shareMPTIDValue; sle->at(sfWithdrawalPolicy) = withdrawalPolicyValue; sle->at(sfScale) = scaleValue; + sle->at(sfLEVersion) = lEVersionValue; VaultBuilder builderFromSle{sle}; EXPECT_TRUE(builderFromSle.validate()); @@ -390,6 +402,19 @@ TEST(VaultTests, BuilderFromSleRoundTrip) expectEqualField(expected, *fromBuilderOpt, "sfScale"); } + { + auto const& expected = lEVersionValue; + + auto const fromSleOpt = entryFromSle.getLEVersion(); + auto const fromBuilderOpt = entryFromBuilder.getLEVersion(); + + ASSERT_TRUE(fromSleOpt.has_value()); + ASSERT_TRUE(fromBuilderOpt.has_value()); + + expectEqualField(expected, *fromSleOpt, "sfLEVersion"); + expectEqualField(expected, *fromBuilderOpt, "sfLEVersion"); + } + EXPECT_EQ(entryFromSle.getKey(), index); EXPECT_EQ(entryFromBuilder.getKey(), index); } @@ -472,5 +497,7 @@ TEST(VaultTests, OptionalFieldsReturnNullopt) EXPECT_FALSE(entry.getLossUnrealized().has_value()); EXPECT_FALSE(entry.hasScale()); EXPECT_FALSE(entry.getScale().has_value()); + EXPECT_FALSE(entry.hasLEVersion()); + EXPECT_FALSE(entry.getLEVersion().has_value()); } } diff --git a/src/tests/libxrpl/resource/Logic.cpp b/src/tests/libxrpl/resource/Logic.cpp index 1f935ebf4b..b38ca2e051 100644 --- a/src/tests/libxrpl/resource/Logic.cpp +++ b/src/tests/libxrpl/resource/Logic.cpp @@ -17,11 +17,14 @@ #include #include +#include #include #include +#include #include +#include -namespace xrpl::Resource { +namespace xrpl::resource { class ResourceManagerTest : public ::testing::Test { @@ -54,9 +57,10 @@ protected: //-------------------------------------------------------------------------- - static void - populateGossip(Gossip& gossip) + static Gossip + makeGossip() { + Gossip gossip; std::uint8_t const v(10 + randInt(9)); std::uint8_t const n(10 + randInt(9)); gossip.items.reserve(n); @@ -64,15 +68,16 @@ protected: { Gossip::Item item; item.balance = 100 + randInt(499); - beast::IP::AddressV4::bytes_type const d = {{ + beast::ip::AddressV4::bytes_type const d = {{ 192, 0, 2, static_cast(v + i), }}; - item.address = beast::IP::Endpoint{beast::IP::AddressV4{d}}; - gossip.items.push_back(item); + item.address = beast::ip::Endpoint{beast::ip::AddressV4{d}}; + gossip.items.push_back(std::move(item)); } + return gossip; } }; @@ -81,14 +86,14 @@ TEST_F(ResourceManagerTest, limited_warn_drop) TestLogic logic{j_}; Charge const fee{kDropThreshold + 1}; - beast::IP::Endpoint const addr{beast::IP::Endpoint::fromString("192.0.2.2")}; + beast::ip::Endpoint const addr{beast::ip::Endpoint::fromString("192.0.2.2")}; { Consumer c{logic.newInboundEndpoint(addr)}; // Create load until we get a warning - int n = 10000; - bool warned = false; + auto n = 10000; + auto warned = false; while (--n >= 0) { @@ -97,7 +102,7 @@ TEST_F(ResourceManagerTest, limited_warn_drop) warned = true; break; } - ++logic.clock(); + logic.advance(); } ASSERT_TRUE(warned) << "Loop count exceeded without warning"; @@ -113,7 +118,7 @@ TEST_F(ResourceManagerTest, limited_warn_drop) EXPECT_TRUE(c.disconnect(j_)); break; } - ++logic.clock(); + logic.advance(); } ASSERT_TRUE(dropped) << "Loop count exceeded without dropping"; @@ -135,7 +140,7 @@ TEST_F(ResourceManagerTest, limited_warn_drop) auto n = kSecondsUntilExpiration + 1s; while (--n > 0s) { - ++logic.clock(); + logic.advance(); logic.periodicActivity(); Consumer const c{logic.newInboundEndpoint(addr)}; if (c.disposition() != Disposition::Drop) @@ -153,7 +158,7 @@ TEST_F(ResourceManagerTest, unlimited_warn_drop) TestLogic logic{j_}; Charge const fee{kDropThreshold + 1}; - beast::IP::Endpoint const addr{beast::IP::Endpoint::fromString("192.0.2.2")}; + beast::ip::Endpoint const addr{beast::ip::Endpoint::fromString("192.0.2.2")}; Consumer c{logic.newUnlimitedEndpoint(addr)}; // Create load until we get a warning @@ -167,7 +172,7 @@ TEST_F(ResourceManagerTest, unlimited_warn_drop) warned = true; break; } - ++logic.clock(); + logic.advance(); } EXPECT_FALSE(warned) << "Should loop forever with no warning"; @@ -175,15 +180,17 @@ TEST_F(ResourceManagerTest, unlimited_warn_drop) TEST_F(ResourceManagerTest, charges) { + static constexpr auto kDecayTicks = 128uz; + TestLogic logic{j_}; { - beast::IP::Endpoint const address{beast::IP::Endpoint::fromString("192.0.2.1")}; + beast::ip::Endpoint const address{beast::ip::Endpoint::fromString("192.0.2.1")}; Consumer c{logic.newInboundEndpoint(address)}; Charge const fee{1000}; JLOG(j_.info()) << "Charging " << c.toString() << " " << fee << " per second"; c.charge(fee); - for (int i = 0; i < 128; ++i) + for (auto tick = 0uz; tick < kDecayTicks; ++tick) { JLOG(j_.info()) << "Time= " << logic.clock().now().time_since_epoch().count() << ", Balance = " << c.balance(); @@ -192,11 +199,11 @@ TEST_F(ResourceManagerTest, charges) } { - beast::IP::Endpoint const address{beast::IP::Endpoint::fromString("192.0.2.2")}; + beast::ip::Endpoint const address{beast::ip::Endpoint::fromString("192.0.2.2")}; Consumer c{logic.newInboundEndpoint(address)}; Charge const fee{1000}; JLOG(j_.info()) << "Charging " << c.toString() << " " << fee << " per second"; - for (int i = 0; i < 128; ++i) + for (auto tick = 0uz; tick < kDecayTicks; ++tick) { c.charge(fee); JLOG(j_.info()) << "Time= " << logic.clock().now().time_since_epoch().count() @@ -210,13 +217,10 @@ TEST_F(ResourceManagerTest, imports) { TestLogic logic{j_}; - Gossip g[5]; - - for (auto& i : g) - populateGossip(i); - - for (int i = 0; i < 5; ++i) - logic.importConsumers(std::to_string(i), g[i]); + static constexpr auto kGossipSources = 5uz; + std::ranges::for_each(std::views::iota(0uz, kGossipSources), [&](auto const i) { + logic.importConsumers(std::to_string(i), makeGossip()); + }); } TEST_F(ResourceManagerTest, import) @@ -226,16 +230,16 @@ TEST_F(ResourceManagerTest, import) Gossip g; Gossip::Item item; item.balance = 100; - beast::IP::AddressV4::bytes_type const d = {{ + beast::ip::AddressV4::bytes_type const d = {{ 192, 0, 2, 1, }}; - item.address = beast::IP::Endpoint{beast::IP::AddressV4{d}}; - g.items.push_back(item); + item.address = beast::ip::Endpoint{beast::ip::AddressV4{d}}; + g.items.push_back(std::move(item)); logic.importConsumers("g", g); } -} // namespace xrpl::Resource +} // namespace xrpl::resource diff --git a/src/tests/libxrpl/shamap/SHAMap.cpp b/src/tests/libxrpl/shamap/SHAMap.cpp index 238c34bf9c..e662e16be4 100644 --- a/src/tests/libxrpl/shamap/SHAMap.cpp +++ b/src/tests/libxrpl/shamap/SHAMap.cpp @@ -257,12 +257,9 @@ TEST_P(SHAMapTest, add_traverse_snapshot_build_tear_and_iterate) map.invariants(); } - int h = 7; + auto keyIndex = kKeys.size(); for (auto const& k : map) - { - EXPECT_EQ(k.key(), kKeys[h]); - --h; - } + EXPECT_EQ(k.key(), kKeys[--keyIndex]); } } @@ -288,7 +285,11 @@ TEST_F(SHAMapPathProof, verify_proof_path) uint256 rootHash; std::vector goodPath; - for (unsigned char c = 1; c < 100; ++c) + static constexpr unsigned char kFirstKey = 1; + static constexpr unsigned char kKeyCount = 100; + static constexpr unsigned char kLastKey = kKeyCount - 1; + + for (unsigned char c = kFirstKey; c < kKeyCount; ++c) { uint256 k(c); map.addItem(SHAMapNodeType::TnAccountState, makeShamapitem(k, Slice{k.data(), k.size()})); @@ -304,7 +305,7 @@ TEST_F(SHAMapPathProof, verify_proof_path) auto& proofPath = *path; EXPECT_TRUE(map.verifyProofPath(root, k, proofPath)); - if (c == 1) + if (c == kFirstKey) { // extra node proofPath.insert(proofPath.begin(), proofPath.front()); @@ -313,7 +314,7 @@ TEST_F(SHAMapPathProof, verify_proof_path) uint256 const wrongKey(c + 1); EXPECT_FALSE(map.getProofPath(wrongKey)); } - if (c == 99) + if (c == kLastKey) { key = k; rootHash = root; diff --git a/src/tests/libxrpl/shamap/SHAMapSync.cpp b/src/tests/libxrpl/shamap/SHAMapSync.cpp index 5cefbae8a1..e4bcbd8970 100644 --- a/src/tests/libxrpl/shamap/SHAMapSync.cpp +++ b/src/tests/libxrpl/shamap/SHAMapSync.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -18,6 +17,7 @@ #include #include +#include #include #include #include @@ -34,15 +34,17 @@ protected: boost::intrusive_ptr makeRandomAS() { + static constexpr auto kWordsPerState = 3uz; + Serializer s; - for (int d = 0; d < 3; ++d) + for (auto word = 0uz; word < kWordsPerState; ++word) s.add32(randInt(eng_)); return makeShamapitem(s.getSHA512Half(), s.slice()); } bool - confuseMap(SHAMap& map, int count) + confuseMap(SHAMap& map, std::size_t count) { // add a bunch of random states to a map, then remove them // map should be the same @@ -50,7 +52,7 @@ protected: std::list items; - for (int i = 0; i < count; ++i) + for (auto i = 0uz; i < count; ++i) { auto item = makeRandomAS(); items.push_back(item->key()); @@ -87,39 +89,45 @@ TEST_F(SHAMapSyncTest, sync) SHAMap source{SHAMapType::FREE, f}; SHAMap destination{SHAMapType::FREE, f2}; - int const items = 10000; - for (int i = 0; i < items; ++i) + static constexpr auto kItemCount = 10000uz; + static constexpr auto kInvariantInterval = 100uz; + static constexpr auto kNodesToConfuse = 500uz; + static constexpr auto kMaxNodesPerRequest = 2048; + + for (auto i = 0uz; i < kItemCount; ++i) { source.addItem(SHAMapNodeType::TnAccountState, makeRandomAS()); - if (i % 100 == 0) + if (i % kInvariantInterval == 0) source.invariants(); } source.invariants(); - ASSERT_TRUE(confuseMap(source, 500)); + ASSERT_TRUE(confuseMap(source, kNodesToConfuse)); source.invariants(); source.setImmutable(); - int count = 0; + std::size_t count = 0; source.visitLeaves([&count]([[maybe_unused]] auto const& item) { ++count; }); - EXPECT_EQ(count, items); + EXPECT_EQ(count, kItemCount); std::vector missingNodes; - source.walkMap(missingNodes, 2048); + source.walkMap(missingNodes, kMaxNodesPerRequest); EXPECT_TRUE(missingNodes.empty()); destination.setSynching(); { - std::vector> a; + std::vector a; ASSERT_TRUE(source.getNodeFat(SHAMapNodeID(), a, randBool(eng_), randInt(eng_, 2))); ASSERT_FALSE(a.empty()) << "NodeSize"; - ASSERT_TRUE( - destination.addRootNode(source.getHash(), makeSlice(a[0].second), nullptr).isGood()); + auto node = SHAMapTreeNode::makeFromWire(makeSlice(a[0].data)); + if (!node) + FAIL() << "Could not create node"; + ASSERT_TRUE(destination.addRootNode(source.getHash(), std::move(node), nullptr).isGood()); } do @@ -127,13 +135,13 @@ TEST_F(SHAMapSyncTest, sync) f.clock().advance(std::chrono::seconds(1)); // get the list of nodes we know we need - auto nodesMissing = destination.getMissingNodes(2048, nullptr); + auto nodesMissing = destination.getMissingNodes(kMaxNodesPerRequest, nullptr); if (nodesMissing.empty()) break; // get as many nodes as possible based on this information - std::vector> b; + std::vector b; for (auto& it : nodesMissing) { @@ -155,7 +163,12 @@ TEST_F(SHAMapSyncTest, sync) // Keep failures fatal here because this loop is data-dependent. // non-deterministic number of times and the number of tests run // should be deterministic - if (!destination.addKnownNode(i.first, makeSlice(i.second), nullptr).isUseful()) + auto node = SHAMapTreeNode::makeFromWire(makeSlice(i.data)); + if (!node) + FAIL() << "Could not create node"; + if (i.isLeaf != node->isLeaf()) + FAIL() << "Node is not a leaf"; + if (!destination.addKnownNode(i.nodeID, std::move(node), nullptr).isUseful()) FAIL() << "Known node was not useful"; } } while (true); diff --git a/src/tests/libxrpl/shamap/common.h b/src/tests/libxrpl/shamap/common.h index 5b44f2b251..91401d2973 100644 --- a/src/tests/libxrpl/shamap/common.h +++ b/src/tests/libxrpl/shamap/common.h @@ -24,13 +24,13 @@ namespace xrpl::tests { class TestNodeFamily : public Family { private: - std::unique_ptr db_; + std::unique_ptr db_; std::shared_ptr fbCache_; std::shared_ptr tnCache_; TestStopwatch clock_; - NodeStore::DummyScheduler scheduler_; + node_store::DummyScheduler scheduler_; beast::Journal const j_; @@ -49,17 +49,17 @@ public: Section testSection; testSection.set(Keys::kType, "memory"); testSection.set(Keys::kPath, "SHAMap_test"); - db_ = NodeStore::Manager::instance().makeDatabase( + db_ = node_store::Manager::instance().makeDatabase( megabytes(4), scheduler_, 1, testSection, j); } - NodeStore::Database& + node_store::Database& db() override { return *db_; } - [[nodiscard]] NodeStore::Database const& + [[nodiscard]] node_store::Database const& db() const override { return *db_; diff --git a/src/xrpld/app/consensus/RCLConsensus.cpp b/src/xrpld/app/consensus/RCLConsensus.cpp index 20421ab916..28b910c8e5 100644 --- a/src/xrpld/app/consensus/RCLConsensus.cpp +++ b/src/xrpld/app/consensus/RCLConsensus.cpp @@ -847,7 +847,7 @@ RCLConsensus::Adaptor::validate(RCLCxLedger const& ledger, RCLTxSet const& txns, // Report our server version every flag ledger: if (ledger.ledger->isVotingLedger()) - v.setFieldU64(sfServerVersion, BuildInfo::getEncodedVersion()); + v.setFieldU64(sfServerVersion, build_info::getEncodedVersion()); // Report our load { diff --git a/src/xrpld/app/ledger/AccountStateSF.h b/src/xrpld/app/ledger/AccountStateSF.h index f5117db4d4..5c1d260c9a 100644 --- a/src/xrpld/app/ledger/AccountStateSF.h +++ b/src/xrpld/app/ledger/AccountStateSF.h @@ -18,7 +18,7 @@ namespace xrpl { class AccountStateSF : public SHAMapSyncFilter { public: - AccountStateSF(NodeStore::Database& db, AbstractFetchPackContainer& fp) : db_(db), fp_(fp) + AccountStateSF(node_store::Database& db, AbstractFetchPackContainer& fp) : db_(db), fp_(fp) { } @@ -34,7 +34,7 @@ public: getNode(SHAMapHash const& nodeHash) const override; private: - NodeStore::Database& db_; + node_store::Database& db_; AbstractFetchPackContainer& fp_; }; diff --git a/src/xrpld/app/ledger/InboundLedger.h b/src/xrpld/app/ledger/InboundLedger.h index d8a9ddf46b..9a7ee510f6 100644 --- a/src/xrpld/app/ledger/InboundLedger.h +++ b/src/xrpld/app/ledger/InboundLedger.h @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -24,7 +23,7 @@ #include #include #include -#include +#include #include #include @@ -136,7 +135,7 @@ private: addPeers(); void - tryDB(NodeStore::Database& srcDB); + tryDB(node_store::Database& srcDB); void done(); @@ -154,16 +153,19 @@ private: processData(std::shared_ptr peer, protocol::TMLedgerData const& data); bool - takeHeader(std::string const& data); + takeHeader(std::string_view data); void - receiveNode(protocol::TMLedgerData const& packet, SHAMapAddNode&); + receiveNode( + std::shared_ptr const& peer, + protocol::TMLedgerData const& packet, + SHAMapAddNode& san); bool - takeTxRootNode(Slice const& data, SHAMapAddNode&); + takeTxRootNode(std::string_view data, SHAMapAddNode& san); bool - takeAsRootNode(Slice const& data, SHAMapAddNode&); + takeAsRootNode(std::string_view data, SHAMapAddNode& san); std::vector neededTxHashes(int max, SHAMapSyncFilter const* filter) const; diff --git a/src/xrpld/app/ledger/LedgerNodeHelpers.h b/src/xrpld/app/ledger/LedgerNodeHelpers.h new file mode 100644 index 0000000000..9df9ab06c7 --- /dev/null +++ b/src/xrpld/app/ledger/LedgerNodeHelpers.h @@ -0,0 +1,52 @@ +#pragma once + +#include +#include + +#include +#include + +namespace protocol { +class TMLedgerNode; +} // namespace protocol + +namespace xrpl { + +/** + * @brief Deserializes a SHAMapTreeNode from wire format data. + * + * This function attempts to create a SHAMapTreeNode from the provided data string. If the data is + * malformed or deserialization fails, the function returns a nullptr instead of throwing an + * exception. + * + * @param data The serialized node data in wire format. + * @return The deserialized tree node if successful, or a nullptr if deserialization fails. + */ +[[nodiscard]] SHAMapTreeNodePtr +getTreeNode(std::string_view data); + +/** + * @brief Extracts or reconstructs the SHAMapNodeID from a ledger node proto message. + * + * This function retrieves the SHAMapNodeID for a tree node, with behavior that depends on which + * field is set and the node type (inner vs. leaf). + * + * When the legacy `nodeid` field is set in the message: + * - For all nodes: Deserializes the node ID from the field. + * - For leaf nodes: Validates that the node ID is consistent with the leaf's key. + * + * When the new `id` or `depth` field is set in the message: + * - For inner nodes: Deserializes the node ID from the `id` field. + * - For leaf nodes: Reconstructs the node ID using both the depth from the `depth` field and the + * key from the leaf node's item. + * Note that root nodes may be inner nodes or leaf nodes. + * + * @param ledgerNode The validated protocol message containing the ledger node data. + * @param treeNode The deserialized tree node (inner or leaf node). + * @return An optional containing the node ID if extraction/reconstruction succeeds, or std::nullopt + * if the required fields are missing or validation fails. + */ +[[nodiscard]] std::optional +getSHAMapNodeID(protocol::TMLedgerNode const& ledgerNode, SHAMapTreeNode const& treeNode); + +} // namespace xrpl diff --git a/src/xrpld/app/ledger/LedgerReplayer.h b/src/xrpld/app/ledger/LedgerReplayer.h index 6feb187df6..b2806ee813 100644 --- a/src/xrpld/app/ledger/LedgerReplayer.h +++ b/src/xrpld/app/ledger/LedgerReplayer.h @@ -24,7 +24,7 @@ namespace test { class LedgerReplayClient; } // namespace test -namespace LedgerReplayParameters { +namespace ledger_replay_parameters { // timeout value for LedgerReplayTask constexpr auto kTaskTimeout = std::chrono::milliseconds{500}; @@ -53,7 +53,7 @@ constexpr std::uint32_t kMaxTaskSize = 256; // to limit the number of LedgerReplay related jobs in JobQueue constexpr std::uint32_t kMaxQueuedTasks = 100; -} // namespace LedgerReplayParameters +} // namespace ledger_replay_parameters /** * Manages the lifetime of ledger replay tasks. diff --git a/src/xrpld/app/ledger/LedgerToJson.h b/src/xrpld/app/ledger/LedgerToJson.h index 1eac4d68f1..e1172e897a 100644 --- a/src/xrpld/app/ledger/LedgerToJson.h +++ b/src/xrpld/app/ledger/LedgerToJson.h @@ -18,7 +18,7 @@ struct LedgerFill { LedgerFill( ReadView const& l, - RPC::Context const* ctx, + rpc::Context const* ctx, int o = 0, std::vector q = {}) : ledger(l), options(o), txQueue(std::move(q)), context(ctx) @@ -40,7 +40,7 @@ struct LedgerFill ReadView const& ledger; int options; std::vector txQueue; - RPC::Context const* context; + rpc::Context const* context; std::optional closeTime; }; diff --git a/src/xrpld/app/ledger/TransactionStateSF.h b/src/xrpld/app/ledger/TransactionStateSF.h index a3f7e7f55a..b8c1b2c835 100644 --- a/src/xrpld/app/ledger/TransactionStateSF.h +++ b/src/xrpld/app/ledger/TransactionStateSF.h @@ -18,7 +18,7 @@ namespace xrpl { class TransactionStateSF : public SHAMapSyncFilter { public: - TransactionStateSF(NodeStore::Database& db, AbstractFetchPackContainer& fp) : db_(db), fp_(fp) + TransactionStateSF(node_store::Database& db, AbstractFetchPackContainer& fp) : db_(db), fp_(fp) { } @@ -34,7 +34,7 @@ public: getNode(SHAMapHash const& nodeHash) const override; private: - NodeStore::Database& db_; + node_store::Database& db_; AbstractFetchPackContainer& fp_; }; diff --git a/src/xrpld/app/ledger/detail/InboundLedger.cpp b/src/xrpld/app/ledger/detail/InboundLedger.cpp index 627a5d574f..246c7d567b 100644 --- a/src/xrpld/app/ledger/detail/InboundLedger.cpp +++ b/src/xrpld/app/ledger/detail/InboundLedger.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -44,8 +45,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -224,7 +225,7 @@ InboundLedger::neededStateHashes(int max, SHAMapSyncFilter const* filter) const // See how much of the ledger data is stored locally // Data found in a fetch pack will be stored void -InboundLedger::tryDB(NodeStore::Database& srcDB) +InboundLedger::tryDB(node_store::Database& srcDB) { if (!haveHeader_) { @@ -779,7 +780,7 @@ InboundLedger::filterNodes( */ // data must not have hash prefix bool -InboundLedger::takeHeader(std::string const& data) +InboundLedger::takeHeader(std::string_view data) { // Return value: true=normal, false=bad data JLOG(journal_.trace()) << "got header acquiring ledger " << hash_; @@ -825,7 +826,10 @@ InboundLedger::takeHeader(std::string const& data) * Call with a lock */ void -InboundLedger::receiveNode(protocol::TMLedgerData const& packet, SHAMapAddNode& san) +InboundLedger::receiveNode( + std::shared_ptr const& peer, + protocol::TMLedgerData const& packet, + SHAMapAddNode& san) { if (!haveHeader_) { @@ -868,32 +872,47 @@ InboundLedger::receiveNode(protocol::TMLedgerData const& packet, SHAMapAddNode& { auto const f = filter.get(); - for (auto const& node : packet.nodes()) + for (auto const& ledgerNode : packet.nodes()) { - auto const nodeID = deserializeSHAMapNodeID(node.nodeid()); + auto treeNode = getTreeNode(ledgerNode.nodedata()); + if (!treeNode) + { + JLOG(journal_.warn()) + << "Got invalid node data for ledger " << hash_ << " from peer " << peer->id(); + peer->charge(resource::kFeeInvalidData, "ledger_node.node_data invalid"); + san.incInvalid(); + return; + } + auto const nodeID = getSHAMapNodeID(ledgerNode, *treeNode); if (!nodeID) - throw std::runtime_error("data does not properly deserialize"); - - if (nodeID->isRoot()) { - san += map.addRootNode(rootHash, makeSlice(node.nodedata()), f); - } - else - { - san += map.addKnownNode(*nodeID, makeSlice(node.nodedata()), f); + JLOG(journal_.warn()) + << "Got invalid node id for ledger " << hash_ << " from peer " << peer->id(); + peer->charge(resource::kFeeInvalidData, "ledger_node.node_id invalid"); + san.incInvalid(); + return; } - if (!san.isGood()) + auto const result = nodeID->isRoot() + ? map.addRootNode(rootHash, std::move(treeNode), f) + : map.addKnownNode(*nodeID, std::move(treeNode), f); + san += result; + + if (result.isInvalid()) { - JLOG(journal_.warn()) << "Received bad node data"; + JLOG(journal_.warn()) << "Got invalid node " << *nodeID << " for ledger " << hash_ + << " from peer " << peer->id(); + peer->charge(resource::kFeeInvalidData, "ledger_node invalid"); return; } } } catch (std::exception const& e) { - JLOG(journal_.error()) << "Received bad node data: " << e.what(); + // If we get here it is not necessarily because the node was bad, so don't charge the peer. + JLOG(journal_.error()) << "Could not process node for ledger " << hash_ << " from peer " + << peer->id() << ": " << e.what(); san.incInvalid(); return; } @@ -922,7 +941,7 @@ InboundLedger::receiveNode(protocol::TMLedgerData const& packet, SHAMapAddNode& * Call with a lock */ bool -InboundLedger::takeAsRootNode(Slice const& data, SHAMapAddNode& san) +InboundLedger::takeAsRootNode(std::string_view data, SHAMapAddNode& san) { if (failed_ || haveState_) { @@ -938,10 +957,19 @@ InboundLedger::takeAsRootNode(Slice const& data, SHAMapAddNode& san) // LCOV_EXCL_STOP } + auto treeNode = getTreeNode(data); + if (!treeNode) + { + JLOG(journal_.warn()) << "Got invalid AS root node data for ledger " << hash_; + san.incInvalid(); + return false; + } + AccountStateSF filter(ledger_->stateMap().family().db(), app_.getLedgerMaster()); - san += - ledger_->stateMap().addRootNode(SHAMapHash{ledger_->header().accountHash}, data, &filter); - return san.isGood(); + auto const result = ledger_->stateMap().addRootNode( + SHAMapHash{ledger_->header().accountHash}, std::move(treeNode), &filter); + san += result; + return !result.isInvalid(); } /** @@ -949,7 +977,7 @@ InboundLedger::takeAsRootNode(Slice const& data, SHAMapAddNode& san) * Call with a lock */ bool -InboundLedger::takeTxRootNode(Slice const& data, SHAMapAddNode& san) +InboundLedger::takeTxRootNode(std::string_view data, SHAMapAddNode& san) { if (failed_ || haveTransactions_) { @@ -965,9 +993,19 @@ InboundLedger::takeTxRootNode(Slice const& data, SHAMapAddNode& san) // LCOV_EXCL_STOP } + auto treeNode = getTreeNode(data); + if (!treeNode) + { + JLOG(journal_.warn()) << "Got invalid TX root node data for ledger " << hash_; + san.incInvalid(); + return false; + } + TransactionStateSF filter(ledger_->txMap().family().db(), app_.getLedgerMaster()); - san += ledger_->txMap().addRootNode(SHAMapHash{ledger_->header().txHash}, data, &filter); - return san.isGood(); + auto const result = ledger_->txMap().addRootNode( + SHAMapHash{ledger_->header().txHash}, std::move(treeNode), &filter); + san += result; + return !result.isInvalid(); } std::vector @@ -1030,7 +1068,7 @@ InboundLedger::gotData( * Returns the number of useful nodes */ // VFALCO NOTE, it is not necessary to pass the entire Peer, -// we can get away with just a Resource::Consumer endpoint. +// we can get away with just a resource::Consumer endpoint. // // TODO Change peer to Consumer // @@ -1042,7 +1080,7 @@ InboundLedger::processData(std::shared_ptr peer, protocol::TMLedgerData co if (packet.nodes().empty()) { JLOG(journal_.warn()) << peer->id() << ": empty header data"; - peer->charge(Resource::kFeeMalformedRequest, "ledger_data empty header"); + peer->charge(resource::kFeeMalformedRequest, "ledger_data empty header"); return -1; } @@ -1057,7 +1095,7 @@ InboundLedger::processData(std::shared_ptr peer, protocol::TMLedgerData co if (!takeHeader(packet.nodes(0).nodedata())) { JLOG(journal_.warn()) << "Got invalid header data"; - peer->charge(Resource::kFeeMalformedRequest, "ledger_data invalid header"); + peer->charge(resource::kFeeMalformedRequest, "ledger_data invalid header"); return -1; } @@ -1065,22 +1103,35 @@ InboundLedger::processData(std::shared_ptr peer, protocol::TMLedgerData co } if (!haveState_ && (packet.nodes().size() > 1) && - !takeAsRootNode(makeSlice(packet.nodes(1).nodedata()), san)) + !takeAsRootNode(packet.nodes(1).nodedata(), san)) { - JLOG(journal_.warn()) << "Included AS root invalid"; + JLOG(journal_.warn()) << "Included AS root invalid for ledger " << hash_ + << " from peer " << peer->id(); + if (san.isInvalid()) + { + peer->charge(resource::kFeeInvalidData, "ledger_data invalid AS root"); + return -1; + } } if (!haveTransactions_ && (packet.nodes().size() > 2) && - !takeTxRootNode(makeSlice(packet.nodes(2).nodedata()), san)) + !takeTxRootNode(packet.nodes(2).nodedata(), san)) { - JLOG(journal_.warn()) << "Included TX root invalid"; + JLOG(journal_.warn()) << "Included TX root invalid for ledger " << hash_ + << " from peer " << peer->id(); + if (san.isInvalid()) + { + peer->charge(resource::kFeeInvalidData, "ledger_data invalid TX root"); + return -1; + } } } catch (std::exception const& ex) { - JLOG(journal_.warn()) << "Included AS/TX root invalid: " << ex.what(); + JLOG(journal_.warn()) << "Included AS/TX root invalid for ledger " << hash_ + << " from peer " << peer->id() << ": " << ex.what(); using namespace std::string_literals; - peer->charge(Resource::kFeeInvalidData, "ledger_data "s + ex.what()); + peer->charge(resource::kFeeInvalidData, "ledger_data "s + ex.what()); return -1; } @@ -1096,30 +1147,24 @@ InboundLedger::processData(std::shared_ptr peer, protocol::TMLedgerData co if (packet.nodes().empty()) { JLOG(journal_.info()) << peer->id() << ": response with no nodes"; - peer->charge(Resource::kFeeMalformedRequest, "ledger_data no nodes"); + peer->charge(resource::kFeeMalformedRequest, "ledger_data no nodes"); return -1; } ScopedLockType const sl(mtx_); - // Verify node IDs and data are complete - for (auto const& node : packet.nodes()) - { - if (!node.has_nodeid() || !node.has_nodedata()) - { - JLOG(journal_.warn()) << "Got bad node"; - peer->charge(Resource::kFeeMalformedRequest, "ledger_data bad node"); - return -1; - } - } - SHAMapAddNode san; - receiveNode(packet, san); + receiveNode(peer, packet, san); JLOG(journal_.debug()) << "Ledger " << ((packet.type() == protocol::liTX_NODE) ? "TX" : "AS") << " node stats: " << san.get(); + // `san` accumulates across the whole packet, so `isInvalid()` (bad_ > 0) does not mean the + // packet had no useful nodes: credit whatever good/useful nodes were sent rather than + // discarding everything because one node in an otherwise-good packet was bad. + // Note: Peer charges for invalid/malformed data are issued from within receiveNode at the + // exact failure site, so the peer is only charged for problems they are responsible for. if (san.isUseful()) progress_ = true; diff --git a/src/xrpld/app/ledger/detail/InboundLedgers.cpp b/src/xrpld/app/ledger/detail/InboundLedgers.cpp index dc361694cf..4d565ca674 100644 --- a/src/xrpld/app/ledger/detail/InboundLedgers.cpp +++ b/src/xrpld/app/ledger/detail/InboundLedgers.cpp @@ -2,13 +2,13 @@ #include #include +#include #include #include #include #include #include -#include #include #include #include @@ -252,23 +252,17 @@ public: Serializer s; try { - for (int i = 0; i < packetPtr->nodes().size(); ++i) + for (auto const& ledgerNode : packetPtr->nodes()) { - auto const& node = packetPtr->nodes(i); - - if (!node.has_nodeid() || !node.has_nodedata()) - return; - - auto newNode = SHAMapTreeNode::makeFromWire(makeSlice(node.nodedata())); - - if (!newNode) + auto const treeNode = getTreeNode(ledgerNode.nodedata()); + if (!treeNode) return; s.erase(); - newNode->serializeWithPrefix(s); + treeNode->serializeWithPrefix(s); app_.getLedgerMaster().addFetchPack( - newNode->getHash().asUInt256(), std::make_shared(s.begin(), s.end())); + treeNode->getHash().asUInt256(), std::make_shared(s.begin(), s.end())); } } catch (std::exception const&) // NOLINT(bugprone-empty-catch) diff --git a/src/xrpld/app/ledger/detail/InboundTransactions.cpp b/src/xrpld/app/ledger/detail/InboundTransactions.cpp index 9b50a1584f..62897fe617 100644 --- a/src/xrpld/app/ledger/detail/InboundTransactions.cpp +++ b/src/xrpld/app/ledger/detail/InboundTransactions.cpp @@ -1,11 +1,11 @@ #include +#include #include #include #include #include -#include #include #include #include @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -137,34 +138,45 @@ public: if (ta == nullptr) { - peer->charge(Resource::kFeeUselessData, "ledger_data"); + peer->charge(resource::kFeeUselessData, "ledger_data useless"); return; } - std::vector> data; + std::vector> data; data.reserve(packet.nodes().size()); - for (auto const& node : packet.nodes()) + for (auto const& ledgerNode : packet.nodes()) { - if (!node.has_nodeid() || !node.has_nodedata()) + auto treeNode = getTreeNode(ledgerNode.nodedata()); + if (!treeNode) { - peer->charge(Resource::kFeeMalformedRequest, "ledger_data"); + JLOG(j_.warn()) << "Got invalid node data for TX set " << hash << " from peer " + << peer->id(); + peer->charge(resource::kFeeInvalidData, "ledger_node.node_data invalid"); return; } - auto const id = deserializeSHAMapNodeID(node.nodeid()); - - if (!id) + auto const nodeID = getSHAMapNodeID(ledgerNode, *treeNode); + if (!nodeID) { - peer->charge(Resource::kFeeInvalidData, "ledger_data"); + JLOG(j_.warn()) << "Got invalid node id for TX set " << hash << " from peer " + << peer->id(); + peer->charge(resource::kFeeInvalidData, "ledger_node.node_id invalid"); return; } - data.emplace_back(*id, makeSlice(node.nodedata())); + data.emplace_back(*nodeID, std::move(treeNode)); } - if (!ta->takeNodes(data, peer).isUseful()) - peer->charge(Resource::kFeeUselessData, "ledger_data not useful"); + auto const san = ta->takeNodes(std::move(data), peer); + if (san.isInvalid()) + { + peer->charge(resource::kFeeInvalidData, "ledger_data invalid"); + } + else if (!san.isUseful()) + { + peer->charge(resource::kFeeUselessData, "ledger_data useless"); + } } void diff --git a/src/xrpld/app/ledger/detail/LedgerDeltaAcquire.cpp b/src/xrpld/app/ledger/detail/LedgerDeltaAcquire.cpp index 7ac85b892e..344d5cb8fc 100644 --- a/src/xrpld/app/ledger/detail/LedgerDeltaAcquire.cpp +++ b/src/xrpld/app/ledger/detail/LedgerDeltaAcquire.cpp @@ -43,10 +43,10 @@ LedgerDeltaAcquire::LedgerDeltaAcquire( : TimeoutCounter( app, ledgerHash, - LedgerReplayParameters::kSubTaskTimeout, + ledger_replay_parameters::kSubTaskTimeout, {.jobType = JtReplayTask, .jobName = "LedReplDelta", - .jobLimit = LedgerReplayParameters::kMaxQueuedTasks}, + .jobLimit = ledger_replay_parameters::kMaxQueuedTasks}, app.getJournal("LedgerReplayDelta")) , inboundLedgers_(inboundLedgers) , ledgerSeq_(ledgerSeq) @@ -101,10 +101,10 @@ LedgerDeltaAcquire::trigger(std::size_t limit, ScopedLockType& sl) } else { - if (++noFeaturePeerCount_ >= LedgerReplayParameters::kMaxNoFeaturePeerCount) + if (++noFeaturePeerCount_ >= ledger_replay_parameters::kMaxNoFeaturePeerCount) { JLOG(journal_.debug()) << "Fall back for " << hash_; - timerInterval_ = LedgerReplayParameters::kSubTaskFallbackTimeout; + timerInterval_ = ledger_replay_parameters::kSubTaskFallbackTimeout; fallBack_ = true; } } @@ -119,7 +119,7 @@ void LedgerDeltaAcquire::onTimer(bool progress, ScopedLockType& sl) { JLOG(journal_.trace()) << "timeouts_=" << timeouts_ << " for " << hash_; - if (timeouts_ > LedgerReplayParameters::kSubTaskMaxTimeouts) + if (timeouts_ > ledger_replay_parameters::kSubTaskMaxTimeouts) { failed_ = true; JLOG(journal_.debug()) << "too many timeouts " << hash_; diff --git a/src/xrpld/app/ledger/detail/LedgerMaster.cpp b/src/xrpld/app/ledger/detail/LedgerMaster.cpp index 2bd83b0f18..83d76bcd2a 100644 --- a/src/xrpld/app/ledger/detail/LedgerMaster.cpp +++ b/src/xrpld/app/ledger/detail/LedgerMaster.cpp @@ -653,7 +653,7 @@ LedgerMaster::tryFill(std::shared_ptr ledger) std::uint32_t minHas = seq; std::uint32_t maxHas = seq; - NodeStore::Database& nodeStore{app_.getNodeStore()}; + node_store::Database& nodeStore{app_.getNodeStore()}; while (!app_.getJobQueue().isStopping() && seq > 0) { { @@ -1042,8 +1042,8 @@ LedgerMaster::checkAccept(std::shared_ptr const& ledger) if (v->isFieldPresent(sfServerVersion)) { auto version = v->getFieldU64(sfServerVersion); - higherVersionCount += BuildInfo::isNewerVersion(version) ? 1 : 0; - xrpldCount += BuildInfo::isXrpldVersion(version) ? 1 : 0; + higherVersionCount += build_info::isNewerVersion(version) ? 1 : 0; + xrpldCount += build_info::isXrpldVersion(version) ? 1 : 0; } } // We report only if (1) we have accumulated validation messages @@ -2088,21 +2088,21 @@ LedgerMaster::makeFetchPack( if (!have) { JLOG(journal_.info()) << "Peer requests fetch pack for ledger we don't have: " << have; - peer->charge(Resource::kFeeRequestNoReply, "get_object ledger"); + peer->charge(resource::kFeeRequestNoReply, "get_object ledger"); return; } if (have->open()) { JLOG(journal_.warn()) << "Peer requests fetch pack from open ledger: " << have; - peer->charge(Resource::kFeeMalformedRequest, "get_object ledger open"); + peer->charge(resource::kFeeMalformedRequest, "get_object ledger open"); return; } if (have->header().seq < getEarliestFetch()) { JLOG(journal_.debug()) << "Peer requests fetch pack that is too early"; - peer->charge(Resource::kFeeMalformedRequest, "get_object ledger early"); + peer->charge(resource::kFeeMalformedRequest, "get_object ledger early"); return; } @@ -2112,7 +2112,7 @@ LedgerMaster::makeFetchPack( { JLOG(journal_.info()) << "Peer requests fetch pack for ledger whose predecessor we " << "don't have: " << have; - peer->charge(Resource::kFeeRequestNoReply, "get_object ledger no parent"); + peer->charge(resource::kFeeRequestNoReply, "get_object ledger no parent"); return; } diff --git a/src/xrpld/app/ledger/detail/LedgerNodeHelpers.cpp b/src/xrpld/app/ledger/detail/LedgerNodeHelpers.cpp new file mode 100644 index 0000000000..531dba59f9 --- /dev/null +++ b/src/xrpld/app/ledger/detail/LedgerNodeHelpers.cpp @@ -0,0 +1,89 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +namespace xrpl { + +SHAMapTreeNodePtr +getTreeNode(std::string_view data) +{ + auto const slice = makeSlice(data); + try + { + return SHAMapTreeNode::makeFromWire(slice); + } + catch (std::exception const&) + { + return {}; + } +} + +std::optional +getSHAMapNodeID(protocol::TMLedgerNode const& ledgerNode, SHAMapTreeNode const& treeNode) +{ + if (ledgerNode.has_id() || ledgerNode.has_depth()) + { + // Reject ambiguous messages that mix the legacy and new reference fields. + if (ledgerNode.has_nodeid()) + return std::nullopt; + + if (treeNode.isInner()) + { + if (!ledgerNode.has_id()) + return std::nullopt; + + REACHABLE("xrpl::getSHAMapNodeID : inner node ID from id field"); + return deserializeSHAMapNodeID(ledgerNode.id()); + } + + if (treeNode.isLeaf()) + { + SOMETIMES( + ledgerNode.has_depth() && ledgerNode.depth() > SHAMap::kLeafDepth, + "xrpl::getSHAMapNodeID : leaf depth exceeds max"); + if (!ledgerNode.has_depth() || ledgerNode.depth() > SHAMap::kLeafDepth) + return std::nullopt; + + auto const key = leafKey(treeNode); + REACHABLE("xrpl::getSHAMapNodeID : leaf node ID reconstructed from depth"); + return SHAMapNodeID::createID(ledgerNode.depth(), key); + } + // LCOV_EXCL_START + UNREACHABLE("xrpl::getSHAMapNodeID : tree node is neither inner nor leaf"); + return std::nullopt; + // LCOV_EXCL_STOP + } + + if (!ledgerNode.has_nodeid()) + return std::nullopt; + + auto nodeID = deserializeSHAMapNodeID(ledgerNode.nodeid()); + if (!nodeID.has_value()) + return std::nullopt; + + if (treeNode.isLeaf()) + { + auto const key = leafKey(treeNode); + auto const expectedID = SHAMapNodeID::createID(static_cast(nodeID->getDepth()), key); + SOMETIMES( + nodeID->getNodeID() != expectedID.getNodeID(), + "xrpl::getSHAMapNodeID : legacy leaf ID inconsistent with key"); + if (nodeID->getNodeID() != expectedID.getNodeID()) + return std::nullopt; + } + + return nodeID; +} + +} // namespace xrpl diff --git a/src/xrpld/app/ledger/detail/LedgerReplayTask.cpp b/src/xrpld/app/ledger/detail/LedgerReplayTask.cpp index e7cd031247..3d7b1e0f92 100644 --- a/src/xrpld/app/ledger/detail/LedgerReplayTask.cpp +++ b/src/xrpld/app/ledger/detail/LedgerReplayTask.cpp @@ -87,18 +87,18 @@ LedgerReplayTask::LedgerReplayTask( : TimeoutCounter( app, parameter.finishHash, - LedgerReplayParameters::kTaskTimeout, + ledger_replay_parameters::kTaskTimeout, {.jobType = JtReplayTask, .jobName = "LedReplTask", - .jobLimit = LedgerReplayParameters::kMaxQueuedTasks}, + .jobLimit = ledger_replay_parameters::kMaxQueuedTasks}, app.getJournal("LedgerReplayTask")) , inboundLedgers_(inboundLedgers) , replayer_(replayer) , parameter_(parameter) , maxTimeouts_( std::max( - LedgerReplayParameters::kTaskMaxTimeoutsMinimum, - parameter.totalLedgers * LedgerReplayParameters::kTaskMaxTimeoutsMultiplier)) + ledger_replay_parameters::kTaskMaxTimeoutsMinimum, + parameter.totalLedgers * ledger_replay_parameters::kTaskMaxTimeoutsMultiplier)) , skipListAcquirer_(skipListAcquirer) { JLOG(journal_.trace()) << "Create " << hash_; diff --git a/src/xrpld/app/ledger/detail/LedgerReplayer.cpp b/src/xrpld/app/ledger/detail/LedgerReplayer.cpp index 3bb5ca9434..52184c1723 100644 --- a/src/xrpld/app/ledger/detail/LedgerReplayer.cpp +++ b/src/xrpld/app/ledger/detail/LedgerReplayer.cpp @@ -51,7 +51,7 @@ LedgerReplayer::replay( { XRPL_ASSERT( finishLedgerHash.isNonZero() && totalNumLedgers > 0 && - totalNumLedgers <= LedgerReplayParameters::kMaxTaskSize, + totalNumLedgers <= ledger_replay_parameters::kMaxTaskSize, "xrpl::LedgerReplayer::replay : valid inputs"); // NOLINTNEXTLINE(misc-const-correctness) @@ -64,7 +64,7 @@ LedgerReplayer::replay( std::scoped_lock const lock(mtx_); if (app_.isStopping()) return; - if (tasks_.size() >= LedgerReplayParameters::kMaxTasks) + if (tasks_.size() >= ledger_replay_parameters::kMaxTasks) { JLOG(j_.info()) << "Too many replay tasks, dropping new task " << parameter.finishHash; return; diff --git a/src/xrpld/app/ledger/detail/LedgerToJson.cpp b/src/xrpld/app/ledger/detail/LedgerToJson.cpp index 7a581e2389..9d3820e9f7 100644 --- a/src/xrpld/app/ledger/detail/LedgerToJson.cpp +++ b/src/xrpld/app/ledger/detail/LedgerToJson.cpp @@ -135,7 +135,7 @@ fillJsonTx( { copyFrom(txJson[jss::tx_json], txn->getJson(JsonOptions::Values::DisableApiPriorV2, false)); txJson[jss::hash] = to_string(txn->getTransactionID()); - RPC::insertDeliverMax(txJson[jss::tx_json], txnType, fill.context->apiVersion); + rpc::insertDeliverMax(txJson[jss::tx_json], txnType, fill.context->apiVersion); if (stMeta) { @@ -144,7 +144,7 @@ fillJsonTx( // If applicable, insert delivered amount if (txnType == ttPAYMENT || txnType == ttCHECK_CASH) { - RPC::insertDeliveredAmount( + rpc::insertDeliveredAmount( txJson[jss::meta], fill.ledger, txn, @@ -152,7 +152,7 @@ fillJsonTx( } // If applicable, insert mpt issuance id - RPC::insertMPTokenIssuanceID( + rpc::insertMPTokenIssuanceID( txJson[jss::meta], txn, {txn->getTransactionID(), fill.ledger.seq(), *stMeta}); } @@ -172,7 +172,7 @@ fillJsonTx( else { copyFrom(txJson, txn->getJson(JsonOptions::Values::None)); - RPC::insertDeliverMax(txJson, txnType, fill.context->apiVersion); + rpc::insertDeliverMax(txJson, txnType, fill.context->apiVersion); if (stMeta) { txJson[jss::metaData] = stMeta->getJson(JsonOptions::Values::None); @@ -180,7 +180,7 @@ fillJsonTx( // If applicable, insert delivered amount if (txnType == ttPAYMENT || txnType == ttCHECK_CASH) { - RPC::insertDeliveredAmount( + rpc::insertDeliveredAmount( txJson[jss::metaData], fill.ledger, txn, @@ -188,7 +188,7 @@ fillJsonTx( } // If applicable, insert mpt issuance id - RPC::insertMPTokenIssuanceID( + rpc::insertMPTokenIssuanceID( txJson[jss::metaData], txn, {txn->getTransactionID(), fill.ledger.seq(), *stMeta}); } } @@ -337,7 +337,7 @@ fillJson(json::Value& json, LedgerFill const& fill) fill.ledger.header(), bFull, ((fill.context != nullptr) ? fill.context->apiVersion - : RPC::kApiMaximumSupportedVersion)); + : rpc::kApiMaximumSupportedVersion)); } if (bFull || ((fill.options & static_cast(LedgerFill::Options::DumpTxrp)) != 0)) diff --git a/src/xrpld/app/ledger/detail/SkipListAcquire.cpp b/src/xrpld/app/ledger/detail/SkipListAcquire.cpp index 8ebd14083a..97b50a9a3a 100644 --- a/src/xrpld/app/ledger/detail/SkipListAcquire.cpp +++ b/src/xrpld/app/ledger/detail/SkipListAcquire.cpp @@ -37,10 +37,10 @@ SkipListAcquire::SkipListAcquire( : TimeoutCounter( app, ledgerHash, - LedgerReplayParameters::kSubTaskTimeout, + ledger_replay_parameters::kSubTaskTimeout, {.jobType = JtReplayTask, .jobName = "SkipListAcq", - .jobLimit = LedgerReplayParameters::kMaxQueuedTasks}, + .jobLimit = ledger_replay_parameters::kMaxQueuedTasks}, app.getJournal("LedgerReplaySkipList")) , inboundLedgers_(inboundLedgers) , peerSet_(std::move(peerSet)) @@ -96,10 +96,10 @@ SkipListAcquire::trigger(std::size_t limit, ScopedLockType& sl) { JLOG(journal_.trace()) << "Add a no feature peer " << peer->id() << " for " << hash_; - if (++noFeaturePeerCount_ >= LedgerReplayParameters::kMaxNoFeaturePeerCount) + if (++noFeaturePeerCount_ >= ledger_replay_parameters::kMaxNoFeaturePeerCount) { JLOG(journal_.debug()) << "Fall back for " << hash_; - timerInterval_ = LedgerReplayParameters::kSubTaskFallbackTimeout; + timerInterval_ = ledger_replay_parameters::kSubTaskFallbackTimeout; fallBack_ = true; } } @@ -114,7 +114,7 @@ void SkipListAcquire::onTimer(bool progress, ScopedLockType& sl) { JLOG(journal_.trace()) << "timeouts_=" << timeouts_ << " for " << hash_; - if (timeouts_ > LedgerReplayParameters::kSubTaskMaxTimeouts) + if (timeouts_ > ledger_replay_parameters::kSubTaskMaxTimeouts) { failed_ = true; JLOG(journal_.debug()) << "too many timeouts " << hash_; diff --git a/src/xrpld/app/ledger/detail/TransactionAcquire.cpp b/src/xrpld/app/ledger/detail/TransactionAcquire.cpp index 62312b04d2..db99299fd6 100644 --- a/src/xrpld/app/ledger/detail/TransactionAcquire.cpp +++ b/src/xrpld/app/ledger/detail/TransactionAcquire.cpp @@ -7,13 +7,13 @@ #include #include -#include #include #include #include #include #include #include +#include #include @@ -171,7 +171,7 @@ TransactionAcquire::trigger(std::shared_ptr const& peer) SHAMapAddNode TransactionAcquire::takeNodes( - std::vector> const& data, + std::vector> data, std::shared_ptr const& peer) { ScopedLockType const sl(mtx_); @@ -195,7 +195,7 @@ TransactionAcquire::takeNodes( ConsensusTransSetSF sf(app_, app_.getTempNodeCache()); - for (auto const& d : data) + for (auto& d : data) { if (d.first.isRoot()) { @@ -203,18 +203,22 @@ TransactionAcquire::takeNodes( { JLOG(journal_.debug()) << "Got root TXS node, already have it"; } - else if (!map_->addRootNode(SHAMapHash{hash_}, d.second, nullptr).isGood()) + else if (!map_->addRootNode(SHAMapHash{hash_}, std::move(d.second), nullptr) + .isGood()) { - JLOG(journal_.warn()) << "TX acquire got bad root node"; + JLOG(journal_.warn()) << "TX acquire got bad root node for TX set " << hash_ + << " from peer " << peer->id(); + return SHAMapAddNode::invalid(); } else { haveRoot_ = true; } } - else if (!map_->addKnownNode(d.first, d.second, &sf).isGood()) + else if (!map_->addKnownNode(d.first, std::move(d.second), &sf).isGood()) { - JLOG(journal_.warn()) << "TX acquire got bad non-root node"; + JLOG(journal_.warn()) << "TX acquire got bad non-root node " << d.first + << " for TX set " << hash_ << " from peer " << peer->id(); return SHAMapAddNode::invalid(); } } diff --git a/src/xrpld/app/ledger/detail/TransactionAcquire.h b/src/xrpld/app/ledger/detail/TransactionAcquire.h index 5b33066390..2faf74b557 100644 --- a/src/xrpld/app/ledger/detail/TransactionAcquire.h +++ b/src/xrpld/app/ledger/detail/TransactionAcquire.h @@ -6,10 +6,10 @@ #include #include -#include #include #include #include +#include #include #include @@ -32,8 +32,8 @@ public: SHAMapAddNode takeNodes( - std::vector> const& data, - std::shared_ptr const&); + std::vector> data, + std::shared_ptr const& peer); void init(int startPeers); diff --git a/src/xrpld/app/main/Application.cpp b/src/xrpld/app/main/Application.cpp index d329475874..52ec9ce544 100644 --- a/src/xrpld/app/main/Application.cpp +++ b/src/xrpld/app/main/Application.cpp @@ -230,9 +230,9 @@ public: std::optional> nodeIdentity_; ValidatorKeys const validatorKeys_; - std::unique_ptr resourceManager_; + std::unique_ptr resourceManager_; - std::unique_ptr nodeStore_; + std::unique_ptr nodeStore_; NodeFamily nodeFamily_; std::unique_ptr orderBookDB_; std::unique_ptr pathRequestManager_; @@ -375,7 +375,7 @@ public: , networkIDService_(std::make_unique(config_->networkId)) , validatorKeys_(*config_, journal_) , resourceManager_( - Resource::makeManager(collectorManager_->collector(), logs_->journal("Resource"))) + resource::makeManager(collectorManager_->collector(), logs_->journal("Resource"))) , nodeStore_(shaMapStore_->makeNodeStore( config_->prefetchWorkers > 0 ? config_->prefetchWorkers : 4)) , nodeFamily_(*this, *collectorManager_) @@ -655,7 +655,7 @@ public: return tempNodeCache_; } - NodeStore::Database& + node_store::Database& getNodeStore() override { return *nodeStore_; @@ -673,7 +673,7 @@ public: return *loadManager_; } - Resource::Manager& + resource::Manager& getResourceManager() override { return *resourceManager_; @@ -860,9 +860,9 @@ public: if (config_->doImport) { auto j = logs_->journal("NodeObject"); - NodeStore::DummyScheduler dummyScheduler; - std::unique_ptr source = - NodeStore::Manager::instance().makeDatabase( + node_store::DummyScheduler dummyScheduler; + std::unique_ptr source = + node_store::Manager::instance().makeDatabase( megabytes(config_->getValueFor(SizedItem::BurstSize, std::nullopt)), dummyScheduler, 0, @@ -1187,7 +1187,7 @@ ApplicationImp::setup(boost::program_options::variables_map const& cmdline) logs_->threshold(Severity::Debug); } - JLOG(journal_.info()) << "Process starting: " << BuildInfo::getFullVersionString() + JLOG(journal_.info()) << "Process starting: " << build_info::getFullVersionString() << ", Instance Cookie: " << instanceCookie_; if (numberOfThreads(*config_) < 2) @@ -1457,9 +1457,9 @@ ApplicationImp::setup(boost::program_options::variables_map const& cmdline) JLOG(journal_.fatal()) << "Startup RPC: " << jvCommand << std::endl; } - Resource::Charge loadType = Resource::kFeeReferenceRpc; - Resource::Consumer c; - RPC::JsonContext context{ + resource::Charge loadType = resource::kFeeReferenceRpc; + resource::Consumer c; + rpc::JsonContext context{ {.j = getJournal("RPCHandler"), .app = *this, .loadType = loadType, @@ -1469,11 +1469,11 @@ ApplicationImp::setup(boost::program_options::variables_map const& cmdline) .role = Role::ADMIN, .coro = {}, .infoSub = {}, - .apiVersion = RPC::kApiMaximumSupportedVersion}, + .apiVersion = rpc::kApiMaximumSupportedVersion}, jvCommand}; json::Value jvResult; - RPC::doCommand(context, jvResult); + rpc::doCommand(context, jvResult); if (!config_->quiet()) { @@ -1489,7 +1489,7 @@ ApplicationImp::setup(boost::program_options::variables_map const& cmdline) void ApplicationImp::start(bool withTimers) { - JLOG(journal_.info()) << "Application starting. Version is " << BuildInfo::getVersionString(); + JLOG(journal_.info()) << "Application starting. Version is " << build_info::getVersionString(); if (withTimers) { diff --git a/src/xrpld/app/main/CollectorManager.cpp b/src/xrpld/app/main/CollectorManager.cpp index 9e1278607f..87b5286f97 100644 --- a/src/xrpld/app/main/CollectorManager.cpp +++ b/src/xrpld/app/main/CollectorManager.cpp @@ -30,8 +30,8 @@ public: if (server == "statsd") { - beast::IP::Endpoint const address( - beast::IP::Endpoint::fromString(get(params, Keys::kAddress))); + beast::ip::Endpoint const address( + beast::ip::Endpoint::fromString(get(params, Keys::kAddress))); std::string const& prefix(get(params, Keys::kPrefix)); collector_ = beast::insight::StatsDCollector::make(address, prefix, journal); diff --git a/src/xrpld/app/main/GRPCServer.cpp b/src/xrpld/app/main/GRPCServer.cpp index 1146cbdc08..1b20ff1d49 100644 --- a/src/xrpld/app/main/GRPCServer.cpp +++ b/src/xrpld/app/main/GRPCServer.cpp @@ -71,10 +71,10 @@ getEndpoint(std::string const& peer) peerClean = peer.substr(first + 1); } - std::optional endpoint = - beast::IP::Endpoint::fromStringChecked(peerClean); + std::optional endpoint = + beast::ip::Endpoint::fromStringChecked(peerClean); if (endpoint) - return beast::IP::toAsioEndpoint(endpoint.value()); + return beast::ip::toAsioEndpoint(endpoint.value()); } catch (std::exception const&) // NOLINT(bugprone-empty-catch) { @@ -92,8 +92,8 @@ GRPCServerImpl::CallData::CallData( BindListener bindListener, Handler handler, Forward forward, - RPC::Condition requiredCondition, - Resource::Charge loadType, + rpc::Condition requiredCondition, + resource::Charge loadType, std::vector const& secureGatewayIPs) : service_(service) , cq_(cq) @@ -195,7 +195,7 @@ GRPCServerImpl::CallData::process(std::shared_ptr context{ + rpc::GRPCContext context{ {app_.getJournal("gRPCServer"), app_, loadType, @@ -209,11 +209,11 @@ GRPCServerImpl::CallData::process(std::shared_ptr::isFinished() } template -Resource::Charge +resource::Charge GRPCServerImpl::CallData::getLoadType() { return loadType_; @@ -323,12 +323,12 @@ GRPCServerImpl::CallData::setIsUnlimited(Response& response, } template -Resource::Consumer +resource::Consumer GRPCServerImpl::CallData::getUsage() { auto endpoint = getClientEndpoint(); if (endpoint) - return app_.getResourceManager().newInboundEndpoint(beast::IP::fromAsio(endpoint.value())); + return app_.getResourceManager().newInboundEndpoint(beast::ip::fromAsio(endpoint.value())); Throw("Failed to get client endpoint"); } @@ -527,7 +527,7 @@ GRPCServerImpl::handleRpcs() std::vector> GRPCServerImpl::setupListeners() { - using RPC::Condition; + using rpc::Condition; std::vector> requests; auto addToRequests = [&requests](auto callData) { requests.push_back(std::move(callData)); }; @@ -545,7 +545,7 @@ GRPCServerImpl::setupListeners() doLedgerGrpc, &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedger, Condition::NoCondition, - Resource::kFeeMediumBurdenRpc, + resource::kFeeMediumBurdenRpc, secureGatewayIPs_)); } { @@ -562,7 +562,7 @@ GRPCServerImpl::setupListeners() doLedgerDataGrpc, &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedgerData, Condition::NoCondition, - Resource::kFeeMediumBurdenRpc, + resource::kFeeMediumBurdenRpc, secureGatewayIPs_)); } { @@ -579,7 +579,7 @@ GRPCServerImpl::setupListeners() doLedgerDiffGrpc, &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedgerDiff, Condition::NoCondition, - Resource::kFeeMediumBurdenRpc, + resource::kFeeMediumBurdenRpc, secureGatewayIPs_)); } { @@ -596,7 +596,7 @@ GRPCServerImpl::setupListeners() doLedgerEntryGrpc, &org::xrpl::rpc::v1::XRPLedgerAPIService::Stub::GetLedgerEntry, Condition::NoCondition, - Resource::kFeeMediumBurdenRpc, + resource::kFeeMediumBurdenRpc, secureGatewayIPs_)); } return requests; diff --git a/src/xrpld/app/main/GRPCServer.h b/src/xrpld/app/main/GRPCServer.h index db948cab99..98b50fcd0c 100644 --- a/src/xrpld/app/main/GRPCServer.h +++ b/src/xrpld/app/main/GRPCServer.h @@ -102,7 +102,7 @@ private: // typedef for actual handler (that populates a response) // handlers are defined in rpc/GRPCHandlers.h template - using Handler = std::function(RPC::GRPCContext&)>; + using Handler = std::function(rpc::GRPCContext&)>; // This implementation is currently limited to v1 of the API static constexpr unsigned kApiVersion = 1; @@ -189,10 +189,10 @@ private: Forward forward_; // Condition required for this RPC - RPC::Condition requiredCondition_; + rpc::Condition requiredCondition_; // Load type for this RPC - Resource::Charge loadType_; + resource::Charge loadType_; std::vector const& secureGatewayIPs_; @@ -209,8 +209,8 @@ private: BindListener bindListener, Handler handler, Forward forward, - RPC::Condition requiredCondition, - Resource::Charge loadType, + rpc::Condition requiredCondition, + resource::Charge loadType, std::vector const& secureGatewayIPs); CallData(CallData const&) = delete; @@ -233,7 +233,7 @@ private: process(std::shared_ptr coro); // return load type of this RPC - Resource::Charge + resource::Charge getLoadType(); // return the Role used for this RPC @@ -241,7 +241,7 @@ private: getRole(bool isUnlimited); // register endpoint with ResourceManager and return usage - Resource::Consumer + resource::Consumer getUsage(); // Returns the ip of the client @@ -290,7 +290,7 @@ private: // forward request to a p2p node void - forwardToP2p(RPC::GRPCContext& context); + forwardToP2p(rpc::GRPCContext& context); }; // CallData diff --git a/src/xrpld/app/main/Main.cpp b/src/xrpld/app/main/Main.cpp index d0b40efce8..a23b84f2e8 100644 --- a/src/xrpld/app/main/Main.cpp +++ b/src/xrpld/app/main/Main.cpp @@ -506,7 +506,7 @@ run(int argc, char** argv) if (vm.contains("version")) { // LCOV_EXCL_START - std::cout << "xrpld version " << BuildInfo::getVersionString() << std::endl; + std::cout << "xrpld version " << build_info::getVersionString() << std::endl; std::cout << "Git commit hash: " << xrpl::git::getCommitHash() << std::endl; std::cout << "Git build branch: " << xrpl::git::getBuildBranch() << std::endl; return 0; @@ -716,7 +716,7 @@ run(int argc, char** argv) // happen after the config file is loaded. if (vm.contains("rpc_ip")) { - auto endpoint = beast::IP::Endpoint::fromStringChecked(vm["rpc_ip"].as()); + auto endpoint = beast::ip::Endpoint::fromStringChecked(vm["rpc_ip"].as()); if (!endpoint) { std::cerr << "Invalid rpc_ip = " << vm["rpc_ip"].as() << "\n"; @@ -826,7 +826,7 @@ run(int argc, char** argv) // We have an RPC command to process: beast::setCurrentThreadName("xrpld: rpc"); - return RPCCall::fromCommandLine( + return rpc_call::fromCommandLine( *config, vm["parameters"].as>(), *logs); // LCOV_EXCL_STOP } diff --git a/src/xrpld/app/main/NodeStoreScheduler.cpp b/src/xrpld/app/main/NodeStoreScheduler.cpp index 7892503f90..c3ac5d78cf 100644 --- a/src/xrpld/app/main/NodeStoreScheduler.cpp +++ b/src/xrpld/app/main/NodeStoreScheduler.cpp @@ -12,7 +12,7 @@ NodeStoreScheduler::NodeStoreScheduler(JobQueue& jobQueue) : jobQueue_(jobQueue) } void -NodeStoreScheduler::scheduleTask(NodeStore::Task& task) +NodeStoreScheduler::scheduleTask(node_store::Task& task) { if (jobQueue_.isStopped()) return; @@ -26,19 +26,19 @@ NodeStoreScheduler::scheduleTask(NodeStore::Task& task) } void -NodeStoreScheduler::onFetch(NodeStore::FetchReport const& report) +NodeStoreScheduler::onFetch(node_store::FetchReport const& report) { if (jobQueue_.isStopped()) return; jobQueue_.addLoadEvents( - report.fetchType == NodeStore::FetchType::Async ? JtNsAsyncRead : JtNsSyncRead, + report.fetchType == node_store::FetchType::Async ? JtNsAsyncRead : JtNsSyncRead, 1, report.elapsed); } void -NodeStoreScheduler::onBatchWrite(NodeStore::BatchWriteReport const& report) +NodeStoreScheduler::onBatchWrite(node_store::BatchWriteReport const& report) { if (jobQueue_.isStopped()) return; diff --git a/src/xrpld/app/main/NodeStoreScheduler.h b/src/xrpld/app/main/NodeStoreScheduler.h index 8bfd1607ae..09a48d5be1 100644 --- a/src/xrpld/app/main/NodeStoreScheduler.h +++ b/src/xrpld/app/main/NodeStoreScheduler.h @@ -7,19 +7,19 @@ namespace xrpl { /** - * A NodeStore::Scheduler which uses the JobQueue. + * A node_store::Scheduler which uses the JobQueue. */ -class NodeStoreScheduler : public NodeStore::Scheduler +class NodeStoreScheduler : public node_store::Scheduler { public: explicit NodeStoreScheduler(JobQueue& jobQueue); void - scheduleTask(NodeStore::Task& task) override; + scheduleTask(node_store::Task& task) override; void - onFetch(NodeStore::FetchReport const& report) override; + onFetch(node_store::FetchReport const& report) override; void - onBatchWrite(NodeStore::BatchWriteReport const& report) override; + onBatchWrite(node_store::BatchWriteReport const& report) override; private: JobQueue& jobQueue_; diff --git a/src/xrpld/app/misc/DeliverMax.h b/src/xrpld/app/misc/DeliverMax.h index 73ccc95800..1683219c8b 100644 --- a/src/xrpld/app/misc/DeliverMax.h +++ b/src/xrpld/app/misc/DeliverMax.h @@ -6,7 +6,7 @@ namespace json { class Value; } // namespace json -namespace xrpl::RPC { +namespace xrpl::rpc { /** * Copy `Amount` field to `DeliverMax` field in transaction output JSON. @@ -22,4 +22,4 @@ insertDeliverMax(json::Value& txJson, TxType txnType, unsigned int apiVersion); /** @} */ -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/app/misc/NetworkOPs.cpp b/src/xrpld/app/misc/NetworkOPs.cpp index 47cbebb901..8f31ce1eb3 100644 --- a/src/xrpld/app/misc/NetworkOPs.cpp +++ b/src/xrpld/app/misc/NetworkOPs.cpp @@ -1215,7 +1215,7 @@ NetworkOPsImp::processClusterTimer() n.set_nodename(node.name()); }); - Resource::Gossip const gossip = registry_.get().getResourceManager().exportConsumers(); + resource::Gossip const gossip = registry_.get().getResourceManager().exportConsumers(); for (auto& item : gossip.items) { protocol::TMLoadSource& node = *cluster.add_loadsources(); @@ -2487,7 +2487,7 @@ NetworkOPsImp::pubValidation(std::shared_ptr const& val) // for consumers supporting different API versions MultiApiJson multiObj{jvObj}; multiObj.visit( - RPC::kApiVersion<1>, // + rpc::kApiVersion<1>, // [](json::Value& jvTx) { // Type conversion for older API versions to string if (jvTx.isMember(jss::ledger_index)) @@ -2688,7 +2688,7 @@ NetworkOPsImp::getServerInfo(bool human, bool admin, bool counters) if (!registry_.get().getApp().config().serverDomain.empty()) info[jss::server_domain] = registry_.get().getApp().config().serverDomain; - info[jss::build_version] = BuildInfo::getVersionString(); + info[jss::build_version] = build_info::getVersionString(); info[jss::server_state] = strOperatingMode(admin); @@ -3167,7 +3167,7 @@ NetworkOPsImp::pubLedger(std::shared_ptr const& lpAccepted) if (!streamMaps_[SBookChanges].empty()) { - json::Value const jvObj = xrpl::RPC::computeBookChanges(lpAccepted); + json::Value const jvObj = xrpl::rpc::computeBookChanges(lpAccepted); auto it = streamMaps_[SBookChanges].begin(); while (it != streamMaps_[SBookChanges].end()) @@ -3281,9 +3281,9 @@ NetworkOPsImp::transJson( if (meta) { jvObj[jss::meta] = meta->get().getJson(JsonOptions::Values::None); - RPC::insertDeliveredAmount(jvObj[jss::meta], *ledger, transaction, meta->get()); - RPC::insertNFTSyntheticInJson(jvObj, transaction, meta->get()); - RPC::insertMPTokenIssuanceID(jvObj[jss::meta], transaction, meta->get()); + rpc::insertDeliveredAmount(jvObj[jss::meta], *ledger, transaction, meta->get()); + rpc::insertNFTSyntheticInJson(jvObj, transaction, meta->get()); + rpc::insertMPTokenIssuanceID(jvObj[jss::meta], transaction, meta->get()); } // add CTID where the needed data for it exists @@ -3295,7 +3295,7 @@ NetworkOPsImp::transJson( if (transaction->isFieldPresent(sfNetworkID)) netID = transaction->getFieldU32(sfNetworkID); - if (std::optional ctid = RPC::encodeCTID(ledger->header().seq, txnSeq, netID); + if (std::optional ctid = rpc::encodeCTID(ledger->header().seq, txnSeq, netID); ctid) jvObj[jss::ctid] = *ctid; } @@ -3346,7 +3346,7 @@ NetworkOPsImp::transJson( forAllApiVersions( multiObj.visit(), // [&](json::Value& jvTx, std::integral_constant) { - RPC::insertDeliverMax(jvTx[jss::transaction], transaction->getTxnType(), Version); + rpc::insertDeliverMax(jvTx[jss::transaction], transaction->getTxnType(), Version); if constexpr (Version > 1) { @@ -3891,7 +3891,7 @@ NetworkOPsImp::addAccountHistoryJob(SubAccountHistoryInfoWeak subInfo) int feeChargeCount = 0; if (auto sptr = subInfo.sinkWptr.lock(); sptr) { - sptr->getConsumer().charge(Resource::kFeeMediumBurdenRpc); + sptr->getConsumer().charge(resource::kFeeMediumBurdenRpc); ++feeChargeCount; } else diff --git a/src/xrpld/app/misc/SHAMapStore.h b/src/xrpld/app/misc/SHAMapStore.h index df696c685f..eeb04df53d 100644 --- a/src/xrpld/app/misc/SHAMapStore.h +++ b/src/xrpld/app/misc/SHAMapStore.h @@ -43,7 +43,7 @@ public: [[nodiscard]] virtual std::uint32_t clampFetchDepth(std::uint32_t fetchDepth) const = 0; - virtual std::unique_ptr + virtual std::unique_ptr makeNodeStore(int readThreads) = 0; /** @@ -101,5 +101,5 @@ public: //------------------------------------------------------------------------------ std::unique_ptr -makeSHAMapStore(Application& app, NodeStore::Scheduler& scheduler, beast::Journal journal); +makeSHAMapStore(Application& app, node_store::Scheduler& scheduler, beast::Journal journal); } // namespace xrpl diff --git a/src/xrpld/app/misc/SHAMapStoreImp.cpp b/src/xrpld/app/misc/SHAMapStoreImp.cpp index 9b5f412fc5..e41837d206 100644 --- a/src/xrpld/app/misc/SHAMapStoreImp.cpp +++ b/src/xrpld/app/misc/SHAMapStoreImp.cpp @@ -94,7 +94,7 @@ SHAMapStoreImp::SavedStateDB::setLastRotated(LedgerIndex seq) SHAMapStoreImp::SHAMapStoreImp( Application& app, - NodeStore::Scheduler& scheduler, + node_store::Scheduler& scheduler, beast::Journal journal) : app_(app) , scheduler_(scheduler) @@ -165,7 +165,7 @@ SHAMapStoreImp::SHAMapStoreImp( } } -std::unique_ptr +std::unique_ptr SHAMapStoreImp::makeNodeStore(int readThreads) { auto nscfg = app_.config().section(Sections::kNodeDatabase); @@ -185,7 +185,7 @@ SHAMapStoreImp::makeNodeStore(int readThreads) std::to_string(app_.config().getValueFor(SizedItem::TreeCacheAge, std::nullopt))); } - std::unique_ptr db; + std::unique_ptr db; if (deleteInterval_ != 0u) { @@ -201,7 +201,7 @@ SHAMapStoreImp::makeNodeStore(int readThreads) // Create NodeStore with two backends to allow online deletion of // data - auto dbr = std::make_unique( + auto dbr = std::make_unique( scheduler_, readThreads, std::move(writableBackend), @@ -210,11 +210,11 @@ SHAMapStoreImp::makeNodeStore(int readThreads) app_.getJournal(kNodeStoreName)); fdRequired_ += dbr->fdRequired(); dbRotating_ = dbr.get(); - db.reset(dynamic_cast(dbr.release())); + db.reset(dynamic_cast(dbr.release())); } else { - db = NodeStore::Manager::instance().makeDatabase( + db = node_store::Manager::instance().makeDatabase( megabytes(app_.config().getValueFor(SizedItem::BurstSize, std::nullopt)), scheduler_, readThreads, @@ -257,7 +257,7 @@ SHAMapStoreImp::copyNode(std::uint64_t& nodeCount, SHAMapTreeNode const& node) { // Copy a single record from node to dbRotating_ auto obj = dbRotating_->fetchNodeObject( - node.getHash().asUInt256(), 0, NodeStore::FetchType::Synchronous, true); + node.getHash().asUInt256(), 0, node_store::FetchType::Synchronous, true); if (!obj) { XRPL_ASSERT(node.cowid() == 0, "SHAMapStoreImp::copyNode : rescued node must be clean"); @@ -374,7 +374,7 @@ SHAMapStoreImp::run() // exception) also clear the flag. struct RotationExposureGuard { - NodeStore::DatabaseRotating& db; + node_store::DatabaseRotating& db; ~RotationExposureGuard() { db.setRotationInFlight(false); @@ -516,7 +516,7 @@ SHAMapStoreImp::dbPaths() boost::filesystem::remove_all(p); } -std::unique_ptr +std::unique_ptr SHAMapStoreImp::makeBackendRotating(std::string path) { Section section{app_.config().section(Sections::kNodeDatabase)}; @@ -535,7 +535,7 @@ SHAMapStoreImp::makeBackendRotating(std::string path) } section.set(Keys::kPath, newPath.string()); - auto backend{NodeStore::Manager::instance().makeBackend( + auto backend{node_store::Manager::instance().makeBackend( section, megabytes(app_.config().getValueFor(SizedItem::BurstSize, std::nullopt)), scheduler_, @@ -702,7 +702,7 @@ SHAMapStoreImp::minimumOnline() const //------------------------------------------------------------------------------ std::unique_ptr -makeSHAMapStore(Application& app, NodeStore::Scheduler& scheduler, beast::Journal journal) +makeSHAMapStore(Application& app, node_store::Scheduler& scheduler, beast::Journal journal) { return std::make_unique(app, scheduler, journal); } diff --git a/src/xrpld/app/misc/SHAMapStoreImp.h b/src/xrpld/app/misc/SHAMapStoreImp.h index a0ca59ecc8..8a1b7504b9 100644 --- a/src/xrpld/app/misc/SHAMapStoreImp.h +++ b/src/xrpld/app/misc/SHAMapStoreImp.h @@ -81,9 +81,9 @@ private: // minimum ledger to maintain online. std::atomic minimumOnline_; - NodeStore::Scheduler& scheduler_; + node_store::Scheduler& scheduler_; beast::Journal const journal_; - NodeStore::DatabaseRotating* dbRotating_ = nullptr; + node_store::DatabaseRotating* dbRotating_ = nullptr; SavedStateDB stateDb_; std::thread thread_; bool stop_ = false; @@ -119,7 +119,7 @@ private: static constexpr auto kNodeStoreName = "NodeStore"; public: - SHAMapStoreImp(Application& app, NodeStore::Scheduler& scheduler, beast::Journal journal); + SHAMapStoreImp(Application& app, node_store::Scheduler& scheduler, beast::Journal journal); std::uint32_t clampFetchDepth(std::uint32_t fetchDepth) const override @@ -127,7 +127,7 @@ public: return (deleteInterval_ != 0u) ? std::min(fetchDepth, deleteInterval_) : fetchDepth; } - std::unique_ptr + std::unique_ptr makeNodeStore(int readThreads) override; LedgerIndex @@ -180,7 +180,7 @@ private: void dbPaths(); - std::unique_ptr + std::unique_ptr makeBackendRotating(std::string path = std::string()); template @@ -191,7 +191,7 @@ private: for (auto const& key : cache.getKeys()) { - dbRotating_->fetchNodeObject(key, 0, NodeStore::FetchType::Synchronous, true); + dbRotating_->fetchNodeObject(key, 0, node_store::FetchType::Synchronous, true); if (!(++check % checkHealthInterval_) && healthWait() == HealthResult::Stopping) return true; } diff --git a/src/xrpld/app/misc/detail/DeliverMax.cpp b/src/xrpld/app/misc/detail/DeliverMax.cpp index add3cf89ee..e512b078c7 100644 --- a/src/xrpld/app/misc/detail/DeliverMax.cpp +++ b/src/xrpld/app/misc/detail/DeliverMax.cpp @@ -3,7 +3,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { void insertDeliverMax(json::Value& txJson, TxType txnType, unsigned int apiVersion) @@ -19,4 +19,4 @@ insertDeliverMax(json::Value& txJson, TxType txnType, unsigned int apiVersion) } } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/app/misc/detail/Transaction.cpp b/src/xrpld/app/misc/detail/Transaction.cpp index e29181bfe9..59cc4b6c4c 100644 --- a/src/xrpld/app/misc/detail/Transaction.cpp +++ b/src/xrpld/app/misc/detail/Transaction.cpp @@ -182,7 +182,7 @@ Transaction::getJson(JsonOptions options, bool binary) const if (txnSeq_ && netID) { - std::optional const ctid = RPC::encodeCTID(ledgerIndex_, *txnSeq_, *netID); + std::optional const ctid = rpc::encodeCTID(ledgerIndex_, *txnSeq_, *netID); if (ctid) ret[jss::ctid] = *ctid; } diff --git a/src/xrpld/app/misc/detail/WorkBase.h b/src/xrpld/app/misc/detail/WorkBase.h index 73e5081036..fd36cb5318 100644 --- a/src/xrpld/app/misc/detail/WorkBase.h +++ b/src/xrpld/app/misc/detail/WorkBase.h @@ -223,7 +223,7 @@ WorkBase::onStart() req_.target(path_.empty() ? "/" : path_); req_.version(11); req_.set("Host", host_ + ":" + port_); - req_.set("User-Agent", BuildInfo::getFullVersionString()); + req_.set("User-Agent", build_info::getFullVersionString()); req_.prepare_payload(); boost::beast::http::async_write( impl().stream(), diff --git a/src/xrpld/app/rdb/PeerFinder.h b/src/xrpld/app/rdb/PeerFinder.h index 5d916000a3..3ff7b7268b 100644 --- a/src/xrpld/app/rdb/PeerFinder.h +++ b/src/xrpld/app/rdb/PeerFinder.h @@ -44,6 +44,6 @@ readPeerFinderDB(soci::session& session, std::function const& v); +savePeerFinderDB(soci::session& session, std::vector const& v); } // namespace xrpl diff --git a/src/xrpld/app/rdb/detail/PeerFinder.cpp b/src/xrpld/app/rdb/detail/PeerFinder.cpp index 72a275c7cd..8d9af69ae3 100644 --- a/src/xrpld/app/rdb/detail/PeerFinder.cpp +++ b/src/xrpld/app/rdb/detail/PeerFinder.cpp @@ -108,7 +108,7 @@ updatePeerFinderDB(soci::session& session, int currentSchemaVersion, beast::Jour std::size_t count = 0; session << "SELECT COUNT(*) FROM PeerFinder_BootstrapCache;", soci::into(count); - std::vector list; + std::vector list; { list.reserve(count); @@ -125,8 +125,8 @@ updatePeerFinderDB(soci::session& session, int currentSchemaVersion, beast::Jour st.execute(); while (st.fetch()) { - PeerFinder::Store::Entry entry; - entry.endpoint = beast::IP::Endpoint::fromString(s); + peer_finder::Store::Entry entry; + entry.endpoint = beast::ip::Endpoint::fromString(s); if (!isUnspecified(entry.endpoint)) { entry.valence = valence; @@ -226,7 +226,7 @@ readPeerFinderDB(soci::session& session, std::function const& v) +savePeerFinderDB(soci::session& session, std::vector const& v) { soci::transaction tr(session); session << "DELETE FROM PeerFinder_BootstrapCache;"; diff --git a/src/xrpld/core/Config.h b/src/xrpld/core/Config.h index 852e46218a..d43a7a566d 100644 --- a/src/xrpld/core/Config.h +++ b/src/xrpld/core/Config.h @@ -277,7 +277,7 @@ public: std::size_t txRelayPercentage = 25; // These override the command line client settings - std::optional rpcIp; + std::optional rpcIp; std::unordered_set> features; diff --git a/src/xrpld/overlay/Overlay.h b/src/xrpld/overlay/Overlay.h index 6cc229f5a0..9ab80e6697 100644 --- a/src/xrpld/overlay/Overlay.h +++ b/src/xrpld/overlay/Overlay.h @@ -55,7 +55,7 @@ public: explicit Setup() = default; std::shared_ptr context; - beast::IP::Address publicIp; + beast::ip::Address publicIp; int ipLimit = 0; std::uint32_t crawlOptions = 0; std::optional networkID; @@ -92,7 +92,7 @@ public: * performed asynchronously. */ virtual void - connect(beast::IP::Endpoint const& address) = 0; + connect(beast::ip::Endpoint const& address) = 0; /** * Returns the maximum number of peers we are configured to allow. diff --git a/src/xrpld/overlay/Peer.h b/src/xrpld/overlay/Peer.h index 23a45dc512..c2631cc7ce 100644 --- a/src/xrpld/overlay/Peer.h +++ b/src/xrpld/overlay/Peer.h @@ -15,14 +15,15 @@ namespace xrpl { -namespace Resource { +namespace resource { class Charge; -} // namespace Resource +} // namespace resource enum class ProtocolFeature { ValidatorListPropagation, ValidatorList2Propagation, LedgerReplay, + LedgerNodeDepth, }; /** @@ -50,7 +51,7 @@ public: virtual void send(std::shared_ptr const& m) = 0; - [[nodiscard]] virtual beast::IP::Endpoint + [[nodiscard]] virtual beast::ip::Endpoint getRemoteAddress() const = 0; /** @@ -75,7 +76,7 @@ public: * Adjust this peer's load balance based on the type of load imposed. */ virtual void - charge(Resource::Charge const& fee, std::string const& context) = 0; + charge(resource::Charge const& fee, std::string const& context) = 0; // // Identity diff --git a/src/xrpld/overlay/detail/ConnectAttempt.cpp b/src/xrpld/overlay/detail/ConnectAttempt.cpp index 0f0b3242de..b78b8eb7b8 100644 --- a/src/xrpld/overlay/detail/ConnectAttempt.cpp +++ b/src/xrpld/overlay/detail/ConnectAttempt.cpp @@ -49,10 +49,10 @@ ConnectAttempt::ConnectAttempt( Application& app, boost::asio::io_context& ioContext, endpoint_type remoteEndpoint, - Resource::Consumer usage, + resource::Consumer usage, shared_context const& context, Peer::id_t id, - std::shared_ptr const& slot, + std::shared_ptr const& slot, beast::Journal journal, OverlayImpl& overlay) : Child(overlay) @@ -462,7 +462,7 @@ ConnectAttempt::processResponse() auto const result = overlay_.peerFinder().activate(slot_, publicKey, static_cast(member)); - if (result != PeerFinder::Result::Success) + if (result != peer_finder::Result::Success) { fail("Outbound " + std::string(to_string(result))); return; diff --git a/src/xrpld/overlay/detail/ConnectAttempt.h b/src/xrpld/overlay/detail/ConnectAttempt.h index f9ba33571f..3ebffc529d 100644 --- a/src/xrpld/overlay/detail/ConnectAttempt.h +++ b/src/xrpld/overlay/detail/ConnectAttempt.h @@ -41,7 +41,7 @@ private: beast::WrappedSink sink_; beast::Journal const journal_; endpoint_type remoteEndpoint_; - Resource::Consumer usage_; + resource::Consumer usage_; boost::asio::strand strand_; boost::asio::basic_waitable_timer timer_; std::unique_ptr streamPtr_; @@ -49,7 +49,7 @@ private: stream_type& stream_; boost::beast::multi_buffer readBuf_; response_type response_; - std::shared_ptr slot_; + std::shared_ptr slot_; request_type req_; public: @@ -57,10 +57,10 @@ public: Application& app, boost::asio::io_context& ioContext, endpoint_type remoteEndpoint, - Resource::Consumer usage, + resource::Consumer usage, shared_context const& context, Peer::id_t id, - std::shared_ptr const& slot, + std::shared_ptr const& slot, beast::Journal journal, OverlayImpl& overlay); @@ -102,7 +102,7 @@ private: static boost::asio::ip::tcp::endpoint parseEndpoint(std::string const& s, boost::system::error_code& ec) { - beast::IP::Endpoint bep; + beast::ip::Endpoint bep; std::istringstream is(s); is >> bep; if (is.fail()) diff --git a/src/xrpld/overlay/detail/Handshake.cpp b/src/xrpld/overlay/detail/Handshake.cpp index a860d2d604..a12923d1c3 100644 --- a/src/xrpld/overlay/detail/Handshake.cpp +++ b/src/xrpld/overlay/detail/Handshake.cpp @@ -186,8 +186,8 @@ buildHandshake( boost::beast::http::fields& h, xrpl::uint256 const& sharedValue, std::optional networkID, - beast::IP::Address publicIp, - beast::IP::Address remoteIp, + beast::ip::Address publicIp, + beast::ip::Address remoteIp, Application& app) { if (networkID) @@ -213,7 +213,7 @@ buildHandshake( if (!app.config().serverDomain.empty()) h.insert("Server-Domain", app.config().serverDomain); - if (beast::IP::isPublic(remoteIp)) + if (beast::ip::isPublic(remoteIp)) h.insert("Remote-IP", remoteIp.to_string()); if (!publicIp.is_unspecified()) @@ -231,8 +231,8 @@ verifyHandshake( boost::beast::http::fields const& headers, xrpl::uint256 const& sharedValue, std::optional networkID, - beast::IP::Address publicIp, - beast::IP::Address remote, + beast::ip::Address publicIp, + beast::ip::Address remote, Application& app) { if (auto const iter = headers.find("Server-Domain"); iter != headers.end()) @@ -331,7 +331,7 @@ verifyHandshake( if (ec) throw std::runtime_error("Invalid Local-IP"); - if (beast::IP::isPublic(remote) && remote != localIp) + if (beast::ip::isPublic(remote) && remote != localIp) { throw std::runtime_error( "Incorrect Local-IP: " + remote.to_string() + " instead of " + localIp.to_string()); @@ -346,7 +346,7 @@ verifyHandshake( if (ec) throw std::runtime_error("Invalid Remote-IP"); - if (beast::IP::isPublic(remote) && !beast::IP::isUnspecified(publicIp)) + if (beast::ip::isPublic(remote) && !beast::ip::isUnspecified(publicIp)) { // We know our public IP and peer reports our connection came // from some other IP. @@ -374,7 +374,7 @@ makeRequest( m.method(boost::beast::http::verb::get); m.target("/"); m.version(11); - m.insert("User-Agent", BuildInfo::getFullVersionString()); + m.insert("User-Agent", build_info::getFullVersionString()); m.insert("Upgrade", supportedProtocolVersions()); m.insert("Connection", "Upgrade"); m.insert("Connect-As", "Peer"); @@ -390,8 +390,8 @@ http_response_type makeResponse( bool crawlPublic, http_request_type const& req, - beast::IP::Address publicIp, - beast::IP::Address remoteIp, + beast::ip::Address publicIp, + beast::ip::Address remoteIp, uint256 const& sharedValue, std::optional networkID, ProtocolVersion protocol, @@ -403,7 +403,7 @@ makeResponse( resp.insert("Connection", "Upgrade"); resp.insert("Upgrade", to_string(protocol)); resp.insert("Connect-As", "Peer"); - resp.insert("Server", BuildInfo::getFullVersionString()); + resp.insert("Server", build_info::getFullVersionString()); resp.insert("Crawl", crawlPublic ? "public" : "private"); resp.insert( "X-Protocol-Ctl", diff --git a/src/xrpld/overlay/detail/Handshake.h b/src/xrpld/overlay/detail/Handshake.h index 9a4e5ba507..d54cd3a0ea 100644 --- a/src/xrpld/overlay/detail/Handshake.h +++ b/src/xrpld/overlay/detail/Handshake.h @@ -47,8 +47,8 @@ buildHandshake( boost::beast::http::fields& h, uint256 const& sharedValue, std::optional networkID, - beast::IP::Address publicIp, - beast::IP::Address remoteIp, + beast::ip::Address publicIp, + beast::ip::Address remoteIp, Application& app); /** @@ -68,8 +68,8 @@ verifyHandshake( boost::beast::http::fields const& headers, uint256 const& sharedValue, std::optional networkID, - beast::IP::Address publicIp, - beast::IP::Address remote, + beast::ip::Address publicIp, + beast::ip::Address remote, Application& app); /** @@ -109,8 +109,8 @@ http_response_type makeResponse( bool crawlPublic, http_request_type const& req, - beast::IP::Address publicIp, - beast::IP::Address remoteIp, + beast::ip::Address publicIp, + beast::ip::Address remoteIp, uint256 const& sharedValue, std::optional networkID, ProtocolVersion version, diff --git a/src/xrpld/overlay/detail/OverlayImpl.cpp b/src/xrpld/overlay/detail/OverlayImpl.cpp index f9972548d1..81ead14111 100644 --- a/src/xrpld/overlay/detail/OverlayImpl.cpp +++ b/src/xrpld/overlay/detail/OverlayImpl.cpp @@ -93,13 +93,13 @@ namespace xrpl { -namespace CrawlOptions { +namespace crawl_options { static constexpr auto kDisabled = 0; static constexpr auto kOverlay = (1 << 0); static constexpr auto kServerInfo = (1 << 1); static constexpr auto kServerCounts = (1 << 2); static constexpr auto kUnl = (1 << 3); -} // namespace CrawlOptions +} // namespace crawl_options //------------------------------------------------------------------------------ @@ -155,7 +155,7 @@ OverlayImpl::Timer::onTimer(error_code ec) if (overlay_.app_.config().txReduceRelayEnable) overlay_.sendTxQueue(); - if ((++overlay_.timerCount_ % Tuning::kCheckIdlePeers) == 0) + if ((++overlay_.timerCount_ % tuning::kCheckIdlePeers) == 0) overlay_.deleteIdlePeers(); asyncWait(); @@ -167,7 +167,7 @@ OverlayImpl::OverlayImpl( Application& app, Setup setup, ServerHandler& serverHandler, - Resource::Manager& resourceManager, + resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_context& ioContext, BasicConfig const& config, @@ -182,7 +182,7 @@ OverlayImpl::OverlayImpl( , resourceManager_(resourceManager) , store_(app_.getJournal("PeerFinder")) , peerFinder_( - PeerFinder::makeManager( + peer_finder::makeManager( ioContext, stopwatch(), app_.getJournal("PeerFinder"), @@ -308,7 +308,7 @@ OverlayImpl::onHandoff( bool const reserved = static_cast(app_.getCluster().member(publicKey)) || app_.getPeerReservations().contains(publicKey); auto const result = peerFinder_->activate(slot, publicKey, reserved); - if (result != PeerFinder::Result::Success) + if (result != peer_finder::Result::Success) { peerFinder_->onClosed(slot); JLOG(journal.debug()) @@ -381,14 +381,14 @@ OverlayImpl::makePrefix(std::uint32_t id) std::shared_ptr OverlayImpl::makeRedirectResponse( - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type const& request, address_type remoteAddress) { boost::beast::http::response msg; msg.version(request.version()); msg.result(boost::beast::http::status::service_unavailable); - msg.insert("Server", BuildInfo::getFullVersionString()); + msg.insert("Server", build_info::getFullVersionString()); { std::ostringstream ostr; ostr << remoteAddress; @@ -408,7 +408,7 @@ OverlayImpl::makeRedirectResponse( std::shared_ptr OverlayImpl::makeErrorResponse( - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type const& request, address_type remoteAddress, std::string const& text) @@ -417,7 +417,7 @@ OverlayImpl::makeErrorResponse( msg.version(request.version()); msg.result(boost::beast::http::status::bad_request); msg.reason("Bad Request (" + text + ")"); - msg.insert("Server", BuildInfo::getFullVersionString()); + msg.insert("Server", build_info::getFullVersionString()); msg.insert("Remote-Address", remoteAddress.to_string()); msg.insert(boost::beast::http::field::connection, "close"); msg.prepare_payload(); @@ -427,7 +427,7 @@ OverlayImpl::makeErrorResponse( //------------------------------------------------------------------------------ void -OverlayImpl::connect(beast::IP::Endpoint const& remoteEndpoint) +OverlayImpl::connect(beast::ip::Endpoint const& remoteEndpoint) { XRPL_ASSERT(work_, "xrpl::OverlayImpl::connect : work is set"); @@ -497,7 +497,7 @@ OverlayImpl::addActive(std::shared_ptr const& peer) } void -OverlayImpl::remove(std::shared_ptr const& slot) +OverlayImpl::remove(std::shared_ptr const& slot) { std::scoped_lock const lock(mutex_); auto const iter = peers_.find(slot); @@ -508,7 +508,7 @@ OverlayImpl::remove(std::shared_ptr const& slot) void OverlayImpl::start() { - PeerFinder::Config const config = PeerFinder::makeConfig( + peer_finder::Config const config = peer_finder::makeConfig( app_.config(), serverHandler_.setup().overlay.port(), app_.getValidationPublicKey().has_value(), @@ -541,7 +541,7 @@ OverlayImpl::start() resolver_.resolve( bootstrapIps, - [this](std::string const& name, std::vector const& addresses) { + [this](std::string const& name, std::vector const& addresses) { std::vector ips; ips.reserve(addresses.size()); for (auto const& addr : addresses) @@ -566,8 +566,8 @@ OverlayImpl::start() { resolver_.resolve( app_.config().ipsFixed, - [this](std::string const& name, std::vector const& addresses) { - std::vector ips; + [this](std::string const& name, std::vector const& addresses) { + std::vector ips; ips.reserve(addresses.size()); for (auto& addr : addresses) @@ -868,30 +868,30 @@ OverlayImpl::json() bool OverlayImpl::processCrawl(http_request_type const& req, Handoff& handoff) { - if (req.target() != "/crawl" || setup_.crawlOptions == CrawlOptions::kDisabled) + if (req.target() != "/crawl" || setup_.crawlOptions == crawl_options::kDisabled) return false; boost::beast::http::response msg; msg.version(req.version()); msg.result(boost::beast::http::status::ok); - msg.insert("Server", BuildInfo::getFullVersionString()); + msg.insert("Server", build_info::getFullVersionString()); msg.insert("Content-Type", "application/json"); msg.insert("Connection", "close"); msg.body()["version"] = json::Value(2u); - if ((setup_.crawlOptions & CrawlOptions::kOverlay) != 0u) + if ((setup_.crawlOptions & crawl_options::kOverlay) != 0u) { msg.body()["overlay"] = getOverlayInfo(); } - if ((setup_.crawlOptions & CrawlOptions::kServerInfo) != 0u) + if ((setup_.crawlOptions & crawl_options::kServerInfo) != 0u) { msg.body()["server"] = getServerInfo(); } - if ((setup_.crawlOptions & CrawlOptions::kServerCounts) != 0u) + if ((setup_.crawlOptions & crawl_options::kServerCounts) != 0u) { msg.body()["counts"] = getServerCounts(); } - if ((setup_.crawlOptions & CrawlOptions::kUnl) != 0u) + if ((setup_.crawlOptions & crawl_options::kUnl) != 0u) { msg.body()["unl"] = getUnlInfo(); } @@ -915,7 +915,7 @@ OverlayImpl::processValidatorList(http_request_type const& req, Handoff& handoff boost::beast::http::response msg; msg.version(req.version()); - msg.insert("Server", BuildInfo::getFullVersionString()); + msg.insert("Server", build_info::getFullVersionString()); msg.insert("Content-Type", "application/json"); msg.insert("Connection", "close"); @@ -972,7 +972,7 @@ OverlayImpl::processHealth(http_request_type const& req, Handoff& handoff) return false; boost::beast::http::response msg; msg.version(req.version()); - msg.insert("Server", BuildInfo::getFullVersionString()); + msg.insert("Server", build_info::getFullVersionString()); msg.insert("Content-Type", "application/json"); msg.insert("Connection", "close"); @@ -1535,7 +1535,7 @@ setupOverlay(BasicConfig const& config, beast::Journal j) { boost::system::error_code ec; setup.publicIp = boost::asio::ip::make_address(ip, ec); - if (ec || !beast::IP::isPublic(setup.publicIp)) + if (ec || !beast::ip::isPublic(setup.publicIp)) Throw("Configured public IP is invalid"); } @@ -1577,19 +1577,19 @@ setupOverlay(BasicConfig const& config, beast::Journal j) { if (get(section, Keys::kOverlay, true)) { - setup.crawlOptions |= CrawlOptions::kOverlay; + setup.crawlOptions |= crawl_options::kOverlay; } if (get(section, Keys::kServer, true)) { - setup.crawlOptions |= CrawlOptions::kServerInfo; + setup.crawlOptions |= crawl_options::kServerInfo; } if (get(section, Keys::kCounts, false)) { - setup.crawlOptions |= CrawlOptions::kServerCounts; + setup.crawlOptions |= crawl_options::kServerCounts; } if (get(section, Keys::kUnl, true)) { - setup.crawlOptions |= CrawlOptions::kUnl; + setup.crawlOptions |= crawl_options::kUnl; } } } @@ -1632,7 +1632,7 @@ makeOverlay( Application& app, Overlay::Setup const& setup, ServerHandler& serverHandler, - Resource::Manager& resourceManager, + resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_context& ioContext, BasicConfig const& config, diff --git a/src/xrpld/overlay/detail/OverlayImpl.h b/src/xrpld/overlay/detail/OverlayImpl.h index cd2c7d630b..f274bbba5a 100644 --- a/src/xrpld/overlay/detail/OverlayImpl.h +++ b/src/xrpld/overlay/detail/OverlayImpl.h @@ -109,11 +109,11 @@ private: Setup setup_; beast::Journal const journal_; ServerHandler& serverHandler_; - Resource::Manager& resourceManager_; - PeerFinder::StoreSqdb store_; - std::unique_ptr peerFinder_; + resource::Manager& resourceManager_; + peer_finder::StoreSqdb store_; + std::unique_ptr peerFinder_; TrafficCount traffic_; - hash_map, std::weak_ptr> peers_; + hash_map, std::weak_ptr> peers_; hash_map> ids_; Resolver& resolver_; std::atomic nextId_; @@ -141,7 +141,7 @@ public: Application& app, Setup setup, ServerHandler& serverHandler, - Resource::Manager& resourceManager, + resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_context& ioContext, BasicConfig const& config, @@ -157,13 +157,13 @@ public: void stop() override; - PeerFinder::Manager& + peer_finder::Manager& peerFinder() { return *peerFinder_; } - Resource::Manager& + resource::Manager& resourceManager() { return resourceManager_; @@ -182,7 +182,7 @@ public: endpoint_type remoteEndpoint) override; void - connect(beast::IP::Endpoint const& remoteEndpoint) override; + connect(beast::ip::Endpoint const& remoteEndpoint) override; int limit() override; @@ -252,7 +252,7 @@ public: addActive(std::shared_ptr const& peer); void - remove(std::shared_ptr const& slot); + remove(std::shared_ptr const& slot); /** * Called when a peer has connected successfully @@ -451,13 +451,13 @@ private: std::shared_ptr makeRedirectResponse( - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type const& request, address_type remoteAddress); static std::shared_ptr makeErrorResponse( - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type const& request, address_type remoteAddress, std::string const& msg); diff --git a/src/xrpld/overlay/detail/PeerImp.cpp b/src/xrpld/overlay/detail/PeerImp.cpp index d7a9a9e449..ca6fb180d4 100644 --- a/src/xrpld/overlay/detail/PeerImp.cpp +++ b/src/xrpld/overlay/detail/PeerImp.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -61,6 +62,7 @@ #include #include #include +#include #include #include @@ -123,11 +125,11 @@ constexpr std::chrono::seconds kPeerTimerInterval{60}; PeerImp::PeerImp( Application& app, id_t id, - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type&& request, PublicKey const& publicKey, ProtocolVersion protocol, - Resource::Consumer consumer, + resource::Consumer consumer, std::unique_ptr&& streamPtr, OverlayImpl& overlay) : Child(overlay) @@ -155,7 +157,7 @@ PeerImp::PeerImp( , creationTime_(clock_type::now()) , squelch_(app_.getJournal("Squelch")) , usage_(consumer) - , fee_{.fee = Resource::kFeeTrivialPeer, .context = ""} + , fee_{.fee = resource::kFeeTrivialPeer, .context = ""} , slot_(slot) , request_(std::move(request)) , headers_(request_) @@ -301,7 +303,7 @@ PeerImp::send(std::shared_ptr const& m) auto sendqSize = self->sendQueue_.size(); - if (sendqSize < Tuning::kTargetSendQueue) + if (sendqSize < tuning::kTargetSendQueue) { // To detect a peer that does not read from their // side of the connection, we expect a peer to have @@ -310,7 +312,7 @@ PeerImp::send(std::shared_ptr const& m) } else if ( auto sink = self->journal_.debug(); - sink && (sendqSize % Tuning::kSendQueueLogFreq) == 0) + sink && (sendqSize % tuning::kSendQueueLogFreq) == 0) { std::string const n = self->name(); sink << n << " sendq: " << sendqSize; @@ -372,10 +374,10 @@ PeerImp::removeTxQueue(uint256 const& hash) } void -PeerImp::charge(Resource::Charge const& fee, std::string const& context) +PeerImp::charge(resource::Charge const& fee, std::string const& context) { dispatch(strand_, [self = shared_from_this(), fee, context]() { - if ((self->usage_.charge(fee, context) == Resource::Disposition::Drop) && + if ((self->usage_.charge(fee, context) == resource::Disposition::Drop) && self->usage_.disconnect(self->pJournal_)) { // Idempotent: only the first worker to observe Drop counts the @@ -543,6 +545,8 @@ PeerImp::supportsFeature(ProtocolFeature f) const return protocol_ >= makeProtocol(2, 1); case ProtocolFeature::ValidatorList2Propagation: return protocol_ >= makeProtocol(2, 2); + case ProtocolFeature::LedgerNodeDepth: + return protocol_ >= makeProtocol(2, 3); case ProtocolFeature::LedgerReplay: return ledgerReplayEnabled_; } @@ -714,7 +718,7 @@ PeerImp::onTimer(error_code const& ec) return; } - if (largeSendq_++ >= Tuning::kSendqIntervals) + if (largeSendq_++ >= tuning::kSendqIntervals) { fail("Large send queue"); return; @@ -944,7 +948,7 @@ PeerImp::onReadMessage(error_code ec, std::size_t bytesTransferred) readBuffer_.commit(bytesTransferred); - auto hint = Tuning::kReadBufferBytes; + auto hint = tuning::kReadBufferBytes; while (readBuffer_.size() > 0) { @@ -976,7 +980,7 @@ PeerImp::onReadMessage(error_code ec, std::size_t bytesTransferred) // Timeout on writes only stream_.async_read_some( - readBuffer_.prepare(std::max(Tuning::kReadBufferBytes, hint)), + readBuffer_.prepare(std::max(tuning::kReadBufferBytes, hint)), bind_executor( strand_, [self = shared_from_this()](error_code const& ec, std::size_t bytesTransferred) { @@ -1052,7 +1056,7 @@ PeerImp::onMessageBegin( { auto const name = protocolMessageName(type); loadEvent_ = app_.getJobQueue().makeLoadEvent(JtPeer, name); - fee_ = {.fee = Resource::kFeeTrivialPeer, .context = name}; + fee_ = {.fee = resource::kFeeTrivialPeer, .context = name}; auto const category = TrafficCount::categorize(*m, static_cast(type), true); @@ -1096,12 +1100,12 @@ PeerImp::onMessage(std::shared_ptr const& m) if (s == 0) { - fee_.update(Resource::kFeeUselessData, "empty"); + fee_.update(resource::kFeeUselessData, "empty"); return; } if (s > 100) - fee_.update(Resource::kFeeModerateBurdenPeer, "oversize"); + fee_.update(resource::kFeeModerateBurdenPeer, "oversize"); app_.getJobQueue().addJob(JtManifest, "RcvManifests", [this, that = shared_from_this(), m]() { overlay_.onManifests(m, that); @@ -1114,7 +1118,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (m->type() == protocol::TMPing::ptPING) { // We have received a ping request, reply with a pong - fee_.update(Resource::kFeeModerateBurdenPeer, "ping request"); + fee_.update(resource::kFeeModerateBurdenPeer, "ping request"); m->set_type(protocol::TMPing::ptPONG); send(std::make_shared(*m, protocol::mtPING)); return; @@ -1155,7 +1159,7 @@ PeerImp::onMessage(std::shared_ptr const& m) // VFALCO NOTE I think we should drop the peer immediately if (!cluster()) { - fee_.update(Resource::kFeeUselessData, "unknown cluster"); + fee_.update(resource::kFeeUselessData, "unknown cluster"); return; } @@ -1182,15 +1186,15 @@ PeerImp::onMessage(std::shared_ptr const& m) int const loadSources = m->loadsources().size(); if (loadSources != 0) { - Resource::Gossip gossip; + resource::Gossip gossip; gossip.items.reserve(loadSources); for (int i = 0; i < m->loadsources().size(); ++i) { protocol::TMLoadSource const& node = m->loadsources(i); - Resource::Gossip::Item item; - item.address = beast::IP::Endpoint::fromString(node.name()); + resource::Gossip::Item item; + item.address = beast::ip::Endpoint::fromString(node.name()); item.balance = node.cost(); - if (item.address != beast::IP::Endpoint()) + if (item.address != beast::ip::Endpoint()) gossip.items.push_back(item); } overlay_.resourceManager().importConsumers(name(), gossip); @@ -1230,17 +1234,17 @@ PeerImp::onMessage(std::shared_ptr const& m) // implication for the protocol. if (m->endpoints_v2().size() >= 1024) { - fee_.update(Resource::kFeeUselessData, "endpoints too large"); + fee_.update(resource::kFeeUselessData, "endpoints too large"); return; } - std::vector endpoints; + std::vector endpoints; endpoints.reserve(m->endpoints_v2().size()); auto malformed = 0; for (auto const& tm : m->endpoints_v2()) { - auto result = beast::IP::Endpoint::fromStringChecked(tm.endpoint()); + auto result = beast::ip::Endpoint::fromStringChecked(tm.endpoint()); if (!result) { @@ -1252,7 +1256,7 @@ PeerImp::onMessage(std::shared_ptr const& m) // If hops == 0, this Endpoint describes the peer we are connected // to -- in that case, we take the remote address seen on the - // socket and store that in the IP::Endpoint. If this is the first + // socket and store that in the ip::Endpoint. If this is the first // time, then we'll verify that their listener can receive incoming // by performing a connectivity test. if hops > 0, then we just // take the address/port we were given @@ -1267,7 +1271,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (malformed > 0) { fee_.update( - Resource::kFeeInvalidData * malformed, + resource::kFeeInvalidData * malformed, std::to_string(malformed) + " malformed endpoints"); } @@ -1328,7 +1332,7 @@ PeerImp::handleTransaction( { JLOG(pJournal_.warn()) << "Ignoring Network relayed Tx containing " "tfInnerBatchTxn (handleTransaction)."; - fee_.update(Resource::kFeeModerateBurdenPeer, "inner batch txn"); + fee_.update(resource::kFeeModerateBurdenPeer, "inner batch txn"); return; } // LCOV_EXCL_STOP @@ -1341,7 +1345,7 @@ PeerImp::handleTransaction( // we have seen this transaction recently if (any(flags & HashRouterFlags::BAD)) { - fee_.update(Resource::kFeeUselessData, "known bad"); + fee_.update(resource::kFeeUselessData, "known bad"); JLOG(pJournal_.debug()) << "Ignoring known bad tx " << txID; } @@ -1415,7 +1419,7 @@ void PeerImp::onMessage(std::shared_ptr const& m) { auto badData = [&](std::string const& msg) { - fee_.update(Resource::kFeeInvalidData, "get_ledger " + msg); + fee_.update(resource::kFeeInvalidData, "get_ledger " + msg); JLOG(pJournal_.warn()) << "TMGetLedger: " << msg; }; auto const itype{m->itype()}; @@ -1477,23 +1481,12 @@ PeerImp::onMessage(std::shared_ptr const& m) } } - // Verify ledger node IDs - if (itype != protocol::liBASE) + // Verify ledger node counts. Full parsing of the node IDs is deferred to the job, so the I/O + // thread is not burdened with SHAMapNodeID deserialization for every TMGetLedger message. + if (itype != protocol::liBASE && m->nodeids_size() <= 0) { - if (m->nodeids_size() <= 0) - { - badData("Invalid ledger node IDs"); - return; - } - - for (auto const& nodeId : m->nodeids()) - { - if (deserializeSHAMapNodeID(nodeId) == std::nullopt) - { - badData("Invalid SHAMap node ID"); - return; - } - } + badData("Invalid ledger node IDs"); + return; } // Verify query type @@ -1506,18 +1499,64 @@ PeerImp::onMessage(std::shared_ptr const& m) // Verify query depth if (m->has_querydepth()) { - if (m->querydepth() > Tuning::kMaxQueryDepth || itype == protocol::liBASE) + if (m->querydepth() > tuning::kMaxQueryDepth || itype == protocol::liBASE) { badData("Invalid query depth"); return; } } - // Queue a job to process the request + // Queue a job to process the request. std::weak_ptr const weak = shared_from_this(); - app_.getJobQueue().addJob(JtLedgerReq, "RcvGetLedger", [weak, m]() { - if (auto peer = weak.lock()) - peer->processLedgerRequest(m); + app_.getJobQueue().addJob(JtLedgerReq, "RcvGetLedger", [weak, m, itype]() { + auto peer = weak.lock(); + if (!peer) + return; + + std::vector nodeIDs; + bool tooManyNodeIds = false; + if (itype != protocol::liBASE) + { + nodeIDs.reserve(std::min(m->nodeids_size(), tuning::kSoftMaxReplyNodes)); + for (auto const& nodeId : m->nodeids()) + { + if (nodeIDs.size() >= tuning::kSoftMaxReplyNodes) + { + // The peer requested too many node IDs. Continue processing the received node + // IDs up to the limit. If the request is legitimate then at least they will get + // a response and won't have to resend these nodes in their next request. + tooManyNodeIds = true; + break; + } + auto parsed = deserializeSHAMapNodeID(nodeId); + if (!parsed) + { + peer->charge(resource::kFeeInvalidData, "TMGetLedger: Invalid node ID"); + return; + } + nodeIDs.push_back(std::move(*parsed)); + } + } + + // These are two distinct infractions and are charged independently: requesting too many + // node IDs is charged even for a relay response, while the base "get ledger request" charge + // below is skipped for relay responses. + if (tooManyNodeIds) + { + peer->charge(resource::kFeeModerateBurdenPeer, "TMGetLedger: too many node IDs"); + + // Truncate the request to what was actually parsed and charged for, so that if this + // request ends up being relayed to another peer, we don't forward the oversized list. + m->mutable_nodeids()->DeleteSubrange( + static_cast(nodeIDs.size()), + m->nodeids_size() - static_cast(nodeIDs.size())); + } + if (!m->has_requestcookie()) + { + peer->charge(resource::kFeeModerateBurdenPeer, "TMGetLedger: get ledger request"); + } + + peer->processLedgerRequest(m, std::move(nodeIDs)); }); } @@ -1527,11 +1566,11 @@ PeerImp::onMessage(std::shared_ptr const& m) JLOG(pJournal_.trace()) << "onMessage, TMProofPathRequest"; if (!ledgerReplayEnabled_) { - fee_.update(Resource::kFeeMalformedRequest, "proof_path_request disabled"); + fee_.update(resource::kFeeMalformedRequest, "proof_path_request disabled"); return; } - fee_.update(Resource::kFeeModerateBurdenPeer, "received a proof path request"); + fee_.update(resource::kFeeModerateBurdenPeer, "received a proof path request"); std::weak_ptr const weak = shared_from_this(); app_.getJobQueue().addJob(JtReplayReq, "RcvProofPReq", [weak, m]() { if (auto peer = weak.lock()) @@ -1541,11 +1580,11 @@ PeerImp::onMessage(std::shared_ptr const& m) { if (reply.error() == protocol::TMReplyError::reBAD_REQUEST) { - peer->charge(Resource::kFeeMalformedRequest, "proof_path_request"); + peer->charge(resource::kFeeMalformedRequest, "proof_path_request"); } else { - peer->charge(Resource::kFeeRequestNoReply, "proof_path_request"); + peer->charge(resource::kFeeRequestNoReply, "proof_path_request"); } } else @@ -1561,13 +1600,13 @@ PeerImp::onMessage(std::shared_ptr const& m) { if (!ledgerReplayEnabled_) { - fee_.update(Resource::kFeeMalformedRequest, "proof_path_response disabled"); + fee_.update(resource::kFeeMalformedRequest, "proof_path_response disabled"); return; } if (!ledgerReplayMsgHandler_.processProofPathResponse(m)) { - fee_.update(Resource::kFeeInvalidData, "proof_path_response"); + fee_.update(resource::kFeeInvalidData, "proof_path_response"); } } @@ -1577,11 +1616,11 @@ PeerImp::onMessage(std::shared_ptr const& m) JLOG(pJournal_.trace()) << "onMessage, TMReplayDeltaRequest"; if (!ledgerReplayEnabled_) { - fee_.update(Resource::kFeeMalformedRequest, "replay_delta_request disabled"); + fee_.update(resource::kFeeMalformedRequest, "replay_delta_request disabled"); return; } - fee_.fee = Resource::kFeeModerateBurdenPeer; + fee_.fee = resource::kFeeModerateBurdenPeer; std::weak_ptr const weak = shared_from_this(); app_.getJobQueue().addJob(JtReplayReq, "RcvReplDReq", [weak, m]() { if (auto peer = weak.lock()) @@ -1591,11 +1630,11 @@ PeerImp::onMessage(std::shared_ptr const& m) { if (reply.error() == protocol::TMReplyError::reBAD_REQUEST) { - peer->charge(Resource::kFeeMalformedRequest, "replay_delta_request"); + peer->charge(resource::kFeeMalformedRequest, "replay_delta_request"); } else { - peer->charge(Resource::kFeeRequestNoReply, "replay_delta_request"); + peer->charge(resource::kFeeRequestNoReply, "replay_delta_request"); } } else @@ -1611,13 +1650,13 @@ PeerImp::onMessage(std::shared_ptr const& m) { if (!ledgerReplayEnabled_) { - fee_.update(Resource::kFeeMalformedRequest, "replay_delta_response disabled"); + fee_.update(resource::kFeeMalformedRequest, "replay_delta_response disabled"); return; } if (!ledgerReplayMsgHandler_.processReplayDeltaResponse(m)) { - fee_.update(Resource::kFeeInvalidData, "replay_delta_response"); + fee_.update(resource::kFeeInvalidData, "replay_delta_response"); } } @@ -1625,7 +1664,7 @@ void PeerImp::onMessage(std::shared_ptr const& m) { auto badData = [&](std::string const& msg) { - fee_.update(Resource::kFeeInvalidData, msg); + fee_.update(resource::kFeeInvalidData, msg); JLOG(pJournal_.warn()) << "TMLedgerData: " << msg; }; @@ -1676,18 +1715,125 @@ PeerImp::onMessage(std::shared_ptr const& m) } // Verify ledger nodes. - if (m->nodes_size() <= 0 || m->nodes_size() > Tuning::kHardMaxReplyNodes) + if (m->nodes_size() <= 0 || m->nodes_size() > tuning::kHardMaxReplyNodes) { badData("Invalid Ledger/TXset nodes " + std::to_string(m->nodes_size())); return; } - // If there is a request cookie, attempt to relay the message + // If there is a request cookie, attempt to relay the message. if (m->has_requestcookie()) { if (auto peer = overlay_.findPeerByShortID(m->requestcookie())) { m->clear_requestcookie(); + + // If the original requester doesn't support the new depth-based format, rewrite any + // nodes that use it back to the legacy nodeid format before relaying. Once all nodes + // have upgraded, the old protocol version and this code can be removed. Make sure that + // the format of the nodes is consistent - either all use the legacy format or the new + // format, unless it is liBASE data in which case none of these fields should be set. + auto const peerSupportsNodeDepth = + peer->supportsFeature(ProtocolFeature::LedgerNodeDepth); + enum class MessageType { Unknown, Base, Legacy, Depth }; + MessageType messageType = MessageType::Unknown; + for (int i = 0; i < m->nodes_size(); ++i) + { + auto* ledgerNode = m->mutable_nodes(i); + + // All nodes should have non-empty data. The field is required so we don't need to + // check for presence first. + if (ledgerNode->nodedata().empty()) + { + badData( + "Received node with empty data while relaying ledger data for " + + to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " + + std::to_string(peer->id())); + return; + } + + MessageType msgType = MessageType::Unknown; + if (m->type() == protocol::liBASE) + { + if (ledgerNode->has_nodeid() || ledgerNode->has_id() || ledgerNode->has_depth()) + { + badData( + "Received liBASE message with node reference while relaying ledger " + "data for " + + to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " + + std::to_string(peer->id())); + return; + } + msgType = MessageType::Base; + } + else + { + msgType = ledgerNode->has_nodeid() ? MessageType::Legacy : MessageType::Depth; + } + if (messageType != MessageType::Unknown && messageType != msgType) + { + badData( + "Received mixed mode message while relaying ledger data for " + + to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " + + std::to_string(peer->id())); + return; + } + messageType = msgType; + + if (peerSupportsNodeDepth || msgType != MessageType::Depth) + continue; + + SOMETIMES( + !peerSupportsNodeDepth, + "xrpl::PeerImp : relaying depth-format ledger data to pre-2.3 peer"); + switch (ledgerNode->reference_case()) + { + case protocol::TMLedgerNode::kId: { + // We can directly copy the `id` field, because it uses the same wire format + // as the legacy `nodeid` field. + REACHABLE("xrpl::PeerImp : relay downgrade id to nodeid"); + ledgerNode->set_nodeid(ledgerNode->id()); + ledgerNode->clear_id(); + break; + } + case protocol::TMLedgerNode::kDepth: { + // We need to regenerate the node ID from the node data and depth. + auto treeNode = getTreeNode(ledgerNode->nodedata()); + if (!treeNode) + { + badData( + "Unable to get tree node while relaying ledger data for " + + to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " + + std::to_string(peer->id())); + return; + } + + auto const nodeID = getSHAMapNodeID(*ledgerNode, *treeNode); + if (!nodeID) + { + badData( + "Unable to get node ID while relaying ledger data for " + + to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " + + std::to_string(peer->id())); + return; + } + + REACHABLE("xrpl::PeerImp : relay downgrade depth to nodeid"); + ledgerNode->set_nodeid(nodeID->getRawString()); + ledgerNode->clear_depth(); + break; + } + default: { + SOMETIMES(true, "xrpl::PeerImp : relay node has empty reference"); + badData( + "Empty node reference while relaying ledger data for " + + to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " + + std::to_string(peer->id())); + return; + } + } + } + peer->send(std::make_shared(*m, protocol::mtLEDGER_DATA)); } else @@ -1729,14 +1875,14 @@ PeerImp::onMessage(std::shared_ptr const& m) (publicKeyType(makeSlice(set.nodepubkey())) != KeyType::Secp256k1)) { JLOG(pJournal_.warn()) << "Proposal: malformed"; - fee_.update(Resource::kFeeInvalidSignature, " signature can't be longer than 72 bytes"); + fee_.update(resource::kFeeInvalidSignature, " signature can't be longer than 72 bytes"); return; } if (!stringIsUInt256Sized(set.currenttxhash()) || !stringIsUInt256Sized(set.previousledger())) { JLOG(pJournal_.warn()) << "Proposal: malformed"; - fee_.update(Resource::kFeeMalformedRequest, "bad hashes"); + fee_.update(resource::kFeeMalformedRequest, "bad hashes"); return; } @@ -2018,13 +2164,13 @@ PeerImp::checkTracking(std::uint32_t seq1, std::uint32_t seq2) { std::uint32_t const diff = std::max(seq1, seq2) - std::min(seq1, seq2); - if (diff < Tuning::kConvergedLedgerLimit) + if (diff < tuning::kConvergedLedgerLimit) { // The peer's ledger sequence is close to the validation's tracking_ = Tracking::Converged; } - if ((diff > Tuning::kDivergedLedgerLimit) && (tracking_.load() != Tracking::Diverged)) + if ((diff > tuning::kDivergedLedgerLimit) && (tracking_.load() != Tracking::Diverged)) { // The peer's ledger sequence is way off the validation's std::scoped_lock const sl(recentLock_); @@ -2039,7 +2185,7 @@ PeerImp::onMessage(std::shared_ptr const& m) { if (!stringIsUInt256Sized(m->hash())) { - fee_.update(Resource::kFeeMalformedRequest, "bad hash"); + fee_.update(resource::kFeeMalformedRequest, "bad hash"); return; } @@ -2051,7 +2197,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (std::ranges::find(recentTxSets_, hash) != recentTxSets_.end()) { - fee_.update(Resource::kFeeUselessData, "duplicate (tsHAVE)"); + fee_.update(resource::kFeeUselessData, "duplicate (tsHAVE)"); return; } @@ -2072,7 +2218,7 @@ PeerImp::onValidatorListMessage( { JLOG(pJournal_.warn()) << "Ignored malformed " << messageType; // This shouldn't ever happen with a well-behaved peer - fee_.update(Resource::kFeeHeavyBurdenPeer, "no blobs"); + fee_.update(resource::kFeeHeavyBurdenPeer, "no blobs"); return; } @@ -2086,7 +2232,7 @@ PeerImp::onValidatorListMessage( // Charging this fee here won't hurt the peer in the normal // course of operation (ie. refresh every 5 minutes), but // will add up if the peer is misbehaving. - fee_.update(Resource::kFeeUselessData, "duplicate"); + fee_.update(resource::kFeeUselessData, "duplicate"); return; } @@ -2177,27 +2323,27 @@ PeerImp::onValidatorListMessage( // Charging this fee here won't hurt the peer in the normal // course of operation (ie. refresh every 5 minutes), but // will add up if the peer is misbehaving. - fee_.update(Resource::kFeeUselessData, " duplicate (same_sequence or known_sequence)"); + fee_.update(resource::kFeeUselessData, " duplicate (same_sequence or known_sequence)"); break; case ListDisposition::Stale: // There are very few good reasons for a peer to send an // old list, particularly more than once. - fee_.update(Resource::kFeeInvalidData, "expired"); + fee_.update(resource::kFeeInvalidData, "expired"); break; case ListDisposition::Untrusted: // Charging this fee here won't hurt the peer in the normal // course of operation (ie. refresh every 5 minutes), but // will add up if the peer is misbehaving. - fee_.update(Resource::kFeeUselessData, "untrusted"); + fee_.update(resource::kFeeUselessData, "untrusted"); break; case ListDisposition::Invalid: // This shouldn't ever happen with a well-behaved peer - fee_.update(Resource::kFeeInvalidSignature, "invalid list disposition"); + fee_.update(resource::kFeeInvalidSignature, "invalid list disposition"); break; case ListDisposition::UnsupportedVersion: // During a version transition, this may be legitimate. // If it happens frequently, that's probably bad. - fee_.update(Resource::kFeeInvalidData, "version"); + fee_.update(resource::kFeeInvalidData, "version"); break; // LCOV_EXCL_START default: @@ -2265,7 +2411,7 @@ PeerImp::onMessage(std::shared_ptr const& m) JLOG(pJournal_.debug()) << "ValidatorList: received validator list from peer using " << "protocol version " << to_string(protocol_) << " which shouldn't support this feature."; - fee_.update(Resource::kFeeUselessData, "unsupported peer"); + fee_.update(resource::kFeeUselessData, "unsupported peer"); return; } onValidatorListMessage( @@ -2275,7 +2421,7 @@ PeerImp::onMessage(std::shared_ptr const& m) { JLOG(pJournal_.warn()) << "ValidatorList: Exception, " << e.what(); using namespace std::string_literals; - fee_.update(Resource::kFeeInvalidData, e.what()); + fee_.update(resource::kFeeInvalidData, e.what()); } } @@ -2289,7 +2435,7 @@ PeerImp::onMessage(std::shared_ptr const& m JLOG(pJournal_.debug()) << "ValidatorListCollection: received validator list from peer " << "using protocol version " << to_string(protocol_) << " which shouldn't support this feature."; - fee_.update(Resource::kFeeUselessData, "unsupported peer"); + fee_.update(resource::kFeeUselessData, "unsupported peer"); return; } if (m->version() < 2) @@ -2298,7 +2444,7 @@ PeerImp::onMessage(std::shared_ptr const& m << "ValidatorListCollection: received invalid validator list " "version " << m->version() << " from peer using protocol version " << to_string(protocol_); - fee_.update(Resource::kFeeInvalidData, "wrong version"); + fee_.update(resource::kFeeInvalidData, "wrong version"); return; } onValidatorListMessage( @@ -2308,7 +2454,7 @@ PeerImp::onMessage(std::shared_ptr const& m { JLOG(pJournal_.warn()) << "ValidatorListCollection: Exception, " << e.what(); using namespace std::string_literals; - fee_.update(Resource::kFeeInvalidData, e.what()); + fee_.update(resource::kFeeInvalidData, e.what()); } } @@ -2318,7 +2464,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (m->validation().size() < 50) { JLOG(pJournal_.warn()) << "Validation: Too small"; - fee_.update(Resource::kFeeMalformedRequest, "too small"); + fee_.update(resource::kFeeMalformedRequest, "too small"); return; } @@ -2345,7 +2491,7 @@ PeerImp::onMessage(std::shared_ptr const& m) val->getSeenTime())) { JLOG(pJournal_.trace()) << "Validation: Not current"; - fee_.update(Resource::kFeeUselessData, "not current"); + fee_.update(resource::kFeeUselessData, "not current"); return; } @@ -2414,7 +2560,7 @@ PeerImp::onMessage(std::shared_ptr const& m) { JLOG(pJournal_.warn()) << "Exception processing validation: " << e.what(); using namespace std::string_literals; - fee_.update(Resource::kFeeMalformedRequest, e.what()); + fee_.update(resource::kFeeMalformedRequest, e.what()); } } @@ -2429,7 +2575,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (packet.query()) { // this is a query - if (sendQueue_.size() >= Tuning::kDropSendQueue) + if (sendQueue_.size() >= tuning::kDropSendQueue) { JLOG(pJournal_.debug()) << "GetObject: Large send queue"; return; @@ -2446,7 +2592,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (!txReduceRelayEnabled()) { JLOG(pJournal_.error()) << "TMGetObjectByHash: tx reduce-relay is disabled"; - fee_.update(Resource::kFeeMalformedRequest, "disabled"); + fee_.update(resource::kFeeMalformedRequest, "disabled"); return; } @@ -2463,19 +2609,19 @@ PeerImp::onMessage(std::shared_ptr const& m) if (!stringIsUInt256Sized(packet.ledgerhash())) { JLOG(pJournal_.debug()) << "GetObj: malformed ledgerhash from peer " << id_; - fee_.update(Resource::kFeeMalformedRequest, "get object ledger hash"); + fee_.update(resource::kFeeMalformedRequest, "get object ledger hash"); return; } } // Reject oversized requests before touching the NodeStore. // The legitimate upper bound (InboundLedger::getNeededHashes()) // is 8 hashes; anything beyond kHardMaxReplyNodes is non-conforming. - if (packet.objects_size() > Tuning::kHardMaxReplyNodes) + if (packet.objects_size() > tuning::kHardMaxReplyNodes) { JLOG(pJournal_.warn()) << "GetObj: oversized request from peer " << id_ << " (" << packet.objects_size() - << " > " << Tuning::kHardMaxReplyNodes << ")"; - fee_.update(Resource::kFeeInvalidData, "oversized get object request"); + << " > " << tuning::kHardMaxReplyNodes << ")"; + fee_.update(resource::kFeeInvalidData, "oversized get object request"); return; } @@ -2497,7 +2643,7 @@ PeerImp::onMessage(std::shared_ptr const& m) // back through the resource model so a misbehaving peer // is still accountable rather than silently dropped. JLOG(peer->pJournal_.warn()) << "GetObj: handler threw: " << e.what(); - peer->charge(Resource::kFeeRequestNoReply, "get object handler exception"); + peer->charge(resource::kFeeRequestNoReply, "get object handler exception"); } }); if (!queued) @@ -2514,7 +2660,7 @@ PeerImp::onMessage(std::shared_ptr const& m) // uncharged DoS window. Charge the base burden up-front (after // a successful enqueue); the per-lookup differential is added // in the worker. - fee_.update(Resource::kFeeModerateBurdenPeer, "received a get object by hash request"); + fee_.update(resource::kFeeModerateBurdenPeer, "received a get object by hash request"); } else { @@ -2593,7 +2739,7 @@ PeerImp::processGetObjectByHash(std::shared_ptr con // a peer cannot drive unbounded NodeStore lookups by sending // non-existent hashes. int const requested = packet.objects_size(); - int const iterLimit = std::min(requested, Tuning::kHardMaxReplyNodes); + int const iterLimit = std::min(requested, tuning::kHardMaxReplyNodes); for (int i = 0; i < iterLimit; ++i) { @@ -2624,7 +2770,7 @@ PeerImp::processGetObjectByHash(std::shared_ptr con // JobQueue worker thread. charge( // We pass `requested` directly here, instead of actual lookups done. Which could be - // std::min(packet.objects_size(), static_cast(Tuning::kHardMaxReplyNodes)); + // std::min(packet.objects_size(), static_cast(tuning::kHardMaxReplyNodes)); // Because we want to charge as per the request size, to discourage large requests. computeGetObjectByHashFee(requested, reply.objects_size()), "processed get object by hash request"); @@ -2639,7 +2785,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (!txReduceRelayEnabled()) { JLOG(pJournal_.error()) << "TMHaveTransactions: tx reduce-relay is disabled"; - fee_.update(Resource::kFeeMalformedRequest, "disabled"); + fee_.update(resource::kFeeMalformedRequest, "disabled"); return; } @@ -2664,7 +2810,7 @@ PeerImp::handleHaveTransactions(std::shared_ptr co if (!stringIsUInt256Sized(m->hashes(i))) { JLOG(pJournal_.error()) << "TMHaveTransactions with invalid hash size"; - fee_.update(Resource::kFeeMalformedRequest, "hash size"); + fee_.update(resource::kFeeMalformedRequest, "hash size"); return; } @@ -2702,7 +2848,7 @@ PeerImp::onMessage(std::shared_ptr const& m) if (!txReduceRelayEnabled()) { JLOG(pJournal_.error()) << "TMTransactions: tx reduce-relay is disabled"; - fee_.update(Resource::kFeeMalformedRequest, "disabled"); + fee_.update(resource::kFeeMalformedRequest, "disabled"); return; } @@ -2726,14 +2872,14 @@ PeerImp::onMessage(std::shared_ptr const& m) dispatch(strand_, [self = shared_from_this(), m]() { if (!m->has_validatorpubkey()) { - self->fee_.update(Resource::kFeeInvalidData, "squelch no pubkey"); + self->fee_.update(resource::kFeeInvalidData, "squelch no pubkey"); return; } auto validator = m->validatorpubkey(); auto const slice{makeSlice(validator)}; if (!publicKeyType(slice)) { - self->fee_.update(Resource::kFeeInvalidData, "squelch bad pubkey"); + self->fee_.update(resource::kFeeInvalidData, "squelch bad pubkey"); return; } PublicKey const key(slice); @@ -2753,7 +2899,7 @@ PeerImp::onMessage(std::shared_ptr const& m) } else if (!self->squelch_.addSquelch(key, std::chrono::seconds{duration})) { - self->fee_.update(Resource::kFeeInvalidData, "squelch duration"); + self->fee_.update(resource::kFeeInvalidData, "squelch duration"); } JLOG(self->pJournal_.debug()) @@ -2793,11 +2939,11 @@ PeerImp::doFetchPack(std::shared_ptr const& packet) if (!stringIsUInt256Sized(packet->ledgerhash())) { JLOG(pJournal_.warn()) << "FetchPack hash size malformed"; - fee_.update(Resource::kFeeMalformedRequest, "hash size"); + fee_.update(resource::kFeeMalformedRequest, "hash size"); return; } - fee_.fee = Resource::kFeeHeavyBurdenPeer; + fee_.fee = resource::kFeeHeavyBurdenPeer; uint256 const hash = uint256::fromRaw(packet->ledgerhash()); @@ -2820,7 +2966,7 @@ PeerImp::doTransactions(std::shared_ptr const& pack if (packet->objects_size() > reduce_relay::kMaxTxQueueSize) { JLOG(pJournal_.error()) << "doTransactions, invalid number of hashes"; - fee_.update(Resource::kFeeMalformedRequest, "too big"); + fee_.update(resource::kFeeMalformedRequest, "too big"); return; } @@ -2830,7 +2976,7 @@ PeerImp::doTransactions(std::shared_ptr const& pack if (!stringIsUInt256Sized(obj.hash())) { - fee_.update(Resource::kFeeMalformedRequest, "hash size"); + fee_.update(resource::kFeeMalformedRequest, "hash size"); return; } @@ -2842,7 +2988,7 @@ PeerImp::doTransactions(std::shared_ptr const& pack { JLOG(pJournal_.error()) << "doTransactions, transaction not found " << Slice(hash.data(), hash.size()); - fee_.update(Resource::kFeeMalformedRequest, "tx not found"); + fee_.update(resource::kFeeMalformedRequest, "tx not found"); return; } @@ -2893,7 +3039,7 @@ PeerImp::checkTransaction( { JLOG(pJournal_.warn()) << "Ignoring Network relayed Tx containing " "tfInnerBatchTxn (checkSignature)."; - charge(Resource::kFeeModerateBurdenPeer, "inner batch txn"); + charge(resource::kFeeModerateBurdenPeer, "inner batch txn"); return; } // LCOV_EXCL_STOP @@ -2905,7 +3051,7 @@ PeerImp::checkTransaction( JLOG(pJournal_.info()) << "Marking transaction " << stx->getTransactionID() << "as BAD because it's expired"; app_.getHashRouter().setFlags(stx->getTransactionID(), HashRouterFlags::BAD); - charge(Resource::kFeeUselessData, "expired tx"); + charge(resource::kFeeUselessData, "expired tx"); return; } @@ -2936,7 +3082,7 @@ PeerImp::checkTransaction( if (!batch) { JLOG(pJournal_.debug()) << "Charging for pseudo-transaction tx " << tx->getID(); - charge(Resource::kFeeUselessData, "pseudo tx"); + charge(resource::kFeeUselessData, "pseudo tx"); } return; @@ -2958,7 +3104,7 @@ PeerImp::checkTransaction( // Probably not necessary to set HashRouterFlags::BAD, but // doesn't hurt. app_.getHashRouter().setFlags(stx->getTransactionID(), HashRouterFlags::BAD); - charge(Resource::kFeeInvalidSignature, "check transaction signature failure"); + charge(resource::kFeeInvalidSignature, "check transaction signature failure"); return; } } @@ -2977,7 +3123,7 @@ PeerImp::checkTransaction( JLOG(pJournal_.debug()) << "Exception checking transaction: " << reason; } app_.getHashRouter().setFlags(stx->getTransactionID(), HashRouterFlags::BAD); - charge(Resource::kFeeInvalidSignature, "tx (impossible)"); + charge(resource::kFeeInvalidSignature, "tx (impossible)"); return; } @@ -2989,7 +3135,7 @@ PeerImp::checkTransaction( JLOG(pJournal_.warn()) << "Exception in " << __func__ << ": " << ex.what(); app_.getHashRouter().setFlags(stx->getTransactionID(), HashRouterFlags::BAD); using namespace std::string_literals; - charge(Resource::kFeeInvalidData, "tx "s + ex.what()); + charge(resource::kFeeInvalidData, "tx "s + ex.what()); } } @@ -3008,7 +3154,7 @@ PeerImp::checkPropose( { std::string const desc{"Proposal fails sig check"}; JLOG(pJournal_.warn()) << desc; - charge(Resource::kFeeInvalidSignature, desc); + charge(resource::kFeeInvalidSignature, desc); return; } @@ -3052,7 +3198,7 @@ PeerImp::checkValidation( { std::string const desc{"Validation forwarded by peer is invalid"}; JLOG(pJournal_.debug()) << desc; - charge(Resource::kFeeInvalidSignature, desc); + charge(resource::kFeeInvalidSignature, desc); return; } @@ -3077,7 +3223,7 @@ PeerImp::checkValidation( { JLOG(pJournal_.trace()) << "Exception processing validation: " << ex.what(); using namespace std::string_literals; - charge(Resource::kFeeMalformedRequest, "validation "s + ex.what()); + charge(resource::kFeeMalformedRequest, "validation "s + ex.what()); } } @@ -3234,7 +3380,7 @@ PeerImp::getLedger(std::shared_ptr const& m) { // Do not resource charge a peer responding to a relay if (!m->has_requestcookie()) - charge(Resource::kFeeMalformedRequest, "get_ledger ledgerSeq"); + charge(resource::kFeeMalformedRequest, "get_ledger ledgerSeq"); ledger.reset(); JLOG(pJournal_.warn()) << "getLedger: Invalid ledger sequence " << ledgerSeq; @@ -3287,12 +3433,10 @@ PeerImp::getTxSet(std::shared_ptr const& m) const } void -PeerImp::processLedgerRequest(std::shared_ptr const& m) +PeerImp::processLedgerRequest( + std::shared_ptr const& m, + std::vector nodeIDs) { - // Do not resource charge a peer responding to a relay - if (!m->has_requestcookie()) - charge(Resource::kFeeModerateBurdenPeer, "received a get ledger request"); - std::shared_ptr ledger; std::shared_ptr sharedMap; SHAMap const* map{nullptr}; @@ -3318,7 +3462,7 @@ PeerImp::processLedgerRequest(std::shared_ptr const& m) } else { - if (sendQueue_.size() >= Tuning::kDropSendQueue) + if (sendQueue_.size() >= tuning::kDropSendQueue) { JLOG(pJournal_.debug()) << "processLedgerRequest: Large send queue"; return; @@ -3372,37 +3516,54 @@ PeerImp::processLedgerRequest(std::shared_ptr const& m) } // Add requested node data to reply - if (m->nodeids_size() > 0) + if (!nodeIDs.empty()) { std::uint32_t const defaultDepth = isHighLatency() ? 2 : 1; auto const queryDepth{m->has_querydepth() ? m->querydepth() : defaultDepth}; - std::vector> data; + std::vector data; + data.reserve(tuning::kSoftMaxReplyNodes); + auto const useLedgerNodeDepth = supportsFeature(ProtocolFeature::LedgerNodeDepth); - for (int i = 0; - i < m->nodeids_size() && ledgerData.nodes_size() < Tuning::kSoftMaxReplyNodes; - ++i) + for (auto const& nodeID : nodeIDs) { - auto const shaMapNodeId{deserializeSHAMapNodeID(m->nodeids(i))}; + if (ledgerData.nodes_size() >= tuning::kSoftMaxReplyNodes) + break; data.clear(); - data.reserve(Tuning::kSoftMaxReplyNodes); try { - // NOLINTNEXTLINE(bugprone-unchecked-optional-access) nodeids checked in onGetLedger - if (map->getNodeFat(*shaMapNodeId, data, fatLeaves, queryDepth)) + if (map->getNodeFat(nodeID, data, fatLeaves, queryDepth)) { JLOG(pJournal_.trace()) << "processLedgerRequest: getNodeFat got " << data.size() << " nodes"; for (auto const& d : data) { - if (ledgerData.nodes_size() >= Tuning::kHardMaxReplyNodes) + if (ledgerData.nodes_size() >= tuning::kHardMaxReplyNodes) break; + protocol::TMLedgerNode* node{ledgerData.add_nodes()}; - node->set_nodeid(d.first.getRawString()); - node->set_nodedata(d.second.data(), d.second.size()); + node->set_nodedata(d.data.data(), d.data.size()); + + // When the LedgerNodeDepth protocol feature is not supported by the peer, + // we always set the `nodeid` field. However, when it is supported then we + // set the `id` field for inner nodes and the `depth` field for leaf nodes. + if (!useLedgerNodeDepth) + { + node->set_nodeid(d.nodeID.getRawString()); + } + else if (d.isLeaf) + { + REACHABLE("xrpl::PeerImp : emit leaf depth in reply"); + node->set_depth(d.nodeID.getDepth()); + } + else + { + REACHABLE("xrpl::PeerImp : emit inner id in reply"); + node->set_id(d.nodeID.getRawString()); + } } } else @@ -3441,13 +3602,13 @@ PeerImp::processLedgerRequest(std::shared_ptr const& m) info += ", no hash specified"; JLOG(pJournal_.warn()) - << "processLedgerRequest: getNodeFat with nodeId " << *shaMapNodeId + << "processLedgerRequest: getNodeFat with nodeId " << nodeID << " and ledger info type " << info << " throws exception: " << e.what(); } } JLOG(pJournal_.info()) << "processLedgerRequest: Got request for " << m->nodeids_size() - << " nodes at depth " << queryDepth << ", return " + << " node IDs at depth " << queryDepth << ", return " << ledgerData.nodes_size() << " nodes"; } @@ -3478,30 +3639,30 @@ PeerImp::processLedgerRequest(std::shared_ptr const& m) // // Misses are billed first against the billable budget because a node store // seek dominates a cache hit and because invalid hashes are ~100% miss by construction. -Resource::Charge +resource::Charge PeerImp::computeGetObjectByHashFee(int const requested, int const found) { - int const billable = std::max(0, requested - static_cast(Tuning::kFreeObjectsPerRequest)); + int const billable = std::max(0, requested - static_cast(tuning::kFreeObjectsPerRequest)); // Clamp `missed` so a future caller passing found > requested cannot // produce a negative value that flips the hits/misses split. int const missed = std::max(0, requested - found); int const billableMisses = std::min(missed, billable); int const billableHits = billable - billableMisses; - int sizeBand = Tuning::kCostBandSmall; - if (requested > Tuning::kBandMediumMax) + int sizeBand = tuning::kCostBandSmall; + if (requested > tuning::kBandMediumMax) { - sizeBand = Tuning::kCostBandLarge; + sizeBand = tuning::kCostBandLarge; } - else if (requested > Tuning::kBandSmallMax) + else if (requested > tuning::kBandSmallMax) { - sizeBand = Tuning::kCostBandMedium; + sizeBand = tuning::kCostBandMedium; } - int const dynamic = (billableHits * Tuning::kCostPerLookupHit) + - (billableMisses * Tuning::kCostPerLookupMiss) + sizeBand; + int const dynamic = (billableHits * tuning::kCostPerLookupHit) + + (billableMisses * tuning::kCostPerLookupMiss) + sizeBand; - return Resource::Charge(dynamic, "GetObject differential"); + return resource::Charge(dynamic, "GetObject differential"); } int diff --git a/src/xrpld/overlay/detail/PeerImp.h b/src/xrpld/overlay/detail/PeerImp.h index 90f8a917f4..3fcfe6359a 100644 --- a/src/xrpld/overlay/detail/PeerImp.h +++ b/src/xrpld/overlay/detail/PeerImp.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -96,7 +97,7 @@ private: // Updated at each stage of the connection process to reflect // the current conditions as closely as possible. - beast::IP::Endpoint const remoteAddress_; + beast::ip::Endpoint const remoteAddress_; // These are up here to prevent warnings about order of initializations // @@ -160,11 +161,11 @@ private: struct ChargeWithContext { - Resource::Charge fee = Resource::kFeeTrivialPeer; + resource::Charge fee = resource::kFeeTrivialPeer; std::string context{}; // NOLINT(readability-redundant-member-init) void - update(Resource::Charge f, std::string const& add) + update(resource::Charge f, std::string const& add) { XRPL_ASSERT(f >= fee, "xrpl::PeerImp::ChargeWithContext::update : fee increases"); fee = f; @@ -178,7 +179,7 @@ private: std::mutex mutable recentLock_; protocol::TMStatusChange lastStatus_; - Resource::Consumer usage_; + resource::Consumer usage_; ChargeWithContext fee_; // One-shot guard so concurrent JobQueue workers cannot double-count @@ -186,7 +187,7 @@ private: // post duplicate fail() calls) when several queued requests cross // kDropThreshold before the first fail() lands on the strand. std::atomic chargeDisconnectFired_{false}; - std::shared_ptr const slot_; + std::shared_ptr const slot_; boost::beast::multi_buffer readBuffer_; http_request_type request_; http_response_type response_; @@ -257,11 +258,11 @@ public: PeerImp( Application& app, id_t id, - std::shared_ptr const& slot, + std::shared_ptr const& slot, http_request_type&& request, PublicKey const& publicKey, ProtocolVersion protocol, - Resource::Consumer consumer, + resource::Consumer consumer, std::unique_ptr&& streamPtr, OverlayImpl& overlay); @@ -274,9 +275,9 @@ public: Application& app, std::unique_ptr&& streamPtr, Buffers const& buffers, - std::shared_ptr&& slot, + std::shared_ptr&& slot, http_response_type&& response, - Resource::Consumer usage, + resource::Consumer usage, PublicKey const& publicKey, ProtocolVersion protocol, id_t id, @@ -290,7 +291,7 @@ public: return pJournal_; } - std::shared_ptr const& + std::shared_ptr const& slot() { return slot_; @@ -338,16 +339,18 @@ public: void sendEndpoints(FwdIt first, FwdIt last) requires( - std::is_same_v::value_type, PeerFinder::Endpoint>); + std::is_same_v< // + typename std::iterator_traits::value_type, + peer_finder::Endpoint>); - beast::IP::Endpoint + beast::ip::Endpoint getRemoteAddress() const override { return remoteAddress_; } void - charge(Resource::Charge const& fee, std::string const& context) override; + charge(resource::Charge const& fee, std::string const& context) override; // // Identity @@ -679,7 +682,9 @@ private: getTxSet(std::shared_ptr const& m) const; void - processLedgerRequest(std::shared_ptr const& m); + processLedgerRequest( + std::shared_ptr const& m, + std::vector nodeIDs); protected: // Kept `protected` so test subclasses (see @@ -694,7 +699,7 @@ protected: * * Dispatched from `onMessage(TMGetObjectByHash)` to the JobQueue * (`JtLedgerReq`) so synchronous NodeStore lookups do not block the - * peer's I/O strand. Caps iteration at `Tuning::kHardMaxReplyNodes` + * peer's I/O strand. Caps iteration at `tuning::kHardMaxReplyNodes` * regardless of hit/miss outcome and applies differential pricing * via `computeGetObjectByHashFee()` after the fetch loop completes. * @@ -708,25 +713,25 @@ protected: * request based on how much work was actually performed. * * The charge has three components on top of the base - * `Resource::kFeeModerateBurdenPeer`: + * `resource::kFeeModerateBurdenPeer`: * - per-hit lookup cost (cheap; usually served from cache) * - per-miss lookup cost (expensive node store seeks) * - request-size band surcharge (escalates abusive batch sizes) * - * The first `Tuning::kFreeObjectsPerRequest` objects are free so + * The first `tuning::kFreeObjectsPerRequest` objects are free so * that legitimate `InboundLedger::getNeededHashes()` traffic * (at most 8 objects) is unaffected. * * @param requested Number of objects requested by the message. This * value is used for request-size pricing and may - * exceed `Tuning::kHardMaxReplyNodes` when this + * exceed `tuning::kHardMaxReplyNodes` when this * helper is called directly, even though processing - * caps the iterations to `Tuning::kHardMaxReplyNodes`. + * caps the iterations to `tuning::kHardMaxReplyNodes`. * @param found Number of objects successfully returned in the * reply. - * @return A `Resource::Charge` whose cost reflects the work performed. + * @return A `resource::Charge` whose cost reflects the work performed. */ - static Resource::Charge + static resource::Charge computeGetObjectByHashFee(int const requested, int const found); /** @@ -737,9 +742,9 @@ protected: * full JobQueue handler. Production callers should never read this back — * the value is consumed by `charge()`/`disconnect()` internally. * - * @return The current `Resource::Charge` accumulated on `fee_`. + * @return The current `resource::Charge` accumulated on `fee_`. */ - Resource::Charge + resource::Charge currentFeeCharge() const { return fee_.fee; @@ -753,9 +758,9 @@ PeerImp::PeerImp( Application& app, std::unique_ptr&& streamPtr, Buffers const& buffers, - std::shared_ptr&& slot, + std::shared_ptr&& slot, http_response_type&& response, - Resource::Consumer usage, + resource::Consumer usage, PublicKey const& publicKey, ProtocolVersion protocol, id_t id, @@ -785,7 +790,7 @@ PeerImp::PeerImp( , creationTime_(clock_type::now()) , squelch_(app_.getJournal("Squelch")) , usage_(usage) - , fee_{.fee = Resource::kFeeTrivialPeer} + , fee_{.fee = resource::kFeeTrivialPeer} , slot_(std::move(slot)) , response_(std::move(response)) , headers_(response_) @@ -812,7 +817,8 @@ PeerImp::PeerImp( template void PeerImp::sendEndpoints(FwdIt first, FwdIt last) - requires(std::is_same_v::value_type, PeerFinder::Endpoint>) + requires( + std::is_same_v::value_type, peer_finder::Endpoint>) { protocol::TMEndpoints tm; diff --git a/src/xrpld/overlay/detail/ProtocolVersion.cpp b/src/xrpld/overlay/detail/ProtocolVersion.cpp index 347e59accb..2d5d0a56f7 100644 --- a/src/xrpld/overlay/detail/ProtocolVersion.cpp +++ b/src/xrpld/overlay/detail/ProtocolVersion.cpp @@ -29,6 +29,7 @@ namespace xrpl { constexpr ProtocolVersion const kSupportedProtocolList[]{ {2, 1}, {2, 2}, + {2, 3}, }; // This ugly construct ensures that supportedProtocolList is sorted in strictly diff --git a/src/xrpld/overlay/detail/Tuning.h b/src/xrpld/overlay/detail/Tuning.h index 5488fab07b..7561a4f385 100644 --- a/src/xrpld/overlay/detail/Tuning.h +++ b/src/xrpld/overlay/detail/Tuning.h @@ -5,7 +5,7 @@ #include #include -namespace xrpl::Tuning { +namespace xrpl::tuning { /** * How many ledgers off a server can be and we will @@ -74,7 +74,7 @@ constexpr std::size_t kReadBufferBytes = 16384; * while a hit is usually served from cache. On top of that, a size-band * surcharge kicks in for larger requests so an attacker who crams a * single message with thousands of hashes blows past - * `Resource::kDropThreshold` and gets disconnected. + * `resource::kDropThreshold` and gets disconnected. * * The numbers below are picked to keep three things true given * `kDropThreshold = 25000`: @@ -165,4 +165,4 @@ static constexpr auto kLegitHashesPerType = 4; static constexpr auto kBandSmallMax = kLegitHashesPerType * SHAMapInnerNode::kBranchFactor; static constexpr auto kBandMediumMax = kBandSmallMax * SHAMapInnerNode::kBranchFactor; -} // namespace xrpl::Tuning +} // namespace xrpl::tuning diff --git a/src/xrpld/overlay/make_Overlay.h b/src/xrpld/overlay/make_Overlay.h index a62d4b49de..c730a05c54 100644 --- a/src/xrpld/overlay/make_Overlay.h +++ b/src/xrpld/overlay/make_Overlay.h @@ -27,7 +27,7 @@ makeOverlay( Application& app, Overlay::Setup const& setup, ServerHandler& serverHandler, - Resource::Manager& resourceManager, + resource::Manager& resourceManager, Resolver& resolver, boost::asio::io_context& ioContext, BasicConfig const& config, diff --git a/src/xrpld/peerfinder/PeerfinderManager.h b/src/xrpld/peerfinder/PeerfinderManager.h index f96ea31943..5934e3bc4a 100644 --- a/src/xrpld/peerfinder/PeerfinderManager.h +++ b/src/xrpld/peerfinder/PeerfinderManager.h @@ -6,7 +6,7 @@ #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { Config makeConfig( @@ -16,4 +16,4 @@ makeConfig( int ipLimit, bool verifyEndpoints); -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/xrpld/peerfinder/detail/PeerfinderConfig.cpp b/src/xrpld/peerfinder/detail/PeerfinderConfig.cpp index a893b969e0..b222f6c077 100644 --- a/src/xrpld/peerfinder/detail/PeerfinderConfig.cpp +++ b/src/xrpld/peerfinder/detail/PeerfinderConfig.cpp @@ -5,7 +5,7 @@ #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { Config makeConfig( @@ -36,4 +36,4 @@ makeConfig( verifyEndpoints); } -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/xrpld/peerfinder/detail/StoreSqdb.h b/src/xrpld/peerfinder/detail/StoreSqdb.h index b0973a42b3..ce13d72c15 100644 --- a/src/xrpld/peerfinder/detail/StoreSqdb.h +++ b/src/xrpld/peerfinder/detail/StoreSqdb.h @@ -14,7 +14,7 @@ #include #include -namespace xrpl::PeerFinder { +namespace xrpl::peer_finder { /** * Database persistence for PeerFinder using SQLite @@ -50,7 +50,7 @@ public: std::size_t n(0); readPeerFinderDB(sqlDb_, [&](std::string const& s, int valence) { - beast::IP::Endpoint const endpoint(beast::IP::Endpoint::fromString(s)); + beast::ip::Endpoint const endpoint(beast::ip::Endpoint::fromString(s)); if (!isUnspecified(endpoint)) { @@ -90,4 +90,4 @@ private: } }; -} // namespace xrpl::PeerFinder +} // namespace xrpl::peer_finder diff --git a/src/xrpld/perflog/detail/PerfLogImp.h b/src/xrpld/perflog/detail/PerfLogImp.h index 14477512ff..7efdbe1b7f 100644 --- a/src/xrpld/perflog/detail/PerfLogImp.h +++ b/src/xrpld/perflog/detail/PerfLogImp.h @@ -109,7 +109,7 @@ class PerfLogImp : public PerfLog Application& app_; beast::Journal const j_; std::function const signalStop_; - Counters counters_{xrpl::RPC::getHandlerNames(), JobTypes::instance()}; + Counters counters_{xrpl::rpc::getHandlerNames(), JobTypes::instance()}; std::ofstream logFile_; std::thread thread_; std::mutex mutex_; diff --git a/src/xrpld/rpc/BookChanges.h b/src/xrpld/rpc/BookChanges.h index 3c10ece78f..16f7ea8e43 100644 --- a/src/xrpld/rpc/BookChanges.h +++ b/src/xrpld/rpc/BookChanges.h @@ -32,7 +32,7 @@ class Transaction; class TxMeta; class STTx; -namespace RPC { +namespace rpc { template json::Value @@ -233,5 +233,5 @@ computeBookChanges(std::shared_ptr const& lpAccepted) return jvObj; } -} // namespace RPC +} // namespace rpc } // namespace xrpl diff --git a/src/xrpld/rpc/CTID.h b/src/xrpld/rpc/CTID.h index 7566e0e143..71ded5834f 100644 --- a/src/xrpld/rpc/CTID.h +++ b/src/xrpld/rpc/CTID.h @@ -11,7 +11,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { // CTID stands for Concise Transaction ID. // @@ -111,4 +111,4 @@ decodeCTID(T const ctid) noexcept return std::make_tuple(ledgerSeq, txnIndex, networkID); } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/Context.h b/src/xrpld/rpc/Context.h index 81ba068d8f..e85bf6dfe6 100644 --- a/src/xrpld/rpc/Context.h +++ b/src/xrpld/rpc/Context.h @@ -18,7 +18,7 @@ class Application; class NetworkOPs; class LedgerMaster; -namespace RPC { +namespace rpc { /** * The context of information needed to call an RPC. @@ -27,10 +27,10 @@ struct Context { beast::Journal const j; Application& app; - Resource::Charge& loadType; + resource::Charge& loadType; NetworkOPs& netOps; LedgerMaster& ledgerMaster; - Resource::Consumer& consumer; + resource::Consumer& consumer; Role role; std::shared_ptr coro; InfoSub::pointer infoSub; @@ -59,5 +59,5 @@ struct GRPCContext : public Context RequestType params; }; -} // namespace RPC +} // namespace rpc } // namespace xrpl diff --git a/src/xrpld/rpc/DeliveredAmount.h b/src/xrpld/rpc/DeliveredAmount.h index dc635c6861..a7878070d6 100644 --- a/src/xrpld/rpc/DeliveredAmount.h +++ b/src/xrpld/rpc/DeliveredAmount.h @@ -17,7 +17,7 @@ class Transaction; class TxMeta; class STTx; -namespace RPC { +namespace rpc { struct JsonContext; @@ -41,23 +41,23 @@ insertDeliveredAmount( void insertDeliveredAmount( json::Value& meta, - RPC::JsonContext const&, + rpc::JsonContext const&, std::shared_ptr const&, TxMeta const&); void insertDeliveredAmount( json::Value& meta, - RPC::JsonContext const&, + rpc::JsonContext const&, std::shared_ptr const&, TxMeta const&); std::optional getDeliveredAmount( - RPC::Context const& context, + rpc::Context const& context, std::shared_ptr const& serializedTx, TxMeta const& transactionMeta, LedgerIndex const& ledgerIndex); /** @} */ -} // namespace RPC +} // namespace rpc } // namespace xrpl diff --git a/src/xrpld/rpc/GRPCHandlers.h b/src/xrpld/rpc/GRPCHandlers.h index 9dc7e0b13a..cabd55d53b 100644 --- a/src/xrpld/rpc/GRPCHandlers.h +++ b/src/xrpld/rpc/GRPCHandlers.h @@ -14,22 +14,22 @@ namespace xrpl { /* * These handlers are for gRPC. They each take in a protobuf message that is - * nested inside RPC::GRPCContext, where T is the request type + * nested inside rpc::GRPCContext, where T is the request type * The return value is the response type, as well as a status * If the status is not Status::OK (meaning an error occurred), then only * the status will be sent to the client, and the response will be omitted */ std::pair -doLedgerGrpc(RPC::GRPCContext& context); +doLedgerGrpc(rpc::GRPCContext& context); std::pair -doLedgerEntryGrpc(RPC::GRPCContext& context); +doLedgerEntryGrpc(rpc::GRPCContext& context); std::pair -doLedgerDataGrpc(RPC::GRPCContext& context); +doLedgerDataGrpc(rpc::GRPCContext& context); std::pair -doLedgerDiffGrpc(RPC::GRPCContext& context); +doLedgerDiffGrpc(rpc::GRPCContext& context); } // namespace xrpl diff --git a/src/xrpld/rpc/MPTokenIssuanceID.h b/src/xrpld/rpc/MPTokenIssuanceID.h index f56826bfb8..678fda0369 100644 --- a/src/xrpld/rpc/MPTokenIssuanceID.h +++ b/src/xrpld/rpc/MPTokenIssuanceID.h @@ -8,7 +8,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { /** * Add a `mpt_issuance_id` field to the `meta` input/output parameter. @@ -32,4 +32,4 @@ insertMPTokenIssuanceID( TxMeta const& transactionMeta); /** @} */ -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/Output.h b/src/xrpld/rpc/Output.h index 30b5c090d7..f528efef56 100644 --- a/src/xrpld/rpc/Output.h +++ b/src/xrpld/rpc/Output.h @@ -5,7 +5,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { using Output = std::function; @@ -15,4 +15,4 @@ stringOutput(std::string& s) return [&](boost::string_ref const& b) { s.append(b.data(), b.size()); }; } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/RPCCall.h b/src/xrpld/rpc/RPCCall.h index a72b35e344..2fec78f93b 100644 --- a/src/xrpld/rpc/RPCCall.h +++ b/src/xrpld/rpc/RPCCall.h @@ -26,7 +26,7 @@ namespace xrpl { /** * Processes XRPL RPC calls. */ -namespace RPCCall { +namespace rpc_call { int fromCommandLine(Config const& config, std::vector const& vCmd, Logs& logs); @@ -47,7 +47,7 @@ fromNetwork( std::function callbackFuncP = std::function(), std::unordered_map headers = {}); -} // namespace RPCCall +} // namespace rpc_call json::Value rpcCmdToJson( diff --git a/src/xrpld/rpc/RPCHandler.h b/src/xrpld/rpc/RPCHandler.h index fcd0f54265..637a492943 100644 --- a/src/xrpld/rpc/RPCHandler.h +++ b/src/xrpld/rpc/RPCHandler.h @@ -8,7 +8,7 @@ #include -namespace xrpl::RPC { +namespace xrpl::rpc { struct JsonContext; @@ -16,9 +16,9 @@ struct JsonContext; * Execute an RPC command and store the results in a json::Value. */ Status -doCommand(RPC::JsonContext&, json::Value&); +doCommand(rpc::JsonContext&, json::Value&); Role roleRequired(unsigned int version, bool betaEnabled, std::string const& method); -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/Role.h b/src/xrpld/rpc/Role.h index 2c2ae6b781..48c89333bd 100644 --- a/src/xrpld/rpc/Role.h +++ b/src/xrpld/rpc/Role.h @@ -40,13 +40,13 @@ requestRole( Role const& required, Port const& port, json::Value const& params, - beast::IP::Endpoint const& remoteIp, + beast::ip::Endpoint const& remoteIp, std::string_view user); -Resource::Consumer +resource::Consumer requestInboundEndpoint( - Resource::Manager& manager, - beast::IP::Endpoint const& remoteAddress, + resource::Manager& manager, + beast::ip::Endpoint const& remoteAddress, Role const& role, std::string_view user, std::string_view forwardedFor); @@ -66,7 +66,7 @@ isUnlimited(Role const& role); */ bool ipAllowed( - beast::IP::Address const& remoteIp, + beast::ip::Address const& remoteIp, std::vector const& nets4, std::vector const& nets6); diff --git a/src/xrpld/rpc/ServerHandler.h b/src/xrpld/rpc/ServerHandler.h index 054bec9b5b..a09fc1c18a 100644 --- a/src/xrpld/rpc/ServerHandler.h +++ b/src/xrpld/rpc/ServerHandler.h @@ -80,7 +80,7 @@ private: using stream_type = boost::beast::ssl_stream; Application& app_; - Resource::Manager& resourceManager_; + resource::Manager& resourceManager_; beast::Journal journal_; NetworkOPs& networkOPs_; std::unique_ptr server_; @@ -109,7 +109,7 @@ private: boost::asio::io_context&, JobQueue&, NetworkOPs&, - Resource::Manager&, + resource::Manager&, CollectorManager& cm); public: @@ -120,7 +120,7 @@ public: boost::asio::io_context& ioContext, JobQueue& jobQueue, NetworkOPs& networkOPs, - Resource::Manager& resourceManager, + resource::Manager& resourceManager, CollectorManager& cm); ~ServerHandler(); @@ -196,7 +196,7 @@ private: processRequest( Port const& port, std::string const& request, - beast::IP::Endpoint const& remoteIPAddress, + beast::ip::Endpoint const& remoteIPAddress, Output const&, std::shared_ptr coro, std::string_view forwardedFor, @@ -215,7 +215,7 @@ makeServerHandler( boost::asio::io_context&, JobQueue&, NetworkOPs&, - Resource::Manager&, + resource::Manager&, CollectorManager& cm); } // namespace xrpl diff --git a/src/xrpld/rpc/Status.h b/src/xrpld/rpc/Status.h index dda1e89d31..e2716bd579 100644 --- a/src/xrpld/rpc/Status.h +++ b/src/xrpld/rpc/Status.h @@ -11,7 +11,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { /** * Status represents the results of an operation that might fail. @@ -56,9 +56,11 @@ public: { } - /* Returns a representation of the integer status Code as a string. - If the Status is OK, the result is an empty string. - */ + /** + * If the Status is OK, the result is an empty string. + * + * @return a representation of the integer status Code as a string. + */ [[nodiscard]] std::string codeString() const; @@ -86,7 +88,7 @@ public: [[nodiscard]] TER toTER() const { - XRPL_ASSERT(type_ == Type::TER, "xrpl::RPC::Status::toTER : type is TER"); + XRPL_ASSERT(type_ == Type::TER, "xrpl::rpc::Status::toTER : type is TER"); return TER::fromInt(code_); } @@ -97,7 +99,8 @@ public: [[nodiscard]] ErrorCodeI toErrorCode() const { - XRPL_ASSERT(type_ == Type::ErrorCodeI, "xrpl::RPC::Status::toTER : type is error code"); + XRPL_ASSERT( + type_ == Type::ErrorCodeI, "xrpl::rpc::Status::toErrorCode : type is error code"); return ErrorCodeI(code_); } @@ -155,4 +158,4 @@ private: Strings messages_; }; -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/DeliveredAmount.cpp b/src/xrpld/rpc/detail/DeliveredAmount.cpp index 8d8aac33bf..7b8c5e0623 100644 --- a/src/xrpld/rpc/detail/DeliveredAmount.cpp +++ b/src/xrpld/rpc/detail/DeliveredAmount.cpp @@ -16,7 +16,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { /* GetLedgerIndex and GetCloseTime are lambdas that allow the close time and @@ -114,7 +114,7 @@ insertDeliveredAmount( template static std::optional getDeliveredAmount( - RPC::Context const& context, + rpc::Context const& context, std::shared_ptr const& serializedTx, TxMeta const& transactionMeta, GetLedgerIndex const& getLedgerIndex) @@ -133,7 +133,7 @@ getDeliveredAmount( std::optional getDeliveredAmount( - RPC::Context const& context, + rpc::Context const& context, std::shared_ptr const& serializedTx, TxMeta const& transactionMeta, LedgerIndex const& ledgerIndex) @@ -145,7 +145,7 @@ getDeliveredAmount( void insertDeliveredAmount( json::Value& meta, - RPC::JsonContext const& context, + rpc::JsonContext const& context, std::shared_ptr const& transaction, TxMeta const& transactionMeta) { @@ -155,7 +155,7 @@ insertDeliveredAmount( void insertDeliveredAmount( json::Value& meta, - RPC::JsonContext const& context, + rpc::JsonContext const& context, std::shared_ptr const& transaction, TxMeta const& transactionMeta) { @@ -178,4 +178,4 @@ insertDeliveredAmount( } } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/Handler.cpp b/src/xrpld/rpc/detail/Handler.cpp index 4f5ce34c1f..326af4f4ee 100644 --- a/src/xrpld/rpc/detail/Handler.cpp +++ b/src/xrpld/rpc/detail/Handler.cpp @@ -18,7 +18,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { namespace { /** @@ -33,8 +33,8 @@ byRef(Function const& f) if (result.type() != json::ValueType::Object) { // LCOV_EXCL_START - UNREACHABLE("xrpl::RPC::byRef : result is object"); - result = RPC::makeObjectValue(result); + UNREACHABLE("xrpl::rpc::byRef : result is object"); + result = rpc::makeObjectValue(result); // LCOV_EXCL_STOP } @@ -49,7 +49,7 @@ handle(JsonContext& context, Object& object) XRPL_ASSERT( context.apiVersion >= HandlerImpl::minApiVer && context.apiVersion <= HandlerImpl::maxApiVer, - "xrpl::RPC::handle : valid API version"); + "xrpl::rpc::handle : valid API version"); HandlerImpl handler(context); auto status = handler.check(); @@ -382,10 +382,10 @@ private: unsigned minVer, unsigned maxVer) { - XRPL_ASSERT(minVer <= maxVer, "xrpl::RPC::HandlerTable : valid API version range"); + XRPL_ASSERT(minVer <= maxVer, "xrpl::rpc::HandlerTable : valid API version range"); XRPL_ASSERT( - maxVer <= RPC::kApiMaximumValidVersion, - "xrpl::RPC::HandlerTable : valid max API version"); + maxVer <= rpc::kApiMaximumValidVersion, + "xrpl::rpc::HandlerTable : valid max API version"); return std::any_of( range.first, @@ -427,8 +427,8 @@ public: [[nodiscard]] Handler const* getHandler(unsigned version, bool betaEnabled, std::string const& name) const { - if (version < RPC::kApiMinimumSupportedVersion || - version > (betaEnabled ? RPC::kApiBetaVersion : RPC::kApiMaximumSupportedVersion)) + if (version < rpc::kApiMinimumSupportedVersion || + version > (betaEnabled ? rpc::kApiBetaVersion : rpc::kApiMaximumSupportedVersion)) return nullptr; auto const range = table_.equal_range(name); @@ -457,8 +457,8 @@ private: addHandler() { static_assert(HandlerImpl::minApiVer <= HandlerImpl::maxApiVer); - static_assert(HandlerImpl::maxApiVer <= RPC::kApiMaximumValidVersion); - static_assert(RPC::kApiMinimumSupportedVersion <= HandlerImpl::minApiVer); + static_assert(HandlerImpl::maxApiVer <= rpc::kApiMaximumValidVersion); + static_assert(rpc::kApiMinimumSupportedVersion <= HandlerImpl::minApiVer); if (overlappingApiVersion( table_.equal_range(HandlerImpl::name), @@ -488,4 +488,4 @@ getHandlerNames() return HandlerTable::instance().getHandlerNames(); } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/Handler.h b/src/xrpld/rpc/detail/Handler.h index 37259c8648..7342c5fcbf 100644 --- a/src/xrpld/rpc/detail/Handler.h +++ b/src/xrpld/rpc/detail/Handler.h @@ -20,7 +20,7 @@ namespace json { class Object; } // namespace json -namespace xrpl::RPC { +namespace xrpl::rpc { // Under what condition can we call this RPC? enum class Condition { @@ -38,7 +38,7 @@ struct Handler char const* name; Method valueMethod; Role role; - RPC::Condition condition; + rpc::Condition condition; unsigned minApiVer = kApiMinimumSupportedVersion; unsigned maxApiVer = kApiMaximumValidVersion; @@ -92,7 +92,7 @@ conditionMet(Condition conditionRequired, T& context) if (!context.app.config().standalone() && conditionRequired != Condition::NoCondition) { - if (context.ledgerMaster.getValidatedLedgerAge() > Tuning::kMaxValidatedLedgerAge) + if (context.ledgerMaster.getValidatedLedgerAge() > tuning::kMaxValidatedLedgerAge) { if (context.apiVersion == 1) return RpcNoCurrent; @@ -122,4 +122,4 @@ conditionMet(Condition conditionRequired, T& context) return RpcSuccess; } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/LegacyPathFind.cpp b/src/xrpld/rpc/detail/LegacyPathFind.cpp index 0bfa19a1f4..837d98084e 100644 --- a/src/xrpld/rpc/detail/LegacyPathFind.cpp +++ b/src/xrpld/rpc/detail/LegacyPathFind.cpp @@ -9,7 +9,7 @@ #include -namespace xrpl::RPC { +namespace xrpl::rpc { LegacyPathFind::LegacyPathFind(bool isAdmin, Application& app) { @@ -21,13 +21,13 @@ LegacyPathFind::LegacyPathFind(bool isAdmin, Application& app) } auto const& jobCount = app.getJobQueue().getJobCountGE(JtClient); - if (jobCount > Tuning::kMaxPathfindJobCount || app.getFeeTrack().isLoadedLocal()) + if (jobCount > tuning::kMaxPathfindJobCount || app.getFeeTrack().isLoadedLocal()) return; while (true) { int prevVal = inProgress.load(); - if (prevVal >= Tuning::kMaxPathfindsInProgress) + if (prevVal >= tuning::kMaxPathfindsInProgress) return; if (inProgress.compare_exchange_strong( @@ -47,4 +47,4 @@ LegacyPathFind::~LegacyPathFind() std::atomic LegacyPathFind::inProgress(0); -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/LegacyPathFind.h b/src/xrpld/rpc/detail/LegacyPathFind.h index 226191848f..30e176f245 100644 --- a/src/xrpld/rpc/detail/LegacyPathFind.h +++ b/src/xrpld/rpc/detail/LegacyPathFind.h @@ -6,7 +6,7 @@ namespace xrpl { class Application; -namespace RPC { +namespace rpc { class LegacyPathFind { @@ -26,5 +26,5 @@ private: bool isOk_{false}; }; -} // namespace RPC +} // namespace rpc } // namespace xrpl diff --git a/src/xrpld/rpc/detail/MPTokenIssuanceID.cpp b/src/xrpld/rpc/detail/MPTokenIssuanceID.cpp index e34980aee2..4f57bab9ab 100644 --- a/src/xrpld/rpc/detail/MPTokenIssuanceID.cpp +++ b/src/xrpld/rpc/detail/MPTokenIssuanceID.cpp @@ -16,7 +16,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { bool canHaveMPTokenIssuanceID( @@ -67,4 +67,4 @@ insertMPTokenIssuanceID( response[jss::mpt_issuance_id] = to_string(result.value()); } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/PathRequest.cpp b/src/xrpld/rpc/detail/PathRequest.cpp index 3c09917dad..fb132199bc 100644 --- a/src/xrpld/rpc/detail/PathRequest.cpp +++ b/src/xrpld/rpc/detail/PathRequest.cpp @@ -73,7 +73,7 @@ PathRequest::PathRequest( PathRequest::PathRequest( Application& app, std::function completion, - Resource::Consumer& consumer, + resource::Consumer& consumer, int id, PathRequestManager& owner, beast::Journal journal) @@ -344,7 +344,7 @@ PathRequest::parseJson(json::Value const& jvParams) { json::Value const& jvSrcCurrencies = jvParams[jss::source_currencies]; if (!jvSrcCurrencies.isArray() || jvSrcCurrencies.size() == 0 || - jvSrcCurrencies.size() > RPC::Tuning::kMaxSrcCur) + jvSrcCurrencies.size() > rpc::tuning::kMaxSrcCur) { jvStatus_ = rpcError(RpcSrcCurMalformed); return PFR_PJ_INVALID; @@ -556,7 +556,7 @@ PathRequest::findPaths( [&](TAsset const& a) { if (!sameAccount || a != saDstAmount_.asset()) { - if (sourceAssets.size() >= RPC::Tuning::kMaxAutoSrcCur) + if (sourceAssets.size() >= rpc::tuning::kMaxAutoSrcCur) return false; if constexpr (std::is_same_v) { diff --git a/src/xrpld/rpc/detail/PathRequest.h b/src/xrpld/rpc/detail/PathRequest.h index d40d9c82d6..f56b3d0652 100644 --- a/src/xrpld/rpc/detail/PathRequest.h +++ b/src/xrpld/rpc/detail/PathRequest.h @@ -64,7 +64,7 @@ public: PathRequest( Application& app, std::function completion, - Resource::Consumer& consumer, + resource::Consumer& consumer, int id, PathRequestManager&, beast::Journal journal); @@ -137,7 +137,7 @@ private: std::weak_ptr wpSubscriber_; // Who this request came from std::function fCompletion_; - Resource::Consumer& consumer_; // Charge according to source currencies + resource::Consumer& consumer_; // Charge according to source currencies json::Value jvId_; json::Value jvStatus_; // Last result diff --git a/src/xrpld/rpc/detail/PathRequestManager.cpp b/src/xrpld/rpc/detail/PathRequestManager.cpp index 4953634181..117bfbda1e 100644 --- a/src/xrpld/rpc/detail/PathRequestManager.cpp +++ b/src/xrpld/rpc/detail/PathRequestManager.cpp @@ -254,7 +254,7 @@ json::Value PathRequestManager::makeLegacyPathRequest( PathRequest::pointer& req, std::function completion, - Resource::Consumer& consumer, + resource::Consumer& consumer, std::shared_ptr const& inLedger, json::Value const& request) { @@ -285,7 +285,7 @@ PathRequestManager::makeLegacyPathRequest( json::Value PathRequestManager::doLegacyPathRequest( - Resource::Consumer& consumer, + resource::Consumer& consumer, std::shared_ptr const& inLedger, json::Value const& request) { diff --git a/src/xrpld/rpc/detail/PathRequestManager.h b/src/xrpld/rpc/detail/PathRequestManager.h index f6eb80d291..29a80e66c0 100644 --- a/src/xrpld/rpc/detail/PathRequestManager.h +++ b/src/xrpld/rpc/detail/PathRequestManager.h @@ -65,7 +65,7 @@ public: makeLegacyPathRequest( PathRequest::pointer& req, std::function completion, - Resource::Consumer& consumer, + resource::Consumer& consumer, std::shared_ptr const& inLedger, json::Value const& request); @@ -73,7 +73,7 @@ public: // with the ledger specified by the caller json::Value doLegacyPathRequest( - Resource::Consumer& consumer, + resource::Consumer& consumer, std::shared_ptr const& inLedger, json::Value const& request); diff --git a/src/xrpld/rpc/detail/RPCCall.cpp b/src/xrpld/rpc/detail/RPCCall.cpp index b5d5c680cd..a752858527 100644 --- a/src/xrpld/rpc/detail/RPCCall.cpp +++ b/src/xrpld/rpc/detail/RPCCall.cpp @@ -74,7 +74,7 @@ createHTTPPost( // CHECKME this uses a different version than the replies below use. Is // this by design or an accident or should it be using - // BuildInfo::getFullVersionString () as well? + // build_info::getFullVersionString () as well? s << "POST " << (strPath.empty() ? "/" : strPath) << " HTTP/1.0\r\n" << "User-Agent: " << systemName() << "-json-rpc/v1\r\n" @@ -149,7 +149,7 @@ private: return jvResult; } - return RPC::makeParamError( + return rpc::makeParamError( std::string("Invalid currency/issuer '") + strCurrencyIssuer + "'"); } @@ -355,7 +355,7 @@ private: } catch (std::exception const&) { - return RPC::invalidFieldError(jss::limit); + return rpc::invalidFieldError(jss::limit); } } @@ -369,7 +369,7 @@ private: } catch (std::exception const&) { - return RPC::invalidFieldError(jss::proof); + return rpc::invalidFieldError(jss::proof); } } @@ -1182,7 +1182,7 @@ private: std::string param = jvParams[index++].asString(); if (param.empty()) - return RPC::makeParamError("Invalid first parameter"); + return rpc::makeParamError("Invalid first parameter"); if (param[0] != 'r') { @@ -1196,7 +1196,7 @@ private: } if (size <= index) - return RPC::makeParamError("Invalid hotwallet"); + return rpc::makeParamError("Invalid hotwallet"); param = jvParams[index++].asString(); } @@ -1726,7 +1726,7 @@ rpcClient( { boost::asio::io_context isService; - RPCCall::fromNetwork( + rpc_call::fromNetwork( isService, setup.client.ip, setup.client.port, @@ -1813,12 +1813,12 @@ rpcClient( //------------------------------------------------------------------------------ -namespace RPCCall { +namespace rpc_call { int fromCommandLine(Config const& config, std::vector const& vCmd, Logs& logs) { - auto const result = rpcClient(vCmd, config, logs, RPC::kApiCommandLineVersion); + auto const result = rpcClient(vCmd, config, logs, rpc::kApiCommandLineVersion); std::cout << result.second.toStyledString(); @@ -1883,6 +1883,6 @@ fromNetwork( j); } -} // namespace RPCCall +} // namespace rpc_call } // namespace xrpl diff --git a/src/xrpld/rpc/detail/RPCHandler.cpp b/src/xrpld/rpc/detail/RPCHandler.cpp index 6f46aed62d..96d6bf72d7 100644 --- a/src/xrpld/rpc/detail/RPCHandler.cpp +++ b/src/xrpld/rpc/detail/RPCHandler.cpp @@ -24,7 +24,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { namespace { @@ -114,7 +114,7 @@ fillHandler(JsonContext& context, Handler const*& result) { // Count all jobs at jtCLIENT priority or higher. int const jobCount = context.app.getJobQueue().getJobCountGE(JtClient); - if (jobCount > Tuning::kMaxJobQueueClients) + if (jobCount > tuning::kMaxJobQueueClients) { JLOG(context.j.debug()) << "Too busy for command: " << jobCount; return RpcTooBusy; @@ -179,8 +179,8 @@ callMethod(JsonContext& context, Method method, std::string const& name, Object& perfLog.rpcError(name, curId); JLOG(context.j.info()) << "Caught throw: " << e.what(); - if (context.loadType == Resource::kFeeReferenceRpc) - context.loadType = Resource::kFeeExceptionRpc; + if (context.loadType == resource::kFeeReferenceRpc) + context.loadType = resource::kFeeExceptionRpc; injectError(RpcInternal, result); return RpcInternal; @@ -190,7 +190,7 @@ callMethod(JsonContext& context, Method method, std::string const& name, Object& } // namespace Status -doCommand(RPC::JsonContext& context, json::Value& result) +doCommand(rpc::JsonContext& context, json::Value& result) { Handler const* handler = nullptr; if (auto error = fillHandler(context, handler)) @@ -226,7 +226,7 @@ doCommand(RPC::JsonContext& context, json::Value& result) Role roleRequired(unsigned int version, bool betaEnabled, std::string const& method) { - auto handler = RPC::getHandler(version, betaEnabled, method); + auto handler = rpc::getHandler(version, betaEnabled, method); if (handler == nullptr) return Role::FORBID; @@ -234,4 +234,4 @@ roleRequired(unsigned int version, bool betaEnabled, std::string const& method) return handler->role; } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/RPCHelpers.cpp b/src/xrpld/rpc/detail/RPCHelpers.cpp index 1764375812..4fa0fab6f7 100644 --- a/src/xrpld/rpc/detail/RPCHelpers.cpp +++ b/src/xrpld/rpc/detail/RPCHelpers.cpp @@ -42,7 +42,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { std::uint64_t getStartHint(SLE::const_ref sle, AccountID const& accountID) @@ -116,7 +116,7 @@ parseAccountIds(json::Value const& jvArray) } std::optional -readLimitField(unsigned int& limit, Tuning::LimitRange const& range, JsonContext const& context) +readLimitField(unsigned int& limit, tuning::LimitRange const& range, JsonContext const& context) { limit = range.rDefault; if (!context.params.isMember(jss::limit) || context.params[jss::limit].isNull()) @@ -124,11 +124,11 @@ readLimitField(unsigned int& limit, Tuning::LimitRange const& range, JsonContext auto const& jvLimit = context.params[jss::limit]; if (!jvLimit.isUInt() && (!jvLimit.isInt() || jvLimit.asInt() < 0)) - return RPC::expectedFieldError(jss::limit, "unsigned integer"); + return rpc::expectedFieldError(jss::limit, "unsigned integer"); limit = jvLimit.asUInt(); if (limit == 0) - return RPC::invalidFieldError(jss::limit); + return rpc::invalidFieldError(jss::limit); if (!isUnlimited(context.role)) limit = std::max(range.rmin, std::min(range.rmax, limit)); @@ -184,7 +184,7 @@ getSeedFromRPC(json::Value const& params, json::Value& error) if (count != 1) { - error = RPC::makeParamError( + error = rpc::makeParamError( "Exactly one of the following must be specified: " + std::string(jss::passphrase) + ", " + std::string(jss::seed) + " or " + std::string(jss::seed_hex)); return std::nullopt; @@ -194,7 +194,7 @@ getSeedFromRPC(json::Value const& params, json::Value& error) auto const& param = params[seedType->first]; if (!param.isString()) { - error = RPC::expectedFieldError(seedType->first, "string"); + error = rpc::expectedFieldError(seedType->first, "string"); return std::nullopt; } @@ -232,13 +232,13 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int if (count == 0 || secretType == nullptr) { - error = RPC::missingFieldError(jss::secret); + error = rpc::missingFieldError(jss::secret); return {}; } if (count > 1) { - error = RPC::makeParamError( + error = rpc::makeParamError( "Exactly one of the following must be specified: " + std::string(jss::passphrase) + ", " + std::string(jss::secret) + ", " + std::string(jss::seed) + " or " + std::string(jss::seed_hex)); @@ -252,7 +252,7 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int { if (!params[jss::key_type].isString()) { - error = RPC::expectedFieldError(jss::key_type, "string"); + error = rpc::expectedFieldError(jss::key_type, "string"); return {}; } @@ -262,11 +262,11 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int { if (apiVersion > 1u) { - error = RPC::makeError(RpcBadKeyType); + error = rpc::makeError(RpcBadKeyType); } else { - error = RPC::invalidFieldError(jss::key_type); + error = rpc::invalidFieldError(jss::key_type); } return {}; } @@ -275,7 +275,7 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int // https://developercommunity.visualstudio.com/t/assigning-constexpr-char--to-static-cha/10021357?entry=problem) if (strcmp(secretType, jss::secret.cStr()) == 0) { - error = RPC::makeParamError( + error = rpc::makeParamError( "The secret field is not allowed if " + std::string(jss::key_type) + " is used."); return {}; } @@ -288,7 +288,7 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int // https://developercommunity.visualstudio.com/t/assigning-constexpr-char--to-static-cha/10021357?entry=problem) if (strcmp(secretType, jss::seed_hex.cStr()) != 0) { - seed = RPC::parseXrplLibSeed(params[secretType]); + seed = rpc::parseXrplLibSeed(params[secretType]); if (seed) { @@ -296,7 +296,7 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int // requested another key type, return an error. if (keyType.value_or(KeyType::Ed25519) != KeyType::Ed25519) { - error = RPC::makeError(RpcBadSeed, "Specified seed is for an Ed25519 wallet."); + error = rpc::makeError(RpcBadSeed, "Specified seed is for an Ed25519 wallet."); return {}; } @@ -317,7 +317,7 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int { if (!params[jss::secret].isString()) { - error = RPC::expectedFieldError(jss::secret, "string"); + error = rpc::expectedFieldError(jss::secret, "string"); return {}; } @@ -329,7 +329,7 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int { if (!containsError(error)) { - error = RPC::makeError(RpcBadSeed, RPC::invalidFieldMessage(secretType)); + error = rpc::makeError(RpcBadSeed, rpc::invalidFieldMessage(secretType)); } return {}; @@ -341,10 +341,10 @@ keypairForSignature(json::Value const& params, json::Value& error, unsigned int return generateKeyPair(*keyType, *seed); } -std::pair +std::pair chooseLedgerEntryType(json::Value const& params) { - std::pair result{RPC::Status::kOK, ltANY}; + std::pair result{rpc::Status::kOK, ltANY}; if (params.isMember(jss::type)) { static constexpr auto kTypes = @@ -363,10 +363,10 @@ chooseLedgerEntryType(json::Value const& params) auto const& p = params[jss::type]; if (!p.isString()) { - result.first = RPC::Status{RpcInvalidParams, "Invalid field 'type', not string."}; + result.first = rpc::Status{RpcInvalidParams, "Invalid field 'type', not string."}; XRPL_ASSERT( - result.first.type() == RPC::Status::Type::ErrorCodeI, - "xrpl::RPC::chooseLedgerEntryType : first valid result type"); + result.first.type() == rpc::Status::Type::ErrorCodeI, + "xrpl::rpc::chooseLedgerEntryType : first valid result type"); return result; } @@ -379,10 +379,10 @@ chooseLedgerEntryType(json::Value const& params) }); if (iter == kTypes.end()) { - result.first = RPC::Status{RpcInvalidParams, "Invalid field 'type'."}; + result.first = rpc::Status{RpcInvalidParams, "Invalid field 'type'."}; XRPL_ASSERT( - result.first.type() == RPC::Status::Type::ErrorCodeI, - "xrpl::RPC::chooseLedgerEntryType : second valid result " + result.first.type() == rpc::Status::Type::ErrorCodeI, + "xrpl::rpc::chooseLedgerEntryType : second valid result " "type"); return result; } @@ -466,4 +466,4 @@ parseSubUnsubJson( return RpcSuccess; } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/RPCHelpers.h b/src/xrpld/rpc/detail/RPCHelpers.h index 881b758487..24c06021c0 100644 --- a/src/xrpld/rpc/detail/RPCHelpers.h +++ b/src/xrpld/rpc/detail/RPCHelpers.h @@ -27,7 +27,7 @@ namespace xrpl { class ReadView; -namespace RPC { +namespace rpc { struct JsonContext; @@ -85,7 +85,7 @@ parseAccountIds(json::Value const& jvArray); * std::nullopt on success. */ std::optional -readLimitField(unsigned int& limit, Tuning::LimitRange const& range, JsonContext const& context); +readLimitField(unsigned int& limit, tuning::LimitRange const& range, JsonContext const& context); /** * @brief Extracts a Seed from RPC parameters. @@ -123,7 +123,7 @@ parseXrplLibSeed(json::Value const& params); * @param params The JSON value containing RPC parameters. * @return A pair consisting of the RPC status and the chosen LedgerEntryType. */ -std::pair +std::pair chooseLedgerEntryType(json::Value const& params); /** @@ -172,6 +172,6 @@ parseSubUnsubJson( json::StaticString const& name, beast::Journal j); -} // namespace RPC +} // namespace rpc } // namespace xrpl diff --git a/src/xrpld/rpc/detail/RPCLedgerHelpers.cpp b/src/xrpld/rpc/detail/RPCLedgerHelpers.cpp index 6843c34b19..52e68e87f1 100644 --- a/src/xrpld/rpc/detail/RPCLedgerHelpers.cpp +++ b/src/xrpld/rpc/detail/RPCLedgerHelpers.cpp @@ -30,7 +30,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { namespace { @@ -40,7 +40,7 @@ isValidatedOld(LedgerMaster& ledgerMaster, bool standalone) if (standalone) return false; - return ledgerMaster.getValidatedLedgerAge() > Tuning::kMaxValidatedLedgerAge; + return ledgerMaster.getValidatedLedgerAge() > tuning::kMaxValidatedLedgerAge; } template @@ -282,19 +282,19 @@ getLedger(T& ledger, LedgerShortcut shortcut, Context const& context) return {RpcNotSynced, "notSynced"}; } - XRPL_ASSERT(!ledger->open(), "xrpl::RPC::getLedger : validated is not open"); + XRPL_ASSERT(!ledger->open(), "xrpl::rpc::getLedger : validated is not open"); } else { if (shortcut == LedgerShortcut::Current) { ledger = context.ledgerMaster.getCurrentLedger(); - XRPL_ASSERT(ledger->open(), "xrpl::RPC::getLedger : current is open"); + XRPL_ASSERT(ledger->open(), "xrpl::rpc::getLedger : current is open"); } else if (shortcut == LedgerShortcut::Closed) { ledger = context.ledgerMaster.getClosedLedger(); - XRPL_ASSERT(!ledger->open(), "xrpl::RPC::getLedger : closed is not open"); + XRPL_ASSERT(!ledger->open(), "xrpl::rpc::getLedger : closed is not open"); } else { @@ -386,7 +386,7 @@ lookupLedger(std::shared_ptr& ledger, JsonContext const& context } std::expected, json::Value> -getOrAcquireLedger(RPC::JsonContext const& context) +getOrAcquireLedger(rpc::JsonContext const& context) { auto const hasHash = context.params.isMember(jss::ledger_hash); auto const hasIndex = context.params.isMember(jss::ledger_index); @@ -398,7 +398,7 @@ getOrAcquireLedger(RPC::JsonContext const& context) if ((static_cast(hasHash) + static_cast(hasIndex)) != 1) { return std::unexpected( - RPC::makeParamError( + rpc::makeParamError( "Exactly one of 'ledger_hash' or " "'ledger_index' can be specified.")); } @@ -407,16 +407,16 @@ getOrAcquireLedger(RPC::JsonContext const& context) { auto const& jsonHash = context.params.get(jss::ledger_hash, json::ValueType::Null); if (!jsonHash.isString() || !ledgerHash.parseHex(jsonHash.asString())) - return std::unexpected(RPC::expectedFieldError(jss::ledger_hash, "hex string")); + return std::unexpected(rpc::expectedFieldError(jss::ledger_hash, "hex string")); } else { auto const& jsonIndex = context.params.get(jss::ledger_index, json::ValueType::Null); if (!jsonIndex.isInt() && !jsonIndex.isUInt()) - return std::unexpected(RPC::expectedFieldError(jss::ledger_index, "number")); + return std::unexpected(rpc::expectedFieldError(jss::ledger_index, "number")); // We need a validated ledger to get the hash from the sequence - if (ledgerMaster.getValidatedLedgerAge() > RPC::Tuning::kMaxValidatedLedgerAge) + if (ledgerMaster.getValidatedLedgerAge() > rpc::tuning::kMaxValidatedLedgerAge) { if (context.apiVersion == 1) return std::unexpected(rpcError(RpcNoCurrent)); @@ -427,9 +427,9 @@ getOrAcquireLedger(RPC::JsonContext const& context) auto ledger = ledgerMaster.getValidatedLedger(); if (ledgerIndex >= ledger->header().seq) - return std::unexpected(RPC::makeParamError("Ledger index too large")); + return std::unexpected(rpc::makeParamError("Ledger index too large")); if (ledgerIndex <= 0) - return std::unexpected(RPC::makeParamError("Ledger index too small")); + return std::unexpected(rpc::makeParamError("Ledger index too small")); auto const j = context.app.getJournal("RPCHandler"); // Try to get the hash of the desired ledger from the validated @@ -441,7 +441,7 @@ getOrAcquireLedger(RPC::JsonContext const& context) // ledger auto const refIndex = getCandidateLedger(ledgerIndex); auto refHash = hashOfSeq(*ledger, refIndex, j); - XRPL_ASSERT(refHash, "xrpl::RPC::getOrAcquireLedger : nonzero ledger hash"); + XRPL_ASSERT(refHash, "xrpl::rpc::getOrAcquireLedger : nonzero ledger hash"); // NOLINTBEGIN(bugprone-unchecked-optional-access) assert above ledger = ledgerMaster.getLedgerByHash(*refHash); @@ -453,7 +453,7 @@ getOrAcquireLedger(RPC::JsonContext const& context) if (auto il = context.app.getInboundLedgers().acquire( *refHash, refIndex, InboundLedger::Reason::GENERIC)) { - json::Value jvResult = RPC::makeError( + json::Value jvResult = rpc::makeError( RpcLgrNotFound, "acquiring ledger containing requested index"); jvResult[jss::acquiring] = getJson(LedgerFill(*il, &context)); return std::unexpected(jvResult); @@ -462,7 +462,7 @@ getOrAcquireLedger(RPC::JsonContext const& context) if (auto il = context.app.getInboundLedgers().find(*refHash)) // NOLINTEND(bugprone-unchecked-optional-access) { - json::Value jvResult = RPC::makeError( + json::Value jvResult = rpc::makeError( RpcLgrNotFound, "acquiring ledger containing requested index"); jvResult[jss::acquiring] = il->getJson(0); return std::unexpected(jvResult); @@ -474,7 +474,7 @@ getOrAcquireLedger(RPC::JsonContext const& context) neededHash = hashOfSeq(*ledger, ledgerIndex, j); } - XRPL_ASSERT(neededHash, "xrpl::RPC::getOrAcquireLedger : nonzero needed hash"); + XRPL_ASSERT(neededHash, "xrpl::rpc::getOrAcquireLedger : nonzero needed hash"); ledgerHash = neededHash ? *neededHash : beast::kZero; // kludge } @@ -494,7 +494,7 @@ getOrAcquireLedger(RPC::JsonContext const& context) return std::unexpected(il->getJson(0)); return std::unexpected( - RPC::makeError(RpcNotReady, "findCreate failed to return an inbound ledger")); + rpc::makeError(RpcNotReady, "findCreate failed to return an inbound ledger")); } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/RPCLedgerHelpers.h b/src/xrpld/rpc/detail/RPCLedgerHelpers.h index cbd47d38e6..cad5141917 100644 --- a/src/xrpld/rpc/detail/RPCLedgerHelpers.h +++ b/src/xrpld/rpc/detail/RPCLedgerHelpers.h @@ -21,7 +21,7 @@ namespace xrpl { class ReadView; class Transaction; -namespace RPC { +namespace rpc { struct JsonContext; @@ -172,8 +172,8 @@ ledgerFromSpecifier( * On failure, contains a json::Value describing the error. */ std::expected, json::Value> -getOrAcquireLedger(RPC::JsonContext const& context); +getOrAcquireLedger(rpc::JsonContext const& context); -} // namespace RPC +} // namespace rpc } // namespace xrpl diff --git a/src/xrpld/rpc/detail/RPCSub.cpp b/src/xrpld/rpc/detail/RPCSub.cpp index 92e9849939..8cea8b6bdd 100644 --- a/src/xrpld/rpc/detail/RPCSub.cpp +++ b/src/xrpld/rpc/detail/RPCSub.cpp @@ -75,7 +75,7 @@ public: } path_ = pUrl.path; - JLOG(j_.info()) << "RPCCall::fromNetwork sub: ip=" << ip_ << " port=" << port_ + JLOG(j_.info()) << "rpc_call::fromNetwork sub: ip=" << ip_ << " port=" << port_ << " ssl= " << (ssl_ ? "yes" : "no") << " path='" << path_ << "'"; } @@ -87,14 +87,14 @@ public: std::scoped_lock const sl(lock_); auto jm = broadcast ? j_.debug() : j_.info(); - JLOG(jm) << "RPCCall::fromNetwork push: " << jvObj; + JLOG(jm) << "rpc_call::fromNetwork push: " << jvObj; deque_.emplace_back(seq_++, jvObj); if (!sending_) { // Start a sending thread. - JLOG(j_.info()) << "RPCCall::fromNetwork start"; + JLOG(j_.info()) << "rpc_call::fromNetwork start"; sending_ = jobQueue_.addJob(JtClientSubscribe, "RPCSubSendThr", [this]() { sendThread(); }); @@ -156,9 +156,9 @@ private: // XXX Might not need this in a try. try { - JLOG(j_.info()) << "RPCCall::fromNetwork: " << ip_; + JLOG(j_.info()) << "rpc_call::fromNetwork: " << ip_; - RPCCall::fromNetwork( + rpc_call::fromNetwork( ioContext_, ip_, port_, @@ -173,7 +173,7 @@ private: } catch (std::exception const& e) { - JLOG(j_.info()) << "RPCCall::fromNetwork exception: " << e.what(); + JLOG(j_.info()) << "rpc_call::fromNetwork exception: " << e.what(); } } } while (bSend); diff --git a/src/xrpld/rpc/detail/Role.cpp b/src/xrpld/rpc/detail/Role.cpp index 68c5fcc484..34970b0580 100644 --- a/src/xrpld/rpc/detail/Role.cpp +++ b/src/xrpld/rpc/detail/Role.cpp @@ -40,7 +40,7 @@ passwordUnrequiredOrSentCorrect(Port const& port, json::Value const& params) bool ipAllowed( - beast::IP::Address const& remoteIp, + beast::ip::Address const& remoteIp, std::vector const& nets4, std::vector const& nets6) { @@ -78,7 +78,7 @@ ipAllowed( } bool -isAdmin(Port const& port, json::Value const& params, beast::IP::Address const& remoteIp) +isAdmin(Port const& port, json::Value const& params, beast::ip::Address const& remoteIp) { return ipAllowed(remoteIp, port.adminNetsV4, port.adminNetsV6) && passwordUnrequiredOrSentCorrect(port, params); @@ -89,7 +89,7 @@ requestRole( Role const& required, Port const& port, json::Value const& params, - beast::IP::Endpoint const& remoteIp, + beast::ip::Endpoint const& remoteIp, std::string_view user) { if (isAdmin(port, params, remoteIp.address())) @@ -122,16 +122,16 @@ isUnlimited( Role const& required, Port const& port, json::Value const& params, - beast::IP::Endpoint const& remoteIp, + beast::ip::Endpoint const& remoteIp, std::string const& user) { return isUnlimited(requestRole(required, port, params, remoteIp, user)); } -Resource::Consumer +resource::Consumer requestInboundEndpoint( - Resource::Manager& manager, - beast::IP::Endpoint const& remoteAddress, + resource::Manager& manager, + beast::ip::Endpoint const& remoteAddress, Role const& role, std::string_view user, std::string_view forwardedFor) diff --git a/src/xrpld/rpc/detail/ServerHandler.cpp b/src/xrpld/rpc/detail/ServerHandler.cpp index 768cbf0dc0..0181d5b10f 100644 --- a/src/xrpld/rpc/detail/ServerHandler.cpp +++ b/src/xrpld/rpc/detail/ServerHandler.cpp @@ -93,7 +93,7 @@ statusRequestResponse(http_request_type const& request, boost::beast::http::stat response msg; msg.version(request.version()); msg.result(status); - msg.insert("Server", BuildInfo::getFullVersionString()); + msg.insert("Server", build_info::getFullVersionString()); msg.insert("Content-Type", "text/html"); msg.insert("Connection", "close"); msg.body() = "Invalid protocol."; @@ -129,7 +129,7 @@ ServerHandler::ServerHandler( boost::asio::io_context& ioContext, JobQueue& jobQueue, NetworkOPs& networkOPs, - Resource::Manager& resourceManager, + resource::Manager& resourceManager, CollectorManager& cm) : app_(app) , resourceManager_(resourceManager) @@ -337,7 +337,7 @@ ServerHandler::onWSMessage( { json::Value jv; auto const size = boost::asio::buffer_size(buffers); - if (size > RPC::Tuning::kMaxRequestSize || !json::Reader{}.parse(jv, buffers) || !jv.isObject()) + if (size > rpc::tuning::kMaxRequestSize || !json::Reader{}.parse(jv, buffers) || !jv.isObject()) { json::Value jvResult(json::ValueType::Object); jvResult[jss::type] = jss::error; @@ -426,11 +426,11 @@ ServerHandler::processSession( // Requests without "command" are invalid. json::Value jr(json::ValueType::Object); - Resource::Charge loadType = Resource::kFeeReferenceRpc; + resource::Charge loadType = resource::kFeeReferenceRpc; try { - auto apiVersion = RPC::getAPIVersionNumber(jv, app_.config().betaRpcApi); - if (apiVersion == RPC::kApiInvalidVersion || + auto apiVersion = rpc::getAPIVersionNumber(jv, app_.config().betaRpcApi); + if (apiVersion == rpc::kApiInvalidVersion || (!jv.isMember(jss::command) && !jv.isMember(jss::method)) || (jv.isMember(jss::command) && !jv[jss::command].isString()) || (jv.isMember(jss::method) && !jv[jss::method].isString()) || @@ -439,7 +439,7 @@ ServerHandler::processSession( { jr[jss::type] = jss::response; jr[jss::status] = jss::error; - jr[jss::error] = apiVersion == RPC::kApiInvalidVersion ? jss::invalid_API_version + jr[jss::error] = apiVersion == rpc::kApiInvalidVersion ? jss::invalid_API_version : jss::missingCommand; jr[jss::request] = jv; if (jv.isMember(jss::id)) @@ -451,11 +451,11 @@ ServerHandler::processSession( if (jv.isMember(jss::api_version)) jr[jss::api_version] = jv[jss::api_version]; - is->getConsumer().charge(Resource::kFeeMalformedRpc); + is->getConsumer().charge(resource::kFeeMalformedRpc); return jr; } - auto required = RPC::roleRequired( + auto required = rpc::roleRequired( apiVersion, app_.config().betaRpcApi, jv.isMember(jss::command) ? jv[jss::command].asString() : jv[jss::method].asString()); @@ -463,16 +463,16 @@ ServerHandler::processSession( required, session->port(), jv, - beast::IP::fromAsio(session->remoteEndpoint().address()), + beast::ip::fromAsio(session->remoteEndpoint().address()), is->user()); if (Role::FORBID == role) { - loadType = Resource::kFeeMalformedRpc; + loadType = resource::kFeeMalformedRpc; jr[jss::result] = rpcError(RpcForbidden); } else { - RPC::JsonContext context{ + rpc::JsonContext context{ {.j = app_.getJournal("RPCHandler"), .app = app_, .loadType = loadType, @@ -487,7 +487,7 @@ ServerHandler::processSession( {.user = is->user(), .forwardedFor = is->forwardedFor()}}; auto start = std::chrono::system_clock::now(); - RPC::doCommand(context, jr[jss::result]); + rpc::doCommand(context, jr[jss::result]); auto end = std::chrono::system_clock::now(); logDuration(jv, end - start, journal_); } @@ -495,7 +495,7 @@ ServerHandler::processSession( catch (std::exception const& ex) { // LCOV_EXCL_START - jr[jss::result] = RPC::makeError(RpcInternal); + jr[jss::result] = rpc::makeError(RpcInternal); JLOG(journal_.error()) << "Exception while processing WS: " << ex.what() << "\n" << "Input JSON: " << json::Compact{json::Value{jv}}; // LCOV_EXCL_STOP @@ -601,7 +601,7 @@ void ServerHandler::processRequest( Port const& port, std::string const& request, - beast::IP::Endpoint const& remoteIPAddress, + beast::ip::Endpoint const& remoteIPAddress, Output const& output, std::shared_ptr coro, std::string_view forwardedFor, @@ -612,7 +612,7 @@ ServerHandler::processRequest( json::Value jsonOrig; { json::Reader reader; - if ((request.size() > RPC::Tuning::kMaxRequestSize) || !reader.parse(request, jsonOrig) || + if ((request.size() > rpc::tuning::kMaxRequestSize) || !reader.parse(request, jsonOrig) || !jsonOrig || !jsonOrig.isObject()) { httpReply( @@ -652,21 +652,21 @@ ServerHandler::processRequest( continue; } - unsigned apiVersion = RPC::kApiVersionIfUnspecified; + unsigned apiVersion = rpc::kApiVersionIfUnspecified; if (jsonRPC.isMember(jss::params) && jsonRPC[jss::params].isArray() && jsonRPC[jss::params].size() > 0 && jsonRPC[jss::params][0u].isObject()) { - apiVersion = RPC::getAPIVersionNumber( + apiVersion = rpc::getAPIVersionNumber( jsonRPC[jss::params][json::UInt(0)], app_.config().betaRpcApi); } - if (apiVersion == RPC::kApiVersionIfUnspecified && batch) + if (apiVersion == rpc::kApiVersionIfUnspecified && batch) { // for batch request, api_version may be at a different level - apiVersion = RPC::getAPIVersionNumber(jsonRPC, app_.config().betaRpcApi); + apiVersion = rpc::getAPIVersionNumber(jsonRPC, app_.config().betaRpcApi); } - if (apiVersion == RPC::kApiInvalidVersion) + if (apiVersion == rpc::kApiInvalidVersion) { if (!batch) { @@ -685,7 +685,7 @@ ServerHandler::processRequest( auto required = Role::FORBID; if (jsonRPC.isMember(jss::method) && jsonRPC[jss::method].isString()) { - required = RPC::roleRequired( + required = rpc::roleRequired( apiVersion, app_.config().betaRpcApi, jsonRPC[jss::method].asString()); } @@ -700,7 +700,7 @@ ServerHandler::processRequest( role = requestRole(required, port, json::ValueType::Object, remoteIPAddress, user); } - Resource::Consumer usage; + resource::Consumer usage; if (isUnlimited(role)) { usage = resourceManager_.newUnlimitedEndpoint(remoteIPAddress); @@ -725,7 +725,7 @@ ServerHandler::processRequest( if (role == Role::FORBID) { - usage.charge(Resource::kFeeMalformedRpc); + usage.charge(resource::kFeeMalformedRpc); if (!batch) { httpReply(403, "Forbidden", output, rpcJ); @@ -739,7 +739,7 @@ ServerHandler::processRequest( if (!jsonRPC.isMember(jss::method) || jsonRPC[jss::method].isNull()) { - usage.charge(Resource::kFeeMalformedRpc); + usage.charge(resource::kFeeMalformedRpc); if (!batch) { httpReply(400, "Null method", output, rpcJ); @@ -754,7 +754,7 @@ ServerHandler::processRequest( json::Value const& method = jsonRPC[jss::method]; if (!method.isString()) { - usage.charge(Resource::kFeeMalformedRpc); + usage.charge(resource::kFeeMalformedRpc); if (!batch) { httpReply(400, "method is not string", output, rpcJ); @@ -769,7 +769,7 @@ ServerHandler::processRequest( std::string const strMethod = method.asString(); if (strMethod.empty()) { - usage.charge(Resource::kFeeMalformedRpc); + usage.charge(resource::kFeeMalformedRpc); if (!batch) { httpReply(400, "method is empty", output, rpcJ); @@ -797,7 +797,7 @@ ServerHandler::processRequest( } else if (!params.isArray() || params.size() != 1) { - usage.charge(Resource::kFeeMalformedRpc); + usage.charge(resource::kFeeMalformedRpc); httpReply(400, "params unparsable", output, rpcJ); return; } @@ -806,7 +806,7 @@ ServerHandler::processRequest( params = std::move(params[0u]); if (!params.isObjectOrNull()) { - usage.charge(Resource::kFeeMalformedRpc); + usage.charge(resource::kFeeMalformedRpc); httpReply(400, "params unparsable", output, rpcJ); return; } @@ -822,7 +822,7 @@ ServerHandler::processRequest( { if (!params[jss::ripplerpc].isString()) { - usage.charge(Resource::kFeeMalformedRpc); + usage.charge(resource::kFeeMalformedRpc); if (!batch) { httpReply(400, "ripplerpc is not a string", output, rpcJ); @@ -853,9 +853,9 @@ ServerHandler::processRequest( params[jss::command] = strMethod; JLOG(journal_.trace()) << "doRpcCommand:" << strMethod << ":" << params; - Resource::Charge loadType = Resource::kFeeReferenceRpc; + resource::Charge loadType = resource::kFeeReferenceRpc; - RPC::JsonContext context{ + rpc::JsonContext context{ {.j = journal_, .app = app_, .loadType = loadType, @@ -874,12 +874,12 @@ ServerHandler::processRequest( try { - RPC::doCommand(context, result); + rpc::doCommand(context, result); } catch (std::exception const& ex) { // LCOV_EXCL_START - result = RPC::makeError(RpcInternal); + result = rpc::makeError(RpcInternal); JLOG(journal_.error()) << "Internal error : " << ex.what() << " when processing request: " << json::Compact{json::Value{params}}; @@ -984,7 +984,7 @@ ServerHandler::processRequest( reply[jss::error][jss::error_code].isInt()) { int const errCode = reply[jss::error][jss::error_code].asInt(); - return RPC::errorCodeHttpStatus(static_cast(errCode)); + return rpc::errorCodeHttpStatus(static_cast(errCode)); } } // Return OK. @@ -1043,7 +1043,7 @@ ServerHandler::statusResponse(http_request_type const& request) const msg.body() = "Server cannot accept clients: " + reason + ""; } msg.version(request.version()); - msg.insert("Server", BuildInfo::getFullVersionString()); + msg.insert("Server", build_info::getFullVersionString()); msg.insert("Content-Type", "text/html"); msg.insert("Connection", "close"); msg.prepare_payload(); @@ -1208,7 +1208,7 @@ setupClient(ServerHandler::Setup& setup) if (iter == setup.ports.cend()) return; setup.client.secure = iter->protocol.contains("https"); - if (beast::IP::isUnspecified(iter->ip)) + if (beast::ip::isUnspecified(iter->ip)) { // VFALCO HACK! to make localhost work setup.client.ip = iter->ip.is_v6() ? "::1" : "127.0.0.1"; @@ -1256,7 +1256,7 @@ makeServerHandler( boost::asio::io_context& ioContext, JobQueue& jobQueue, NetworkOPs& networkOPs, - Resource::Manager& resourceManager, + resource::Manager& resourceManager, CollectorManager& cm) { return std::make_unique( diff --git a/src/xrpld/rpc/detail/Status.cpp b/src/xrpld/rpc/detail/Status.cpp index 58d2f8cb80..147f2b31e0 100644 --- a/src/xrpld/rpc/detail/Status.cpp +++ b/src/xrpld/rpc/detail/Status.cpp @@ -9,7 +9,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { std::string Status::codeString() const @@ -25,7 +25,7 @@ Status::codeString() const std::string s1, s2; [[maybe_unused]] auto const success = transResultInfo(toTER(), s1, s2); - XRPL_ASSERT(success, "xrpl::RPC::codeString : valid TER result"); + XRPL_ASSERT(success, "xrpl::rpc::codeString : valid TER result"); return s1 + ": " + s2; } @@ -39,7 +39,7 @@ Status::codeString() const } // LCOV_EXCL_START - UNREACHABLE("xrpl::RPC::codeString : invalid type"); + UNREACHABLE("xrpl::rpc::codeString : invalid type"); return ""; // LCOV_EXCL_STOP } @@ -85,4 +85,4 @@ Status::toString() const return ""; } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/TransactionSign.cpp b/src/xrpld/rpc/detail/TransactionSign.cpp index e1c5180b5c..9c97577b27 100644 --- a/src/xrpld/rpc/detail/TransactionSign.cpp +++ b/src/xrpld/rpc/detail/TransactionSign.cpp @@ -64,7 +64,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { namespace detail { // Used to pass extra parameters used when returning a @@ -218,7 +218,7 @@ checkPayment( { if (txJson[jss::DeliverMax] != txJson[jss::Amount]) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Cannot specify differing 'Amount' and 'DeliverMax'"); } } @@ -231,31 +231,31 @@ checkPayment( } if (!txJson.isMember(jss::Amount)) - return RPC::missingFieldError("tx_json.Amount"); + return rpc::missingFieldError("tx_json.Amount"); STAmount amount; if (!amountFromJsonNoThrow(amount, txJson[jss::Amount])) - return RPC::invalidFieldError("tx_json.Amount"); + return rpc::invalidFieldError("tx_json.Amount"); if (!txJson.isMember(jss::Destination)) - return RPC::missingFieldError("tx_json.Destination"); + return rpc::missingFieldError("tx_json.Destination"); auto const dstAccountID = parseBase58(txJson[jss::Destination].asString()); if (!dstAccountID) - return RPC::invalidFieldError("tx_json.Destination"); + return rpc::invalidFieldError("tx_json.Destination"); if (params.isMember(jss::build_path) && (!doPath || (!app.getOpenLedger().current()->rules().enabled(featureMPTokensV2) && amount.holds()))) { - return RPC::makeError(RpcInvalidParams, "Field 'build_path' not allowed in this context."); + return rpc::makeError(RpcInvalidParams, "Field 'build_path' not allowed in this context."); } if (txJson.isMember(jss::Paths) && params.isMember(jss::build_path)) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "Cannot specify both 'tx_json.Paths' and 'build_path'"); } @@ -266,7 +266,7 @@ checkPayment( if (!txJson[sfDomainID.jsonName].isString() || !num.parseHex(txJson[sfDomainID.jsonName].asString())) { - return RPC::makeError(RpcDomainMalformed, "Unable to parse 'DomainID'."); + return rpc::makeError(RpcDomainMalformed, "Unable to parse 'DomainID'."); } domain = num; @@ -279,7 +279,7 @@ checkPayment( if (txJson.isMember(jss::SendMax)) { if (!amountFromJsonNoThrow(sendMax, txJson[jss::SendMax])) - return RPC::invalidFieldError("tx_json.SendMax"); + return rpc::invalidFieldError("tx_json.SendMax"); } else { @@ -291,7 +291,7 @@ checkPayment( } if (sendMax.native() && amount.native()) - return RPC::makeError(RpcInvalidParams, "Cannot build XRP to XRP paths."); + return rpc::makeError(RpcInvalidParams, "Cannot build XRP to XRP paths."); { LegacyPathFind const lpf(isUnlimited(role), app); @@ -357,19 +357,19 @@ checkTxJsonFields( if (!txJson.isObject()) { - ret.first = RPC::objectFieldError(jss::tx_json); + ret.first = rpc::objectFieldError(jss::tx_json); return ret; } if (!txJson.isMember(jss::TransactionType)) { - ret.first = RPC::missingFieldError("tx_json.TransactionType"); + ret.first = rpc::missingFieldError("tx_json.TransactionType"); return ret; } if (!txJson.isMember(jss::Account)) { - ret.first = RPC::makeError(RpcSrcActMissing, RPC::missingFieldMessage("tx_json.Account")); + ret.first = rpc::makeError(RpcSrcActMissing, rpc::missingFieldMessage("tx_json.Account")); return ret; } @@ -377,12 +377,12 @@ checkTxJsonFields( if (!srcAddressID) { - ret.first = RPC::makeError(RpcSrcActMalformed, RPC::invalidFieldMessage("tx_json.Account")); + ret.first = rpc::makeError(RpcSrcActMalformed, rpc::invalidFieldMessage("tx_json.Account")); return ret; } // Check for current ledger. - if (verify && !config.standalone() && (validatedLedgerAge > Tuning::kMaxValidatedLedgerAge)) + if (verify && !config.standalone() && (validatedLedgerAge > tuning::kMaxValidatedLedgerAge)) { if (apiVersion == 1) { @@ -415,12 +415,12 @@ checkNetworkID(json::Value const& txJson, uint32_t appNetworkId) if (!txJson.isMember(jss::NetworkID)) { return std::unexpected( - RPC::makeError(RpcInvalidParams, RPC::missingFieldMessage("tx_json.NetworkID"))); + rpc::makeError(RpcInvalidParams, rpc::missingFieldMessage("tx_json.NetworkID"))); } if (!txJson[jss::NetworkID].isIntegral() || txJson[jss::NetworkID].asUInt() != appNetworkId) { return std::unexpected( - RPC::makeError(RpcInvalidParams, RPC::invalidFieldMessage("tx_json.NetworkID"))); + rpc::makeError(RpcInvalidParams, rpc::invalidFieldMessage("tx_json.NetworkID"))); } } return std::expected(); @@ -490,13 +490,13 @@ transactionPreProcessImpl( { if (signatureTemplate == nullptr) { // Invalid target field - return RPC::makeError(RpcInvalidParams, signatureTarget->get().getName()); + return rpc::makeError(RpcInvalidParams, signatureTarget->get().getName()); } signingArgs.setSignatureTarget(signatureTarget); } if (!params.isMember(jss::tx_json)) - return RPC::missingFieldError(jss::tx_json); + return rpc::missingFieldError(jss::tx_json); json::Value& txJson(params[jss::tx_json]); @@ -510,14 +510,14 @@ transactionPreProcessImpl( app.getFeeTrack(), getAPIVersionNumber(params, app.config().betaRpcApi)); - if (RPC::containsError(txJsonResult)) + if (rpc::containsError(txJsonResult)) return std::move(txJsonResult); // This test covers the case where we're offline so the sequence number // cannot be determined locally. If we're offline then the caller must // provide the sequence number. if (!verify && !txJson.isMember(jss::Sequence)) - return RPC::missingFieldError("tx_json.Sequence"); + return rpc::missingFieldError("tx_json.Sequence"); SLE::const_pointer sle; if (verify) @@ -565,7 +565,7 @@ transactionPreProcessImpl( app.getTxQ(), app); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; } @@ -573,7 +573,7 @@ transactionPreProcessImpl( json::Value err = checkPayment( params, txJson, srcAddressID, role, app, verify && signingArgs.editFields()); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; } @@ -614,8 +614,8 @@ transactionPreProcessImpl( if (!ptrDelegatedAddressID) { - return RPC::makeError( - RpcSrcActMalformed, RPC::invalidFieldMessage("tx_json.Delegate")); + return rpc::makeError( + RpcSrcActMalformed, rpc::invalidFieldMessage("tx_json.Delegate")); } auto delegatedAddressID = *ptrDelegatedAddressID; @@ -672,17 +672,17 @@ transactionPreProcessImpl( } catch (STObject::FieldErr const& err) { - return RPC::makeError(RpcInvalidParams, err.what()); + return rpc::makeError(RpcInvalidParams, err.what()); } catch (std::exception&) { - return RPC::makeError( + return rpc::makeError( RpcInternal, "Exception occurred constructing serialized transaction"); } std::string reason; if (!passesLocalChecks(*stTx, reason)) - return RPC::makeError(RpcInvalidParams, reason); + return rpc::makeError(RpcInvalidParams, reason); // If multisign then return multiSignature, else set TxnSignature field. if (signingArgs.isMultiSigning()) @@ -716,7 +716,7 @@ transactionConstructImpl( tpTrans = std::make_shared(stTx, reason, app); if (tpTrans->getStatus() != TransStatus::NEW) { - ret.first = RPC::makeError(RpcInternal, "Unable to construct transaction: " + reason); + ret.first = rpc::makeError(RpcInternal, "Unable to construct transaction: " + reason); return ret; } } @@ -741,7 +741,7 @@ transactionConstructImpl( } if (checkValidity(app.getHashRouter(), *sttxNew, rules).first != Validity::Valid) { - ret.first = RPC::makeError(RpcInternal, "Invalid signature."); + ret.first = rpc::makeError(RpcInternal, "Invalid signature."); return ret; } @@ -766,7 +766,7 @@ transactionConstructImpl( if (!tpTrans) { - ret.first = RPC::makeError(RpcInternal, "Unable to sterilize transaction."); + ret.first = rpc::makeError(RpcInternal, "Unable to sterilize transaction."); return ret; } ret.second = std::move(tpTrans); @@ -789,7 +789,7 @@ transactionFormatResultImpl(Transaction::pointer tpTrans, unsigned apiVersion) jvResult[jss::tx_json] = tpTrans->getJson(JsonOptions::Values::None); } - RPC::insertDeliverMax( + rpc::insertDeliverMax( jvResult[jss::tx_json], tpTrans->getSTransaction()->getTxnType(), apiVersion); jvResult[jss::tx_blob] = strHex(tpTrans->getSTransaction()->getSerializer().peekData()); @@ -808,7 +808,7 @@ transactionFormatResultImpl(Transaction::pointer tpTrans, unsigned apiVersion) } catch (std::exception&) { - jvResult = RPC::makeError(RpcInternal, "Exception occurred during JSON handling."); + jvResult = rpc::makeError(RpcInternal, "Exception occurred during JSON handling."); } return jvResult; } @@ -922,7 +922,7 @@ getCurrentNetworkFee( { std::stringstream ss; ss << "Fee of " << fee << " exceeds the requested tx limit of " << *limit; - return RPC::makeError(RpcHighFee, ss.str()); + return rpc::makeError(RpcHighFee, ss.str()); } return fee.jsonClipped(); @@ -943,10 +943,10 @@ checkFee( return json::Value(); if (!doAutoFill) - return RPC::missingFieldError("tx_json.Fee"); + return rpc::missingFieldError("tx_json.Fee"); - int mult = Tuning::kDefaultAutoFillFeeMultiplier; - int div = Tuning::kDefaultAutoFillFeeDivisor; + int mult = tuning::kDefaultAutoFillFeeMultiplier; + int div = tuning::kDefaultAutoFillFeeDivisor; if (request.isMember(jss::fee_mult_max)) { if (request[jss::fee_mult_max].isInt()) @@ -954,15 +954,15 @@ checkFee( mult = request[jss::fee_mult_max].asInt(); if (mult < 0) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, - RPC::expectedFieldMessage(jss::fee_mult_max, "a positive integer")); + rpc::expectedFieldMessage(jss::fee_mult_max, "a positive integer")); } } else { - return RPC::makeError( - RpcHighFee, RPC::expectedFieldMessage(jss::fee_mult_max, "a positive integer")); + return rpc::makeError( + RpcHighFee, rpc::expectedFieldMessage(jss::fee_mult_max, "a positive integer")); } } if (request.isMember(jss::fee_div_max)) @@ -972,15 +972,15 @@ checkFee( div = request[jss::fee_div_max].asInt(); if (div <= 0) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, - RPC::expectedFieldMessage(jss::fee_div_max, "a positive integer")); + rpc::expectedFieldMessage(jss::fee_div_max, "a positive integer")); } } else { - return RPC::makeError( - RpcHighFee, RPC::expectedFieldMessage(jss::fee_div_max, "a positive integer")); + return rpc::makeError( + RpcHighFee, rpc::expectedFieldMessage(jss::fee_div_max, "a positive integer")); } } @@ -1071,7 +1071,7 @@ transactionSubmit( } catch (std::exception&) { - return RPC::makeError(RpcInternal, "Exception occurred during transaction submission."); + return rpc::makeError(RpcInternal, "Exception occurred during transaction submission."); } return transactionFormatResultImpl(txn.second, apiVersion); @@ -1084,21 +1084,21 @@ static json::Value checkMultiSignFields(json::Value const& jvRequest) { if (!jvRequest.isMember(jss::tx_json)) - return RPC::missingFieldError(jss::tx_json); + return rpc::missingFieldError(jss::tx_json); json::Value const& txJson(jvRequest[jss::tx_json]); if (!txJson.isObject()) - return RPC::invalidFieldMessage(jss::tx_json); + return rpc::invalidFieldMessage(jss::tx_json); // There are a couple of additional fields we need to check before // we serialize. If we serialize first then we generate less useful // error messages. if (!txJson.isMember(jss::Sequence)) - return RPC::missingFieldError("tx_json.Sequence"); + return rpc::missingFieldError("tx_json.Sequence"); if (!txJson.isMember(sfSigningPubKey.getJsonName())) - return RPC::missingFieldError("tx_json.SigningPubKey"); + return rpc::missingFieldError("tx_json.SigningPubKey"); // Multi-signing into a signature_target object field is fine, // because it means the signature is not for the transaction @@ -1106,7 +1106,7 @@ checkMultiSignFields(json::Value const& jvRequest) if (!jvRequest.isMember(jss::signature_target) && !txJson[sfSigningPubKey.getJsonName()].asString().empty()) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "When multi-signing 'tx_json.SigningPubKey' must be empty."); } @@ -1120,7 +1120,7 @@ static json::Value sortAndValidateSigners(STArray& signers, AccountID const& signingForID) { if (signers.empty()) - return RPC::makeParamError("Signers array may not be empty."); + return rpc::makeParamError("Signers array may not be empty."); // Signers must be sorted by Account. std::ranges::sort(signers, [](STObject const& a, STObject const& b) { @@ -1137,7 +1137,7 @@ sortAndValidateSigners(STArray& signers, AccountID const& signingForID) std::ostringstream err; err << "Duplicate Signers:Signer:Account entries (" << toBase58((*dupIter)[sfAccount]) << ") are not allowed."; - return RPC::makeParamError(err.str()); + return rpc::makeParamError(err.str()); } // An account may not sign for itself. @@ -1147,7 +1147,7 @@ sortAndValidateSigners(STArray& signers, AccountID const& signingForID) { std::ostringstream err; err << "A Signer may not be the transaction's Account (" << toBase58(signingForID) << ")."; - return RPC::makeParamError(err.str()); + return rpc::makeParamError(err.str()); } return {}; } @@ -1174,23 +1174,23 @@ transactionSignFor( char const accountField[] = "account"; if (!jvRequest.isMember(accountField)) - return RPC::missingFieldError(accountField); + return rpc::missingFieldError(accountField); // Turn the signer's account into an AccountID for multi-sign. auto const signerAccountID = parseBase58(jvRequest[accountField].asString()); if (!signerAccountID) { - return RPC::makeError(RpcSrcActMalformed, RPC::invalidFieldMessage(accountField)); + return rpc::makeError(RpcSrcActMalformed, rpc::invalidFieldMessage(accountField)); } if (!jvRequest.isMember(jss::tx_json)) - return RPC::missingFieldError(jss::tx_json); + return rpc::missingFieldError(jss::tx_json); { json::Value& txJson(jvRequest[jss::tx_json]); if (!txJson.isObject()) - return RPC::objectFieldError(jss::tx_json); + return rpc::objectFieldError(jss::tx_json); if (auto checkResult = detail::checkNetworkID(txJson, app.getNetworkIDService().getNetworkID()); @@ -1211,7 +1211,7 @@ transactionSignFor( using namespace detail; { json::Value err = checkMultiSignFields(jvRequest); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; } @@ -1225,7 +1225,7 @@ transactionSignFor( return preprocResult.first; XRPL_ASSERT( - signForParams.validMultiSign(), "xrpl::RPC::transactionSignFor : valid multi-signature"); + signForParams.validMultiSign(), "xrpl::rpc::transactionSignFor : valid multi-signature"); { SLE::const_pointer const accountState = ledger->read(keylet::account(*signerAccountID)); @@ -1263,7 +1263,7 @@ transactionSignFor( // For delegated transactions, the delegate account is // the one forbidden from appearing in its own Signers array. auto err = sortAndValidateSigners(signers, sttx->getInitiator()); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; } @@ -1299,7 +1299,7 @@ transactionSubmitMultiSigned( using namespace detail; { json::Value err = checkMultiSignFields(jvRequest); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; } @@ -1314,7 +1314,7 @@ transactionSubmitMultiSigned( app.getFeeTrack(), getAPIVersionNumber(jvRequest, app.config().betaRpcApi)); - if (RPC::containsError(txJsonResult)) + if (rpc::containsError(txJsonResult)) return std::move(txJsonResult); SLE::const_pointer const sle = ledger->read(keylet::account(srcAddressID)); @@ -1332,12 +1332,12 @@ transactionSubmitMultiSigned( json::Value err = checkFee(jvRequest, role, false, app.config(), app.getFeeTrack(), app.getTxQ(), app); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; err = checkPayment(jvRequest, txJson, srcAddressID, role, app, false); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; } @@ -1359,17 +1359,17 @@ transactionSubmitMultiSigned( } catch (STObject::FieldErr const& err) { - return RPC::makeError(RpcInvalidParams, err.what()); + return rpc::makeError(RpcInvalidParams, err.what()); } catch (std::exception& ex) { std::string const reason(ex.what()); - return RPC::makeError( + return rpc::makeError( RpcInternal, "Exception while serializing transaction: " + reason); } std::string reason; if (!passesLocalChecks(*stTx, reason)) - return RPC::makeError(RpcInvalidParams, reason); + return rpc::makeError(RpcInvalidParams, reason); } // Validate the fields in the serialized transaction. @@ -1383,7 +1383,7 @@ transactionSubmitMultiSigned( std::ostringstream err; err << "Invalid " << sfSigningPubKey.fieldName << " field. Field must be empty when multi-signing."; - return RPC::makeError(RpcInvalidParams, err.str()); + return rpc::makeError(RpcInvalidParams, err.str()); } // There may not be a TxnSignature field. @@ -1397,26 +1397,26 @@ transactionSubmitMultiSigned( { std::ostringstream err; err << "Invalid " << sfFee.fieldName << " field. Fees must be specified in XRP."; - return RPC::makeError(RpcInvalidParams, err.str()); + return rpc::makeError(RpcInvalidParams, err.str()); } if (fee <= STAmount{0}) { std::ostringstream err; err << "Invalid " << sfFee.fieldName << " field. Fees must be greater than zero."; - return RPC::makeError(RpcInvalidParams, err.str()); + return rpc::makeError(RpcInvalidParams, err.str()); } } // Verify that the Signers field is present. if (!stTx->isFieldPresent(sfSigners)) - return RPC::missingFieldError("tx_json.Signers"); + return rpc::missingFieldError("tx_json.Signers"); // If the Signers field is present the SField guarantees it to be an array. // Get a reference to the Signers array so we can verify and sort it. auto& signers = stTx->peekFieldArray(sfSigners); if (signers.empty()) - return RPC::makeParamError("tx_json.Signers array may not be empty."); + return rpc::makeParamError("tx_json.Signers array may not be empty."); // The Signers array may only contain Signer objects. if (std::ranges::find_if_not(signers, [](STObject const& obj) { @@ -1427,14 +1427,14 @@ transactionSubmitMultiSigned( obj.isFieldPresent(sfTxnSignature) && obj.getCount() == 3); }) != signers.end()) { - return RPC::makeParamError("Signers array may only contain Signer entries."); + return rpc::makeParamError("Signers array may only contain Signer entries."); } // The array must be sorted and validated. // For delegated transactions, getInitiator() returns sfDelegate, // that account is the one forbidden from appearing in its own Signers array. auto err = sortAndValidateSigners(signers, stTx->getInitiator()); - if (RPC::containsError(err)) + if (rpc::containsError(err)) return err; // Make sure the SerializedTransaction makes a legitimate Transaction. @@ -1452,10 +1452,10 @@ transactionSubmitMultiSigned( } catch (std::exception&) { - return RPC::makeError(RpcInternal, "Exception occurred during transaction submission."); + return rpc::makeError(RpcInternal, "Exception occurred during transaction submission."); } return transactionFormatResultImpl(txn.second, apiVersion); } -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/detail/TransactionSign.h b/src/xrpld/rpc/detail/TransactionSign.h index dcb417dd16..9b07cbde20 100644 --- a/src/xrpld/rpc/detail/TransactionSign.h +++ b/src/xrpld/rpc/detail/TransactionSign.h @@ -20,7 +20,7 @@ class LoadFeeTrack; class Transaction; class TxQ; -namespace RPC { +namespace rpc { json::Value getCurrentNetworkFee( @@ -30,8 +30,8 @@ getCurrentNetworkFee( TxQ const& txQ, Application const& app, json::Value const& tx, - int mult = Tuning::kDefaultAutoFillFeeMultiplier, - int div = Tuning::kDefaultAutoFillFeeDivisor); + int mult = tuning::kDefaultAutoFillFeeMultiplier, + int div = tuning::kDefaultAutoFillFeeDivisor); /** * Fill in the fee on behalf of the client. @@ -140,5 +140,5 @@ transactionSubmitMultiSigned( Application& app, ProcessTransactionFn const& processTransaction); -} // namespace RPC +} // namespace rpc } // namespace xrpl diff --git a/src/xrpld/rpc/detail/Tuning.h b/src/xrpld/rpc/detail/Tuning.h index b904822698..5c47ab4365 100644 --- a/src/xrpld/rpc/detail/Tuning.h +++ b/src/xrpld/rpc/detail/Tuning.h @@ -6,7 +6,7 @@ * Tuned constants. */ /** @{ */ -namespace xrpl::RPC::Tuning { +namespace xrpl::rpc::tuning { /** * Represents RPC limit parameter values that have a min, default and max. @@ -98,5 +98,5 @@ static constexpr int kMaxSrcCur = 18; */ static constexpr int kMaxAutoSrcCur = 88; -} // namespace xrpl::RPC::Tuning +} // namespace xrpl::rpc::tuning /** @} */ diff --git a/src/xrpld/rpc/handlers/ChannelVerify.cpp b/src/xrpld/rpc/handlers/ChannelVerify.cpp index 64f616e829..ab54745a5d 100644 --- a/src/xrpld/rpc/handlers/ChannelVerify.cpp +++ b/src/xrpld/rpc/handlers/ChannelVerify.cpp @@ -27,13 +27,13 @@ namespace xrpl { // signature: signature to verify // } json::Value -doChannelVerify(RPC::JsonContext& context) +doChannelVerify(rpc::JsonContext& context) { auto const& params(context.params); for (auto const& p : {jss::public_key, jss::channel_id, jss::amount, jss::signature}) { if (!params.isMember(p)) - return RPC::missingFieldError(p); + return rpc::missingFieldError(p); } std::optional pk; diff --git a/src/xrpld/rpc/handlers/Handlers.h b/src/xrpld/rpc/handlers/Handlers.h index 7b347b2ecc..d192e8726c 100644 --- a/src/xrpld/rpc/handlers/Handlers.h +++ b/src/xrpld/rpc/handlers/Handlers.h @@ -7,147 +7,147 @@ namespace xrpl { json::Value -doAccountCurrencies(RPC::JsonContext&); +doAccountCurrencies(rpc::JsonContext&); json::Value -doAccountInfo(RPC::JsonContext&); +doAccountInfo(rpc::JsonContext&); json::Value -doAccountLines(RPC::JsonContext&); +doAccountLines(rpc::JsonContext&); json::Value -doAccountChannels(RPC::JsonContext&); +doAccountChannels(rpc::JsonContext&); json::Value -doAccountNFTs(RPC::JsonContext&); +doAccountNFTs(rpc::JsonContext&); json::Value -doAccountObjects(RPC::JsonContext&); +doAccountObjects(rpc::JsonContext&); json::Value -doAccountOffers(RPC::JsonContext&); +doAccountOffers(rpc::JsonContext&); json::Value -doAccountTx(RPC::JsonContext&); +doAccountTx(rpc::JsonContext&); json::Value -doAMMInfo(RPC::JsonContext&); +doAMMInfo(rpc::JsonContext&); json::Value -doBookOffers(RPC::JsonContext&); +doBookOffers(rpc::JsonContext&); json::Value -doBookChanges(RPC::JsonContext&); +doBookChanges(rpc::JsonContext&); json::Value -doBlackList(RPC::JsonContext&); +doBlackList(rpc::JsonContext&); json::Value -doCanDelete(RPC::JsonContext&); +doCanDelete(rpc::JsonContext&); json::Value -doChannelAuthorize(RPC::JsonContext&); +doChannelAuthorize(rpc::JsonContext&); json::Value -doChannelVerify(RPC::JsonContext&); +doChannelVerify(rpc::JsonContext&); json::Value -doConnect(RPC::JsonContext&); +doConnect(rpc::JsonContext&); json::Value -doConsensusInfo(RPC::JsonContext&); +doConsensusInfo(rpc::JsonContext&); json::Value -doDepositAuthorized(RPC::JsonContext&); +doDepositAuthorized(rpc::JsonContext&); json::Value -doFeature(RPC::JsonContext&); +doFeature(rpc::JsonContext&); json::Value -doFee(RPC::JsonContext&); +doFee(rpc::JsonContext&); json::Value -doFetchInfo(RPC::JsonContext&); +doFetchInfo(rpc::JsonContext&); json::Value -doGatewayBalances(RPC::JsonContext&); +doGatewayBalances(rpc::JsonContext&); json::Value -doGetCounts(RPC::JsonContext&); +doGetCounts(rpc::JsonContext&); json::Value -doGetAggregatePrice(RPC::JsonContext&); +doGetAggregatePrice(rpc::JsonContext&); json::Value -doLedgerAccept(RPC::JsonContext&); +doLedgerAccept(rpc::JsonContext&); json::Value -doLedgerCleaner(RPC::JsonContext&); +doLedgerCleaner(rpc::JsonContext&); json::Value -doLedgerClosed(RPC::JsonContext&); +doLedgerClosed(rpc::JsonContext&); json::Value -doLedgerCurrent(RPC::JsonContext&); +doLedgerCurrent(rpc::JsonContext&); json::Value -doLedgerData(RPC::JsonContext&); +doLedgerData(rpc::JsonContext&); json::Value -doLedgerEntry(RPC::JsonContext&); +doLedgerEntry(rpc::JsonContext&); json::Value -doLedgerHeader(RPC::JsonContext&); +doLedgerHeader(rpc::JsonContext&); json::Value -doLedgerRequest(RPC::JsonContext&); +doLedgerRequest(rpc::JsonContext&); json::Value -doLogLevel(RPC::JsonContext&); +doLogLevel(rpc::JsonContext&); json::Value -doLogRotate(RPC::JsonContext&); +doLogRotate(rpc::JsonContext&); json::Value -doManifest(RPC::JsonContext&); +doManifest(rpc::JsonContext&); json::Value -doNFTBuyOffers(RPC::JsonContext&); +doNFTBuyOffers(rpc::JsonContext&); json::Value -doNFTSellOffers(RPC::JsonContext&); +doNFTSellOffers(rpc::JsonContext&); json::Value -doNoRippleCheck(RPC::JsonContext&); +doNoRippleCheck(rpc::JsonContext&); json::Value -doOwnerInfo(RPC::JsonContext&); +doOwnerInfo(rpc::JsonContext&); json::Value -doPathFind(RPC::JsonContext&); +doPathFind(rpc::JsonContext&); json::Value -doPause(RPC::JsonContext&); +doPause(rpc::JsonContext&); json::Value -doPeers(RPC::JsonContext&); +doPeers(rpc::JsonContext&); json::Value -doPing(RPC::JsonContext&); +doPing(rpc::JsonContext&); json::Value -doPrint(RPC::JsonContext&); +doPrint(rpc::JsonContext&); json::Value -doRandom(RPC::JsonContext&); +doRandom(rpc::JsonContext&); json::Value -doResume(RPC::JsonContext&); +doResume(rpc::JsonContext&); json::Value -doPeerReservationsAdd(RPC::JsonContext&); +doPeerReservationsAdd(rpc::JsonContext&); json::Value -doPeerReservationsDel(RPC::JsonContext&); +doPeerReservationsDel(rpc::JsonContext&); json::Value -doPeerReservationsList(RPC::JsonContext&); +doPeerReservationsList(rpc::JsonContext&); json::Value -doRipplePathFind(RPC::JsonContext&); +doRipplePathFind(rpc::JsonContext&); json::Value -doServerDefinitions(RPC::JsonContext&); +doServerDefinitions(rpc::JsonContext&); json::Value -doServerInfo(RPC::JsonContext&); // for humans +doServerInfo(rpc::JsonContext&); // for humans json::Value -doServerState(RPC::JsonContext&); // for machines +doServerState(rpc::JsonContext&); // for machines json::Value -doSign(RPC::JsonContext&); +doSign(rpc::JsonContext&); json::Value -doSignFor(RPC::JsonContext&); +doSignFor(rpc::JsonContext&); json::Value -doSimulate(RPC::JsonContext&); +doSimulate(rpc::JsonContext&); json::Value -doStop(RPC::JsonContext&); +doStop(rpc::JsonContext&); json::Value -doSubmit(RPC::JsonContext&); +doSubmit(rpc::JsonContext&); json::Value -doSubmitMultiSigned(RPC::JsonContext&); +doSubmitMultiSigned(rpc::JsonContext&); json::Value -doSubscribe(RPC::JsonContext&); +doSubscribe(rpc::JsonContext&); json::Value -doTransactionEntry(RPC::JsonContext&); +doTransactionEntry(rpc::JsonContext&); json::Value -doTxJson(RPC::JsonContext&); +doTxJson(rpc::JsonContext&); json::Value -doTxHistory(RPC::JsonContext&); +doTxHistory(rpc::JsonContext&); json::Value -doTxReduceRelay(RPC::JsonContext&); +doTxReduceRelay(rpc::JsonContext&); json::Value -doUnlList(RPC::JsonContext&); +doUnlList(rpc::JsonContext&); json::Value -doUnsubscribe(RPC::JsonContext&); +doUnsubscribe(rpc::JsonContext&); json::Value -doValidationCreate(RPC::JsonContext&); +doValidationCreate(rpc::JsonContext&); json::Value -doWalletPropose(RPC::JsonContext&); +doWalletPropose(rpc::JsonContext&); json::Value -doValidators(RPC::JsonContext&); +doValidators(rpc::JsonContext&); json::Value -doValidatorListSites(RPC::JsonContext&); +doValidatorListSites(rpc::JsonContext&); json::Value -doValidatorInfo(RPC::JsonContext&); +doValidatorInfo(rpc::JsonContext&); json::Value -doVaultInfo(RPC::JsonContext&); +doVaultInfo(rpc::JsonContext&); } // namespace xrpl diff --git a/src/xrpld/rpc/handlers/VaultInfo.cpp b/src/xrpld/rpc/handlers/VaultInfo.cpp index b6d2fe259f..034cd8383f 100644 --- a/src/xrpld/rpc/handlers/VaultInfo.cpp +++ b/src/xrpld/rpc/handlers/VaultInfo.cpp @@ -27,7 +27,7 @@ parseVault(json::Value const& params, json::Value& jvResult) { if (!uNodeIndex.parseHex(params[jss::vault_id].asString())) { - RPC::injectError(RpcInvalidParams, jvResult); + rpc::injectError(RpcInvalidParams, jvResult); return std::nullopt; } // else uNodeIndex holds the value we need @@ -37,14 +37,14 @@ parseVault(json::Value const& params, json::Value& jvResult) auto const id = parseBase58(params[jss::owner].asString()); if (!id) { - RPC::injectError(RpcActMalformed, jvResult); + rpc::injectError(RpcActMalformed, jvResult); return std::nullopt; } if (!(params[jss::seq].isInt() || params[jss::seq].isUInt()) || params[jss::seq].asDouble() <= 0.0 || params[jss::seq].asDouble() > double(json::Value::kMaxUInt)) { - RPC::injectError(RpcInvalidParams, jvResult); + rpc::injectError(RpcInvalidParams, jvResult); return std::nullopt; } @@ -53,7 +53,7 @@ parseVault(json::Value const& params, json::Value& jvResult) else { // Invalid combination of fields vault_id/owner/seq - RPC::injectError(RpcInvalidParams, jvResult); + rpc::injectError(RpcInvalidParams, jvResult); return std::nullopt; } @@ -61,10 +61,10 @@ parseVault(json::Value const& params, json::Value& jvResult) } json::Value -doVaultInfo(RPC::JsonContext& context) +doVaultInfo(rpc::JsonContext& context) { std::shared_ptr lpLedger; - auto jvResult = RPC::lookupLedger(lpLedger, context); + auto jvResult = rpc::lookupLedger(lpLedger, context); if (!lpLedger) return jvResult; diff --git a/src/xrpld/rpc/handlers/account/AccountChannels.cpp b/src/xrpld/rpc/handlers/account/AccountChannels.cpp index 8a5c7dc6e3..d50bf1cf07 100644 --- a/src/xrpld/rpc/handlers/account/AccountChannels.cpp +++ b/src/xrpld/rpc/handlers/account/AccountChannels.cpp @@ -69,17 +69,17 @@ addChannel(json::Value& jsonLines, SLE const& line) // marker: opaque // optional, resume previous query // } json::Value -doAccountChannels(RPC::JsonContext& context) +doAccountChannels(rpc::JsonContext& context) { auto const& params(context.params); if (!params.isMember(jss::account)) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (!ledger) return result; @@ -97,7 +97,7 @@ doAccountChannels(RPC::JsonContext& context) if (params.isMember(jss::destination_account)) { if (!params[jss::destination_account].isString()) - return RPC::invalidFieldError(jss::destination_account); + return rpc::invalidFieldError(jss::destination_account); strDst = params[jss::destination_account].asString(); } @@ -108,7 +108,7 @@ doAccountChannels(RPC::JsonContext& context) return rpcError(RpcActMalformed); unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kAccountChannels, context)) + if (auto err = readLimitField(limit, rpc::tuning::kAccountChannels, context)) return *err; json::Value jsonChannels{json::ValueType::Array}; @@ -126,7 +126,7 @@ doAccountChannels(RPC::JsonContext& context) if (params.isMember(jss::marker)) { if (!params[jss::marker].isString()) - return RPC::expectedFieldError(jss::marker, "string"); + return rpc::expectedFieldError(jss::marker, "string"); // Marker is composed of a comma separated index and start hint. The // former will be read as hex, and the latter using boost lexical cast. @@ -157,7 +157,7 @@ doAccountChannels(RPC::JsonContext& context) if (!sle) return rpcError(RpcInvalidParams); - if (!RPC::isRelatedToAccount(*ledger, sle, accountID)) + if (!rpc::isRelatedToAccount(*ledger, sle, accountID)) return rpcError(RpcInvalidParams); } @@ -182,7 +182,7 @@ doAccountChannels(RPC::JsonContext& context) if (++count == limit) { marker = sleCur->key(); - nextHint = RPC::getStartHint(sleCur, visitData.accountID); + nextHint = rpc::getStartHint(sleCur, visitData.accountID); } if (count <= limit && sleCur->getType() == ltPAYCHAN && @@ -213,7 +213,7 @@ doAccountChannels(RPC::JsonContext& context) for (auto const& item : visitData.items) addChannel(jsonChannels, *item); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; result[jss::channels] = std::move(jsonChannels); return result; } diff --git a/src/xrpld/rpc/handlers/account/AccountCurrencies.cpp b/src/xrpld/rpc/handlers/account/AccountCurrencies.cpp index 058c10e224..d9cd41cbbc 100644 --- a/src/xrpld/rpc/handlers/account/AccountCurrencies.cpp +++ b/src/xrpld/rpc/handlers/account/AccountCurrencies.cpp @@ -19,30 +19,30 @@ namespace xrpl { json::Value -doAccountCurrencies(RPC::JsonContext& context) +doAccountCurrencies(rpc::JsonContext& context) { auto& params = context.params; if (!(params.isMember(jss::account) || params.isMember(jss::ident))) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); std::string strIdent; if (params.isMember(jss::account)) { if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); strIdent = params[jss::account].asString(); } else if (params.isMember(jss::ident)) { if (!params[jss::ident].isString()) - return RPC::invalidFieldError(jss::ident); + return rpc::invalidFieldError(jss::ident); strIdent = params[jss::ident].asString(); } // Get the current ledger std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (!ledger) return result; @@ -50,7 +50,7 @@ doAccountCurrencies(RPC::JsonContext& context) auto id = parseBase58(strIdent); if (!id) { - RPC::injectError(RpcActMalformed, result); + rpc::injectError(RpcActMalformed, result); return result; } auto const accountID{id.value()}; diff --git a/src/xrpld/rpc/handlers/account/AccountInfo.cpp b/src/xrpld/rpc/handlers/account/AccountInfo.cpp index 3a96593452..c618ad3b3a 100644 --- a/src/xrpld/rpc/handlers/account/AccountInfo.cpp +++ b/src/xrpld/rpc/handlers/account/AccountInfo.cpp @@ -84,7 +84,7 @@ injectSLE(json::Value& jv, SLE const& sle) // TODO(tom): what is that "default"? json::Value -doAccountInfo(RPC::JsonContext& context) +doAccountInfo(rpc::JsonContext& context) { auto& params = context.params; @@ -92,22 +92,22 @@ doAccountInfo(RPC::JsonContext& context) if (params.isMember(jss::account)) { if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); strIdent = params[jss::account].asString(); } else if (params.isMember(jss::ident)) { if (!params[jss::ident].isString()) - return RPC::invalidFieldError(jss::ident); + return rpc::invalidFieldError(jss::ident); strIdent = params[jss::ident].asString(); } else { - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); } std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (!ledger) return result; @@ -116,7 +116,7 @@ doAccountInfo(RPC::JsonContext& context) auto id = parseBase58(strIdent); if (!id) { - RPC::injectError(RpcActMalformed, result); + rpc::injectError(RpcActMalformed, result); return result; } auto const accountID{id.value()}; @@ -154,7 +154,7 @@ doAccountInfo(RPC::JsonContext& context) { // It doesn't make sense to request the queue // with any closed or validated ledger. - RPC::injectError(RpcInvalidParams, result); + rpc::injectError(RpcInvalidParams, result); return result; } @@ -206,7 +206,7 @@ doAccountInfo(RPC::JsonContext& context) if (context.apiVersion > 1u && params.isMember(jss::signer_lists) && !params[jss::signer_lists].isBool()) { - RPC::injectError(RpcInvalidParams, result); + rpc::injectError(RpcInvalidParams, result); return result; } @@ -331,7 +331,7 @@ doAccountInfo(RPC::JsonContext& context) else { result[jss::account] = toBase58(accountID); - RPC::injectError(RpcActNotFound, result); + rpc::injectError(RpcActNotFound, result); } return result; diff --git a/src/xrpld/rpc/handlers/account/AccountLines.cpp b/src/xrpld/rpc/handlers/account/AccountLines.cpp index e69f70ca5a..f134c8af92 100644 --- a/src/xrpld/rpc/handlers/account/AccountLines.cpp +++ b/src/xrpld/rpc/handlers/account/AccountLines.cpp @@ -82,24 +82,24 @@ addLine(json::Value& jsonLines, RPCTrustLine const& line) // this account's side) // } json::Value -doAccountLines(RPC::JsonContext& context) +doAccountLines(rpc::JsonContext& context) { auto const& params(context.params); if (!params.isMember(jss::account)) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (!ledger) return result; auto id = parseBase58(params[jss::account].asString()); if (!id) { - RPC::injectError(RpcActMalformed, result); + rpc::injectError(RpcActMalformed, result); return result; } auto const accountID{id.value()}; @@ -116,12 +116,12 @@ doAccountLines(RPC::JsonContext& context) }(); if (!strPeer.empty() && !raPeerAccount) { - RPC::injectError(RpcActMalformed, result); + rpc::injectError(RpcActMalformed, result); return result; } unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kAccountLines, context)) + if (auto err = readLimitField(limit, rpc::tuning::kAccountLines, context)) return *err; // this flag allows the requester to ask incoming trustlines in default @@ -150,7 +150,7 @@ doAccountLines(RPC::JsonContext& context) if (params.isMember(jss::marker)) { if (!params[jss::marker].isString()) - return RPC::expectedFieldError(jss::marker, "string"); + return rpc::expectedFieldError(jss::marker, "string"); // Marker is composed of a comma separated index and start hint. The // former will be read as hex, and the latter using boost lexical cast. @@ -181,7 +181,7 @@ doAccountLines(RPC::JsonContext& context) if (!sle) return rpcError(RpcInvalidParams); - if (!RPC::isRelatedToAccount(*ledger, sle, accountID)) + if (!rpc::isRelatedToAccount(*ledger, sle, accountID)) return rpcError(RpcInvalidParams); } @@ -207,7 +207,7 @@ doAccountLines(RPC::JsonContext& context) if (++count == limit) { marker = sleCur->key(); - nextHint = RPC::getStartHint(sleCur, visitData.accountID); + nextHint = rpc::getStartHint(sleCur, visitData.accountID); } if (sleCur->getType() != ltRIPPLE_STATE) @@ -259,7 +259,7 @@ doAccountLines(RPC::JsonContext& context) for (auto const& item : visitData.items) addLine(jsonLines, item); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; return result; } diff --git a/src/xrpld/rpc/handlers/account/AccountNFTs.cpp b/src/xrpld/rpc/handlers/account/AccountNFTs.cpp index ea9bec0f45..580b93caa3 100644 --- a/src/xrpld/rpc/handlers/account/AccountNFTs.cpp +++ b/src/xrpld/rpc/handlers/account/AccountNFTs.cpp @@ -35,14 +35,14 @@ namespace xrpl { * } */ json::Value -doAccountNFTs(RPC::JsonContext& context) +doAccountNFTs(rpc::JsonContext& context) { auto const& params = context.params; if (!params.isMember(jss::account)) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); auto id = parseBase58(params[jss::account].asString()); if (!id) @@ -51,7 +51,7 @@ doAccountNFTs(RPC::JsonContext& context) } std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (ledger == nullptr) return result; auto const accountID{id.value()}; @@ -60,7 +60,7 @@ doAccountNFTs(RPC::JsonContext& context) return rpcError(RpcActNotFound); unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kAccountNfTokens, context)) + if (auto err = readLimitField(limit, rpc::tuning::kAccountNfTokens, context)) return *err; uint256 marker; @@ -70,10 +70,10 @@ doAccountNFTs(RPC::JsonContext& context) { auto const& m = params[jss::marker]; if (!m.isString()) - return RPC::expectedFieldError(jss::marker, "string"); + return rpc::expectedFieldError(jss::marker, "string"); if (!marker.parseHex(m.asString())) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); } auto const first = keylet::nftokenPage(keylet::nftokenPageMin(accountID), marker); @@ -125,7 +125,7 @@ doAccountNFTs(RPC::JsonContext& context) } if (markerSet && !markerFound) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); pastMarker = true; @@ -160,10 +160,10 @@ doAccountNFTs(RPC::JsonContext& context) } if (markerSet && !markerFound) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); result[jss::account] = toBase58(accountID); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; return result; } diff --git a/src/xrpld/rpc/handlers/account/AccountObjects.cpp b/src/xrpld/rpc/handlers/account/AccountObjects.cpp index 4a34ff02fc..e855ed65e6 100644 --- a/src/xrpld/rpc/handlers/account/AccountObjects.cpp +++ b/src/xrpld/rpc/handlers/account/AccountObjects.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -191,6 +192,13 @@ getAccountObjects( for (; entryIter != dirEntries.end(); ++entryIter) { auto const sleNode = ledger.read(keylet::child(*entryIter)); + if (!sleNode) + { + // LCOV_EXCL_START + UNREACHABLE("xrpl::doAccountObjects : null SLE"); + continue; + // LCOV_EXCL_STOP + } bool canAppend = true; @@ -257,24 +265,24 @@ getAccountObjects( } json::Value -doAccountObjects(RPC::JsonContext& context) +doAccountObjects(rpc::JsonContext& context) { auto const& params = context.params; if (!params.isMember(jss::account)) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (ledger == nullptr) return result; auto const id = parseBase58(params[jss::account].asString()); if (!id) { - RPC::injectError(RpcActMalformed, result); + rpc::injectError(RpcActMalformed, result); return result; } auto const accountID{id.value()}; @@ -323,10 +331,10 @@ doAccountObjects(RPC::JsonContext& context) } else { - auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params); + auto [rpcStatus, type] = rpc::chooseLedgerEntryType(params); - if (!RPC::isAccountObjectsValidType(type)) - return RPC::invalidFieldError(jss::type); + if (!rpc::isAccountObjectsValidType(type)) + return rpc::invalidFieldError(jss::type); if (rpcStatus) { @@ -341,7 +349,7 @@ doAccountObjects(RPC::JsonContext& context) } unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kAccountObjects, context)) + if (auto err = readLimitField(limit, rpc::tuning::kAccountObjects, context)) return *err; uint256 dirIndex; @@ -350,18 +358,18 @@ doAccountObjects(RPC::JsonContext& context) { auto const& marker = params[jss::marker]; if (!marker.isString()) - return RPC::expectedFieldError(jss::marker, "string"); + return rpc::expectedFieldError(jss::marker, "string"); auto const& markerStr = marker.asString(); auto const& idx = markerStr.find(','); if (idx == std::string::npos) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); if (!dirIndex.parseHex(markerStr.substr(0, idx))) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); if (!entryIndex.parseHex(markerStr.substr(idx + 1))) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); } std::optional sponsoredFilter; @@ -369,17 +377,17 @@ doAccountObjects(RPC::JsonContext& context) { auto const& sponsoredJv = params[jss::sponsored]; if (!sponsoredJv.isBool()) - return RPC::expectedFieldError(jss::sponsored, "boolean"); + return rpc::expectedFieldError(jss::sponsored, "boolean"); sponsoredFilter = sponsoredJv.asBool(); } if (!getAccountObjects( *ledger, accountID, typeFilter, dirIndex, entryIndex, limit, sponsoredFilter, result)) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); result[jss::account] = toBase58(accountID); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; return result; } diff --git a/src/xrpld/rpc/handlers/account/AccountOffers.cpp b/src/xrpld/rpc/handlers/account/AccountOffers.cpp index 4829ff56b1..1467b14b48 100644 --- a/src/xrpld/rpc/handlers/account/AccountOffers.cpp +++ b/src/xrpld/rpc/handlers/account/AccountOffers.cpp @@ -54,24 +54,24 @@ appendOfferJson(SLE::const_ref offer, json::Value& offers) // marker: opaque // optional, resume previous query // } json::Value -doAccountOffers(RPC::JsonContext& context) +doAccountOffers(rpc::JsonContext& context) { auto const& params(context.params); if (!params.isMember(jss::account)) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (!ledger) return result; auto id = parseBase58(params[jss::account].asString()); if (!id) { - RPC::injectError(RpcActMalformed, result); + rpc::injectError(RpcActMalformed, result); return result; } auto const accountID{id.value()}; @@ -83,7 +83,7 @@ doAccountOffers(RPC::JsonContext& context) return rpcError(RpcActNotFound); unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kAccountOffers, context)) + if (auto err = readLimitField(limit, rpc::tuning::kAccountOffers, context)) return *err; json::Value& jsonOffers(result[jss::offers] = json::ValueType::Array); @@ -94,20 +94,20 @@ doAccountOffers(RPC::JsonContext& context) if (params.isMember(jss::marker)) { if (!params[jss::marker].isString()) - return RPC::expectedFieldError(jss::marker, "string"); + return rpc::expectedFieldError(jss::marker, "string"); // Marker is composed of a comma separated index and start hint. The // former will be read as hex, and the latter using boost lexical cast. std::stringstream marker(params[jss::marker].asString()); std::string value; if (!std::getline(marker, value, ',')) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); if (!startAfter.parseHex(value)) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); if (!std::getline(marker, value, ',')) - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); try { @@ -115,7 +115,7 @@ doAccountOffers(RPC::JsonContext& context) } catch (boost::bad_lexical_cast&) { - return RPC::invalidFieldError(jss::marker); + return rpc::invalidFieldError(jss::marker); } // We then must check if the object pointed to by the marker is actually @@ -125,7 +125,7 @@ doAccountOffers(RPC::JsonContext& context) if (!sle) return rpcError(RpcInvalidParams); - if (!RPC::isRelatedToAccount(*ledger, sle, accountID)) + if (!rpc::isRelatedToAccount(*ledger, sle, accountID)) return rpcError(RpcInvalidParams); } @@ -150,7 +150,7 @@ doAccountOffers(RPC::JsonContext& context) if (++count == limit) { marker = sle->key(); - nextHint = RPC::getStartHint(sle, accountID); + nextHint = rpc::getStartHint(sle, accountID); } if (count <= limit && sle->getType() == ltOFFER) @@ -176,7 +176,7 @@ doAccountOffers(RPC::JsonContext& context) for (auto const& offer : offers) appendOfferJson(offer, jsonOffers); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; return result; } diff --git a/src/xrpld/rpc/handlers/account/AccountTx.cpp b/src/xrpld/rpc/handlers/account/AccountTx.cpp index c43f560861..7b0c34e048 100644 --- a/src/xrpld/rpc/handlers/account/AccountTx.cpp +++ b/src/xrpld/rpc/handlers/account/AccountTx.cpp @@ -43,11 +43,11 @@ static std::expected parseDelegateFilter(json::Value const& delegateNode) { if (!delegateNode.isObject()) - return std::unexpected(RPC::invalidFieldError(jss::delegate)); + return std::unexpected(rpc::invalidFieldError(jss::delegate)); if (!delegateNode.isMember(jss::delegate_filter) || !delegateNode[jss::delegate_filter].isString()) - return std::unexpected(RPC::invalidFieldError(jss::delegate_filter)); + return std::unexpected(rpc::invalidFieldError(jss::delegate_filter)); auto const& delegateFilterStr = delegateNode[jss::delegate_filter].asString(); @@ -58,7 +58,7 @@ parseDelegateFilter(json::Value const& delegateNode) if (delegateFilterStr == "authorizer") return DelegateType::Authorizer; - return std::unexpected(RPC::invalidFieldError(jss::delegate_filter)); + return std::unexpected(rpc::invalidFieldError(jss::delegate_filter)); }(); if (!typeResult) @@ -70,7 +70,7 @@ parseDelegateFilter(json::Value const& delegateNode) if (delegateNode.isMember(jss::counter_party)) { if (!delegateNode[jss::counter_party].isString()) - return std::unexpected(RPC::invalidFieldError(jss::counter_party)); + return std::unexpected(rpc::invalidFieldError(jss::counter_party)); counterparty = parseBase58(delegateNode[jss::counter_party].asString()); @@ -90,7 +90,7 @@ using LedgerSpecifier = RelationalDatabase::LedgerSpecifier; // parses args into a ledger specifier, or returns a Json object on error std::variant, json::Value> -parseLedgerArgs(RPC::Context& context, json::Value const& params) +parseLedgerArgs(rpc::Context& context, json::Value const& params) { json::Value response; // if ledger_index_min or max is specified, then ledger_hash or ledger_index @@ -100,7 +100,7 @@ parseLedgerArgs(RPC::Context& context, json::Value const& params) if ((params.isMember(jss::ledger_index_min) || params.isMember(jss::ledger_index_max)) && (params.isMember(jss::ledger_hash) || params.isMember(jss::ledger_index))) { - RPC::Status const status{RpcInvalidParams, "invalidParams"}; + rpc::Status const status{RpcInvalidParams, "invalidParams"}; status.inject(response); return response; } @@ -123,7 +123,7 @@ parseLedgerArgs(RPC::Context& context, json::Value const& params) auto& hashValue = params[jss::ledger_hash]; if (!hashValue.isString()) { - RPC::Status const status{RpcInvalidParams, "ledgerHashNotString"}; + rpc::Status const status{RpcInvalidParams, "ledgerHashNotString"}; status.inject(response); return response; } @@ -131,7 +131,7 @@ parseLedgerArgs(RPC::Context& context, json::Value const& params) LedgerHash hash; if (!hash.parseHex(hashValue.asString())) { - RPC::Status const status{RpcInvalidParams, "ledgerHashMalformed"}; + rpc::Status const status{RpcInvalidParams, "ledgerHashMalformed"}; status.inject(response); return response; } @@ -162,7 +162,7 @@ parseLedgerArgs(RPC::Context& context, json::Value const& params) } else { - RPC::Status const status{RpcInvalidParams, "ledger_index string malformed"}; + rpc::Status const status{RpcInvalidParams, "ledger_index string malformed"}; status.inject(response); return response; } @@ -172,8 +172,8 @@ parseLedgerArgs(RPC::Context& context, json::Value const& params) return std::optional{}; } -std::variant -getLedgerRange(RPC::Context& context, std::optional const& ledgerSpecifier) +std::variant +getLedgerRange(rpc::Context& context, std::optional const& ledgerSpecifier) { std::uint32_t uValidatedMin = 0; std::uint32_t uValidatedMax = 0; @@ -193,7 +193,7 @@ getLedgerRange(RPC::Context& context, std::optional const& ledg if (ledgerSpecifier) { auto status = std::visit( - [&](auto const& ls) -> RPC::Status { + [&](auto const& ls) -> rpc::Status { using T = std::decay_t; if constexpr (std::is_same_v) { @@ -241,7 +241,7 @@ getLedgerRange(RPC::Context& context, std::optional const& ledg } uLedgerMin = uLedgerMax = ledgerView->header().seq; } - return RPC::Status::kOK; + return rpc::Status::kOK; }, *ledgerSpecifier); @@ -251,15 +251,15 @@ getLedgerRange(RPC::Context& context, std::optional const& ledg return LedgerRange{.min = uLedgerMin, .max = uLedgerMax}; } -std::pair -doAccountTxHelp(RPC::Context& context, AccountTxArgs const& args) +std::pair +doAccountTxHelp(rpc::Context& context, AccountTxArgs const& args) { - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; AccountTxResult result; auto lgrRange = getLedgerRange(context, args.ledger); - if (auto stat = std::get_if(&lgrRange)) + if (auto stat = std::get_if(&lgrRange)) { // An error occurred getting the requested ledger range return {result, *stat}; @@ -318,12 +318,12 @@ doAccountTxHelp(RPC::Context& context, AccountTxArgs const& args) json::Value populateJsonResponse( - std::pair const& res, + std::pair const& res, AccountTxArgs const& args, - RPC::JsonContext const& context) + rpc::JsonContext const& context) { json::Value response; - RPC::Status const& error = res.second; + rpc::Status const& error = res.second; if (error.toErrorCode() != RpcSuccess) { error.inject(response); @@ -374,13 +374,13 @@ populateJsonResponse( } auto const& sttx = txn->getSTransaction(); - RPC::insertDeliverMax(jvObj[jsonTx], sttx->getTxnType(), context.apiVersion); + rpc::insertDeliverMax(jvObj[jsonTx], sttx->getTxnType(), context.apiVersion); if (txnMeta) { jvObj[jss::meta] = txnMeta->getJson(JsonOptions::Values::IncludeDate); insertDeliveredAmount(jvObj[jss::meta], context, txn, *txnMeta); - RPC::insertNFTSyntheticInJson(jvObj, sttx, *txnMeta); - RPC::insertMPTokenIssuanceID(jvObj[jss::meta], sttx, *txnMeta); + rpc::insertNFTSyntheticInJson(jvObj, sttx, *txnMeta); + rpc::insertMPTokenIssuanceID(jvObj[jss::meta], sttx, *txnMeta); } else { @@ -445,7 +445,7 @@ populateJsonResponse( // delegate-filtered query is only valid for a follow-up request that repeats // the same `delegate` object json::Value -doAccountTx(RPC::JsonContext& context) +doAccountTx(rpc::JsonContext& context) { if (!context.app.config().useTxTables()) return rpcError(RpcNotEnabled); @@ -460,24 +460,24 @@ doAccountTx(RPC::JsonContext& context) // onwards only if (context.apiVersion > 1u && params.isMember(jss::binary) && !params[jss::binary].isBool()) { - return RPC::invalidFieldError(jss::binary); + return rpc::invalidFieldError(jss::binary); } if (context.apiVersion > 1u && params.isMember(jss::forward) && !params[jss::forward].isBool()) { - return RPC::invalidFieldError(jss::forward); + return rpc::invalidFieldError(jss::forward); } - if (auto const err = RPC::readLimitField(args.limit, RPC::Tuning::kAccountTx, context)) + if (auto const err = rpc::readLimitField(args.limit, rpc::tuning::kAccountTx, context)) return *err; args.binary = params.isMember(jss::binary) && params[jss::binary].asBool(); args.forward = params.isMember(jss::forward) && params[jss::forward].asBool(); if (!params.isMember(jss::account)) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); auto const account = parseBase58(params[jss::account].asString()); if (!account) @@ -500,7 +500,7 @@ doAccountTx(RPC::JsonContext& context) !token[jss::ledger].isConvertibleTo(json::ValueType::UInt) || !token[jss::seq].isConvertibleTo(json::ValueType::UInt)) { - RPC::Status const status{ + rpc::Status const status{ RpcInvalidParams, "invalid marker. Provide ledger index via ledger field, and " "transaction sequence number via seq field"}; @@ -534,7 +534,7 @@ doAccountTx(RPC::JsonContext& context) params[jss::marker][jss::delegate].asBool(); if (markerFromDelegate != args.delegate.has_value()) { - RPC::Status const status{ + rpc::Status const status{ RpcInvalidParams, "Do not mix delegate and non-delegate pagination markers in account_tx; " "repeat the same `delegate` object when using a delegate marker."}; diff --git a/src/xrpld/rpc/handlers/account/GatewayBalances.cpp b/src/xrpld/rpc/handlers/account/GatewayBalances.cpp index a6730c8e2b..ff19d1d1e5 100644 --- a/src/xrpld/rpc/handlers/account/GatewayBalances.cpp +++ b/src/xrpld/rpc/handlers/account/GatewayBalances.cpp @@ -49,19 +49,19 @@ namespace xrpl { // gateway_balances [] [ [ ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (!ledger) return result; if (!(params.isMember(jss::account) || params.isMember(jss::ident))) - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); std::string const strIdent( params.isMember(jss::account) ? params[jss::account].asString() @@ -72,13 +72,13 @@ doGatewayBalances(RPC::JsonContext& context) if (!id) return rpcError(RpcActMalformed); auto const accountID{id.value()}; - context.loadType = Resource::kFeeHeavyBurdenRpc; + context.loadType = resource::kFeeHeavyBurdenRpc; result[jss::account] = toBase58(accountID); if (context.apiVersion > 1u && !ledger->exists(keylet::account(accountID))) { - RPC::injectError(RpcActNotFound, result); + rpc::injectError(RpcActNotFound, result); return result; } @@ -126,11 +126,11 @@ doGatewayBalances(RPC::JsonContext& context) // not have currency issued by the account from the request. if (context.apiVersion < 2u) { - RPC::injectError(RpcInvalidHotwallet, result); + rpc::injectError(RpcInvalidHotwallet, result); } else { - RPC::injectError(RpcInvalidParams, result); + rpc::injectError(RpcInvalidParams, result); } return result; } diff --git a/src/xrpld/rpc/handlers/account/NoRippleCheck.cpp b/src/xrpld/rpc/handlers/account/NoRippleCheck.cpp index d8bb65aba9..4be6e6f1af 100644 --- a/src/xrpld/rpc/handlers/account/NoRippleCheck.cpp +++ b/src/xrpld/rpc/handlers/account/NoRippleCheck.cpp @@ -26,7 +26,7 @@ namespace xrpl { static void fillTransaction( - RPC::JsonContext& context, + rpc::JsonContext& context, json::Value& txArray, AccountID const& accountID, std::uint32_t& sequence, @@ -49,17 +49,17 @@ fillTransaction( // transactions: true // optional, recommend transactions // } json::Value -doNoRippleCheck(RPC::JsonContext& context) +doNoRippleCheck(rpc::JsonContext& context) { auto const& params(context.params); if (!params.isMember(jss::account)) - return RPC::missingFieldError("account"); + return rpc::missingFieldError("account"); if (!params.isMember("role")) - return RPC::missingFieldError("role"); + return rpc::missingFieldError("role"); if (!params[jss::account].isString()) - return RPC::invalidFieldError(jss::account); + return rpc::invalidFieldError(jss::account); bool roleGateway = false; { @@ -70,12 +70,12 @@ doNoRippleCheck(RPC::JsonContext& context) } else if (role != "user") { - return RPC::invalidFieldError("role"); + return rpc::invalidFieldError("role"); } } unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kNoRippleCheck, context)) + if (auto err = readLimitField(limit, rpc::tuning::kNoRippleCheck, context)) return *err; bool transactions = false; @@ -89,11 +89,11 @@ doNoRippleCheck(RPC::JsonContext& context) if (context.apiVersion > 1u && params.isMember(jss::transactions) && !params[jss::transactions].isBool()) { - return RPC::invalidFieldError(jss::transactions); + return rpc::invalidFieldError(jss::transactions); } std::shared_ptr ledger; - auto result = RPC::lookupLedger(ledger, context); + auto result = rpc::lookupLedger(ledger, context); if (!ledger) return result; @@ -104,7 +104,7 @@ doNoRippleCheck(RPC::JsonContext& context) auto id = parseBase58(params[jss::account].asString()); if (!id) { - RPC::injectError(RpcActMalformed, result); + rpc::injectError(RpcActMalformed, result); return result; } auto const accountID{id.value()}; diff --git a/src/xrpld/rpc/handlers/account/OwnerInfo.cpp b/src/xrpld/rpc/handlers/account/OwnerInfo.cpp index 7cfed3cf53..2f5f76c619 100644 --- a/src/xrpld/rpc/handlers/account/OwnerInfo.cpp +++ b/src/xrpld/rpc/handlers/account/OwnerInfo.cpp @@ -17,11 +17,11 @@ namespace xrpl { // 'ident' : , // } json::Value -doOwnerInfo(RPC::JsonContext& context) +doOwnerInfo(rpc::JsonContext& context) { if (!context.params.isMember(jss::account) && !context.params.isMember(jss::ident)) { - return RPC::missingFieldError(jss::account); + return rpc::missingFieldError(jss::account); } std::string const strIdent = context.params.isMember(jss::account) diff --git a/src/xrpld/rpc/handlers/admin/BlackList.cpp b/src/xrpld/rpc/handlers/admin/BlackList.cpp index 5065a41ec4..7a72651373 100644 --- a/src/xrpld/rpc/handlers/admin/BlackList.cpp +++ b/src/xrpld/rpc/handlers/admin/BlackList.cpp @@ -8,7 +8,7 @@ namespace xrpl { json::Value -doBlackList(RPC::JsonContext& context) +doBlackList(rpc::JsonContext& context) { auto& rm = context.app.getResourceManager(); if (context.params.isMember(jss::threshold)) diff --git a/src/xrpld/rpc/handlers/admin/UnlList.cpp b/src/xrpld/rpc/handlers/admin/UnlList.cpp index c3835c7ae0..61b5e4c640 100644 --- a/src/xrpld/rpc/handlers/admin/UnlList.cpp +++ b/src/xrpld/rpc/handlers/admin/UnlList.cpp @@ -12,7 +12,7 @@ namespace xrpl { json::Value -doUnlList(RPC::JsonContext& context) +doUnlList(rpc::JsonContext& context) { json::Value obj(json::ValueType::Object); diff --git a/src/xrpld/rpc/handlers/admin/data/CanDelete.cpp b/src/xrpld/rpc/handlers/admin/data/CanDelete.cpp index 9ec1157e66..91db16bb4f 100644 --- a/src/xrpld/rpc/handlers/admin/data/CanDelete.cpp +++ b/src/xrpld/rpc/handlers/admin/data/CanDelete.cpp @@ -19,10 +19,10 @@ namespace xrpl { // can_delete [||now|always|never] json::Value -doCanDelete(RPC::JsonContext& context) +doCanDelete(rpc::JsonContext& context) { if (!context.app.getSHAMapStore().advisoryDelete()) - return RPC::makeError(RpcNotEnabled); + return rpc::makeError(RpcNotEnabled); json::Value ret(json::ValueType::Object); @@ -56,20 +56,20 @@ doCanDelete(RPC::JsonContext& context) { canDeleteSeq = context.app.getSHAMapStore().getLastRotated(); if (canDeleteSeq == 0u) - return RPC::makeError(RpcNotReady); + return rpc::makeError(RpcNotReady); } else if (uint256 lh; lh.parseHex(canDeleteStr)) { auto ledger = context.ledgerMaster.getLedgerByHash(lh); if (!ledger) - return RPC::makeError(RpcLgrNotFound, "ledgerNotFound"); + return rpc::makeError(RpcLgrNotFound, "ledgerNotFound"); canDeleteSeq = ledger->header().seq; } else { - return RPC::makeError(RpcInvalidParams); + return rpc::makeError(RpcInvalidParams); } } diff --git a/src/xrpld/rpc/handlers/admin/data/LedgerCleaner.cpp b/src/xrpld/rpc/handlers/admin/data/LedgerCleaner.cpp index a62ca44cca..78a2fec410 100644 --- a/src/xrpld/rpc/handlers/admin/data/LedgerCleaner.cpp +++ b/src/xrpld/rpc/handlers/admin/data/LedgerCleaner.cpp @@ -9,10 +9,10 @@ namespace xrpl { json::Value -doLedgerCleaner(RPC::JsonContext& context) +doLedgerCleaner(rpc::JsonContext& context) { context.app.getLedgerCleaner().clean(context.params); - return RPC::makeObjectValue("Cleaner configured"); + return rpc::makeObjectValue("Cleaner configured"); } } // namespace xrpl diff --git a/src/xrpld/rpc/handlers/admin/data/LedgerRequest.cpp b/src/xrpld/rpc/handlers/admin/data/LedgerRequest.cpp index de1e4439cf..80a49aea3f 100644 --- a/src/xrpld/rpc/handlers/admin/data/LedgerRequest.cpp +++ b/src/xrpld/rpc/handlers/admin/data/LedgerRequest.cpp @@ -13,10 +13,10 @@ namespace xrpl { // ledger_index : // } json::Value -doLedgerRequest(RPC::JsonContext& context) +doLedgerRequest(rpc::JsonContext& context) { - context.loadType = Resource::kFeeHeavyBurdenRpc; - auto res = RPC::getOrAcquireLedger(context); + context.loadType = resource::kFeeHeavyBurdenRpc; + auto res = rpc::getOrAcquireLedger(context); if (!res.has_value()) return res.error(); diff --git a/src/xrpld/rpc/handlers/admin/keygen/ValidationCreate.cpp b/src/xrpld/rpc/handlers/admin/keygen/ValidationCreate.cpp index 0849bad944..5ff3e6727e 100644 --- a/src/xrpld/rpc/handlers/admin/keygen/ValidationCreate.cpp +++ b/src/xrpld/rpc/handlers/admin/keygen/ValidationCreate.cpp @@ -30,7 +30,7 @@ validationSeed(json::Value const& params) // This command requires Role::ADMIN access because it makes // no sense to ask an untrusted server for this. json::Value -doValidationCreate(RPC::JsonContext& context) +doValidationCreate(rpc::JsonContext& context) { json::Value obj(json::ValueType::Object); diff --git a/src/xrpld/rpc/handlers/admin/keygen/WalletPropose.cpp b/src/xrpld/rpc/handlers/admin/keygen/WalletPropose.cpp index 4b5f1821e3..62def76f9a 100644 --- a/src/xrpld/rpc/handlers/admin/keygen/WalletPropose.cpp +++ b/src/xrpld/rpc/handlers/admin/keygen/WalletPropose.cpp @@ -52,7 +52,7 @@ estimateEntropy(std::string const& input) // passphrase: // } json::Value -doWalletPropose(RPC::JsonContext& context) +doWalletPropose(rpc::JsonContext& context) { return walletPropose(context.params); } @@ -68,7 +68,7 @@ walletPropose(json::Value const& params) { if (!params[jss::key_type].isString()) { - return RPC::expectedFieldError(jss::key_type, "string"); + return rpc::expectedFieldError(jss::key_type, "string"); } keyType = keyTypeFromString(params[jss::key_type].asString()); @@ -83,11 +83,11 @@ walletPropose(json::Value const& params) { if (params.isMember(jss::passphrase)) { - seed = RPC::parseXrplLibSeed(params[jss::passphrase]); + seed = rpc::parseXrplLibSeed(params[jss::passphrase]); } else if (params.isMember(jss::seed)) { - seed = RPC::parseXrplLibSeed(params[jss::seed]); + seed = rpc::parseXrplLibSeed(params[jss::seed]); } if (seed) @@ -110,7 +110,7 @@ walletPropose(json::Value const& params) { json::Value err; - seed = RPC::getSeedFromRPC(params, err); + seed = rpc::getSeedFromRPC(params, err); if (!seed) return err; diff --git a/src/xrpld/rpc/handlers/admin/log/LogLevel.cpp b/src/xrpld/rpc/handlers/admin/log/LogLevel.cpp index 1ff5aa1a27..4aae350810 100644 --- a/src/xrpld/rpc/handlers/admin/log/LogLevel.cpp +++ b/src/xrpld/rpc/handlers/admin/log/LogLevel.cpp @@ -16,7 +16,7 @@ namespace xrpl { json::Value -doLogLevel(RPC::JsonContext& context) +doLogLevel(rpc::JsonContext& context) { // log_level if (not context.params.isMember(jss::severity)) diff --git a/src/xrpld/rpc/handlers/admin/log/LogRotate.cpp b/src/xrpld/rpc/handlers/admin/log/LogRotate.cpp index 5f5c3e64df..ca935540dc 100644 --- a/src/xrpld/rpc/handlers/admin/log/LogRotate.cpp +++ b/src/xrpld/rpc/handlers/admin/log/LogRotate.cpp @@ -9,10 +9,10 @@ namespace xrpl { json::Value -doLogRotate(RPC::JsonContext& context) +doLogRotate(rpc::JsonContext& context) { context.app.getPerfLog().rotate(); - return RPC::makeObjectValue(context.app.getLogs().rotate()); + return rpc::makeObjectValue(context.app.getLogs().rotate()); } } // namespace xrpl diff --git a/src/xrpld/rpc/handlers/admin/peer/Connect.cpp b/src/xrpld/rpc/handlers/admin/peer/Connect.cpp index 568dcdaa26..b318af06f9 100644 --- a/src/xrpld/rpc/handlers/admin/peer/Connect.cpp +++ b/src/xrpld/rpc/handlers/admin/peer/Connect.cpp @@ -21,15 +21,15 @@ namespace xrpl { // } // XXX Might allow domain for manual connections. json::Value -doConnect(RPC::JsonContext& context) +doConnect(rpc::JsonContext& context) { if (context.app.config().standalone()) { - return RPC::makeError(RpcNotSynced); + return rpc::makeError(RpcNotSynced); } if (!context.params.isMember(jss::ip)) - return RPC::missingFieldError(jss::ip); + return rpc::missingFieldError(jss::ip); if (context.params.isMember(jss::port) && !context.params[jss::port].isConvertibleTo(json::ValueType::Int)) @@ -49,12 +49,12 @@ doConnect(RPC::JsonContext& context) } auto const ipStr = context.params[jss::ip].asString(); - auto ip = beast::IP::Endpoint::fromString(ipStr); + auto ip = beast::ip::Endpoint::fromString(ipStr); if (!isUnspecified(ip)) context.app.getOverlay().connect(ip.atPort(iPort)); - return RPC::makeObjectValue( + return rpc::makeObjectValue( "attempting connection to IP:" + ipStr + " port: " + std::to_string(iPort)); } diff --git a/src/xrpld/rpc/handlers/admin/peer/PeerReservationsAdd.cpp b/src/xrpld/rpc/handlers/admin/peer/PeerReservationsAdd.cpp index 23b0d094b4..579ad85f41 100644 --- a/src/xrpld/rpc/handlers/admin/peer/PeerReservationsAdd.cpp +++ b/src/xrpld/rpc/handlers/admin/peer/PeerReservationsAdd.cpp @@ -15,12 +15,12 @@ namespace xrpl { json::Value -doPeerReservationsAdd(RPC::JsonContext& context) +doPeerReservationsAdd(rpc::JsonContext& context) { auto const& params = context.params; if (!params.isMember(jss::public_key)) - return RPC::missingFieldError(jss::public_key); + return rpc::missingFieldError(jss::public_key); // Returning JSON from every function ruins any attempt to encapsulate // the pattern of "get field F as type T, and diagnose an error if it is @@ -36,7 +36,7 @@ doPeerReservationsAdd(RPC::JsonContext& context) // essentially an optional (the "maybe monad" in Haskell) with a non-unit // type for the failure case to capture more information. if (!params[jss::public_key].isString()) - return RPC::expectedFieldError(jss::public_key, "a string"); + return rpc::expectedFieldError(jss::public_key, "a string"); // Same for the pattern of "if field F is present, make sure it has type T // and get it". @@ -44,7 +44,7 @@ doPeerReservationsAdd(RPC::JsonContext& context) if (params.isMember(jss::description)) { if (!params[jss::description].isString()) - return RPC::expectedFieldError(jss::description, "a string"); + return rpc::expectedFieldError(jss::description, "a string"); desc = params[jss::description].asString(); } diff --git a/src/xrpld/rpc/handlers/admin/peer/PeerReservationsDel.cpp b/src/xrpld/rpc/handlers/admin/peer/PeerReservationsDel.cpp index c2a8319876..e5912a5eca 100644 --- a/src/xrpld/rpc/handlers/admin/peer/PeerReservationsDel.cpp +++ b/src/xrpld/rpc/handlers/admin/peer/PeerReservationsDel.cpp @@ -14,15 +14,15 @@ namespace xrpl { json::Value -doPeerReservationsDel(RPC::JsonContext& context) +doPeerReservationsDel(rpc::JsonContext& context) { auto const& params = context.params; // We repeat much of the parameter parsing from `doPeerReservationsAdd`. if (!params.isMember(jss::public_key)) - return RPC::missingFieldError(jss::public_key); + return rpc::missingFieldError(jss::public_key); if (!params[jss::public_key].isString()) - return RPC::expectedFieldError(jss::public_key, "a string"); + return rpc::expectedFieldError(jss::public_key, "a string"); std::optional optPk = parseBase58(TokenType::NodePublic, params[jss::public_key].asString()); diff --git a/src/xrpld/rpc/handlers/admin/peer/PeerReservationsList.cpp b/src/xrpld/rpc/handlers/admin/peer/PeerReservationsList.cpp index e0204159fd..30e19a5c0b 100644 --- a/src/xrpld/rpc/handlers/admin/peer/PeerReservationsList.cpp +++ b/src/xrpld/rpc/handlers/admin/peer/PeerReservationsList.cpp @@ -8,7 +8,7 @@ namespace xrpl { json::Value -doPeerReservationsList(RPC::JsonContext& context) +doPeerReservationsList(rpc::JsonContext& context) { auto const& reservations = context.app.getPeerReservations().list(); // Enumerate the reservations in context.app.getPeerReservations() diff --git a/src/xrpld/rpc/handlers/admin/peer/Peers.cpp b/src/xrpld/rpc/handlers/admin/peer/Peers.cpp index ab14325f0e..99f069e27e 100644 --- a/src/xrpld/rpc/handlers/admin/peer/Peers.cpp +++ b/src/xrpld/rpc/handlers/admin/peer/Peers.cpp @@ -17,7 +17,7 @@ namespace xrpl { json::Value -doPeers(RPC::JsonContext& context) +doPeers(rpc::JsonContext& context) { json::Value jvResult(json::ValueType::Object); diff --git a/src/xrpld/rpc/handlers/admin/server_control/LedgerAccept.cpp b/src/xrpld/rpc/handlers/admin/server_control/LedgerAccept.cpp index ce06a6e480..00a259bb52 100644 --- a/src/xrpld/rpc/handlers/admin/server_control/LedgerAccept.cpp +++ b/src/xrpld/rpc/handlers/admin/server_control/LedgerAccept.cpp @@ -12,7 +12,7 @@ namespace xrpl { json::Value -doLedgerAccept(RPC::JsonContext& context) +doLedgerAccept(rpc::JsonContext& context) { json::Value jvResult; diff --git a/src/xrpld/rpc/handlers/admin/server_control/Stop.cpp b/src/xrpld/rpc/handlers/admin/server_control/Stop.cpp index 3e86bd4632..949eccf1e1 100644 --- a/src/xrpld/rpc/handlers/admin/server_control/Stop.cpp +++ b/src/xrpld/rpc/handlers/admin/server_control/Stop.cpp @@ -6,15 +6,15 @@ namespace xrpl { -namespace RPC { +namespace rpc { struct JsonContext; -} // namespace RPC +} // namespace rpc json::Value -doStop(RPC::JsonContext& context) +doStop(rpc::JsonContext& context) { context.app.signalStop("RPC"); - return RPC::makeObjectValue(systemName() + " server stopping"); + return rpc::makeObjectValue(systemName() + " server stopping"); } } // namespace xrpl diff --git a/src/xrpld/rpc/handlers/admin/signing/ChannelAuthorize.cpp b/src/xrpld/rpc/handlers/admin/signing/ChannelAuthorize.cpp index be3ce13d45..d97ce9dac4 100644 --- a/src/xrpld/rpc/handlers/admin/signing/ChannelAuthorize.cpp +++ b/src/xrpld/rpc/handlers/admin/signing/ChannelAuthorize.cpp @@ -30,34 +30,34 @@ namespace xrpl { // drops: 64-bit uint (as string) // } json::Value -doChannelAuthorize(RPC::JsonContext& context) +doChannelAuthorize(rpc::JsonContext& context) { if (context.role != Role::ADMIN && !context.app.config().canSign()) { - return RPC::makeError(RpcNotSupported, "Signing is not supported by this server."); + return rpc::makeError(RpcNotSupported, "Signing is not supported by this server."); } auto const& params(context.params); for (auto const& p : {jss::channel_id, jss::amount}) { if (!params.isMember(p)) - return RPC::missingFieldError(p); + return rpc::missingFieldError(p); } // Compatibility if a key type isn't specified. If it is, the // keypairForSignature code will validate parameters and return // the appropriate error. if (!params.isMember(jss::key_type) && !params.isMember(jss::secret)) - return RPC::missingFieldError(jss::secret); + return rpc::missingFieldError(jss::secret); json::Value result; std::optional> const keyPair = - RPC::keypairForSignature(params, result, context.apiVersion); + rpc::keypairForSignature(params, result, context.apiVersion); XRPL_ASSERT( - keyPair || RPC::containsError(result), + keyPair || rpc::containsError(result), "xrpl::doChannelAuthorize : valid keyPair or an error"); - if (!keyPair || RPC::containsError(result)) + if (!keyPair || rpc::containsError(result)) return result; PublicKey const& pk = keyPair->first; @@ -86,7 +86,7 @@ doChannelAuthorize(RPC::JsonContext& context) catch (std::exception const& ex) { // LCOV_EXCL_START - result = RPC::makeError( + result = rpc::makeError( RpcInternal, "Exception occurred during signing: " + std::string(ex.what())); // LCOV_EXCL_STOP } diff --git a/src/xrpld/rpc/handlers/admin/signing/Sign.cpp b/src/xrpld/rpc/handlers/admin/signing/Sign.cpp index 781e160f54..6aac058a56 100644 --- a/src/xrpld/rpc/handlers/admin/signing/Sign.cpp +++ b/src/xrpld/rpc/handlers/admin/signing/Sign.cpp @@ -15,18 +15,18 @@ namespace xrpl { // secret: // } json::Value -doSign(RPC::JsonContext& context) +doSign(rpc::JsonContext& context) { if (context.role != Role::ADMIN && !context.app.config().canSign()) { - return RPC::makeError(RpcNotSupported, "Signing is not supported by this server."); + return rpc::makeError(RpcNotSupported, "Signing is not supported by this server."); } - context.loadType = Resource::kFeeHeavyBurdenRpc; + context.loadType = resource::kFeeHeavyBurdenRpc; NetworkOPs::FailHard const failType = NetworkOPs::doFailHard( context.params.isMember(jss::fail_hard) && context.params[jss::fail_hard].asBool()); - auto ret = RPC::transactionSign( + auto ret = rpc::transactionSign( context.params, context.apiVersion, failType, diff --git a/src/xrpld/rpc/handlers/admin/signing/SignFor.cpp b/src/xrpld/rpc/handlers/admin/signing/SignFor.cpp index 35274d51f4..2b9c830647 100644 --- a/src/xrpld/rpc/handlers/admin/signing/SignFor.cpp +++ b/src/xrpld/rpc/handlers/admin/signing/SignFor.cpp @@ -16,18 +16,18 @@ namespace xrpl { // secret: // } json::Value -doSignFor(RPC::JsonContext& context) +doSignFor(rpc::JsonContext& context) { if (context.role != Role::ADMIN && !context.app.config().canSign()) { - return RPC::makeError(RpcNotSupported, "Signing is not supported by this server."); + return rpc::makeError(RpcNotSupported, "Signing is not supported by this server."); } - context.loadType = Resource::kFeeHeavyBurdenRpc; + context.loadType = resource::kFeeHeavyBurdenRpc; auto const failHard = context.params[jss::fail_hard].asBool(); auto const failType = NetworkOPs::doFailHard(failHard); - auto ret = RPC::transactionSignFor( + auto ret = rpc::transactionSignFor( context.params, context.apiVersion, failType, diff --git a/src/xrpld/rpc/handlers/admin/status/ConsensusInfo.cpp b/src/xrpld/rpc/handlers/admin/status/ConsensusInfo.cpp index 341980ba6d..8017e7058b 100644 --- a/src/xrpld/rpc/handlers/admin/status/ConsensusInfo.cpp +++ b/src/xrpld/rpc/handlers/admin/status/ConsensusInfo.cpp @@ -7,7 +7,7 @@ namespace xrpl { json::Value -doConsensusInfo(RPC::JsonContext& context) +doConsensusInfo(rpc::JsonContext& context) { json::Value ret(json::ValueType::Object); diff --git a/src/xrpld/rpc/handlers/admin/status/FetchInfo.cpp b/src/xrpld/rpc/handlers/admin/status/FetchInfo.cpp index 16e4789025..ca9bff31f5 100644 --- a/src/xrpld/rpc/handlers/admin/status/FetchInfo.cpp +++ b/src/xrpld/rpc/handlers/admin/status/FetchInfo.cpp @@ -7,7 +7,7 @@ namespace xrpl { json::Value -doFetchInfo(RPC::JsonContext& context) +doFetchInfo(rpc::JsonContext& context) { json::Value ret(json::ValueType::Object); diff --git a/src/xrpld/rpc/handlers/admin/status/GetCounts.cpp b/src/xrpld/rpc/handlers/admin/status/GetCounts.cpp index 789c6dcf17..421f23d237 100644 --- a/src/xrpld/rpc/handlers/admin/status/GetCounts.cpp +++ b/src/xrpld/rpc/handlers/admin/status/GetCounts.cpp @@ -111,7 +111,7 @@ getCountsJson(Application& app, int minObjectCount) // min_count: // optional, defaults to 10 // } json::Value -doGetCounts(RPC::JsonContext& context) +doGetCounts(rpc::JsonContext& context) { int minCount = 10; diff --git a/src/xrpld/rpc/handlers/admin/status/Print.cpp b/src/xrpld/rpc/handlers/admin/status/Print.cpp index 99fd01c9f4..1e1f7f0662 100644 --- a/src/xrpld/rpc/handlers/admin/status/Print.cpp +++ b/src/xrpld/rpc/handlers/admin/status/Print.cpp @@ -8,7 +8,7 @@ namespace xrpl { json::Value -doPrint(RPC::JsonContext& context) +doPrint(rpc::JsonContext& context) { JsonPropertyStream stream; if (context.params.isObject() && context.params[jss::params].isArray() && diff --git a/src/xrpld/rpc/handlers/admin/status/ValidatorInfo.cpp b/src/xrpld/rpc/handlers/admin/status/ValidatorInfo.cpp index efe1529ccb..705e03c1a0 100644 --- a/src/xrpld/rpc/handlers/admin/status/ValidatorInfo.cpp +++ b/src/xrpld/rpc/handlers/admin/status/ValidatorInfo.cpp @@ -12,12 +12,12 @@ namespace xrpl { json::Value -doValidatorInfo(RPC::JsonContext& context) +doValidatorInfo(rpc::JsonContext& context) { // return error if not configured as validator auto const validationPK = context.app.getValidationPublicKey(); if (!validationPK) - return RPC::notValidatorError(); + return rpc::notValidatorError(); json::Value ret; diff --git a/src/xrpld/rpc/handlers/admin/status/ValidatorListSites.cpp b/src/xrpld/rpc/handlers/admin/status/ValidatorListSites.cpp index 7497105c50..9bc8b6bcda 100644 --- a/src/xrpld/rpc/handlers/admin/status/ValidatorListSites.cpp +++ b/src/xrpld/rpc/handlers/admin/status/ValidatorListSites.cpp @@ -7,7 +7,7 @@ namespace xrpl { json::Value -doValidatorListSites(RPC::JsonContext& context) +doValidatorListSites(rpc::JsonContext& context) { return context.app.getValidatorSites().getJson(); } diff --git a/src/xrpld/rpc/handlers/admin/status/Validators.cpp b/src/xrpld/rpc/handlers/admin/status/Validators.cpp index 48e4466861..d605a38f1b 100644 --- a/src/xrpld/rpc/handlers/admin/status/Validators.cpp +++ b/src/xrpld/rpc/handlers/admin/status/Validators.cpp @@ -7,7 +7,7 @@ namespace xrpl { json::Value -doValidators(RPC::JsonContext& context) +doValidators(rpc::JsonContext& context) { return context.app.getValidators().getJson(); } diff --git a/src/xrpld/rpc/handlers/ledger/Ledger.cpp b/src/xrpld/rpc/handlers/ledger/Ledger.cpp index 23a97a5026..51f5bdf348 100644 --- a/src/xrpld/rpc/handlers/ledger/Ledger.cpp +++ b/src/xrpld/rpc/handlers/ledger/Ledger.cpp @@ -34,7 +34,7 @@ #include namespace xrpl { -namespace RPC { +namespace rpc { LedgerHandler::LedgerHandler(JsonContext& context) : context_(context) { @@ -107,7 +107,7 @@ LedgerHandler::check() { return RpcTooBusy; } - context_.loadType = binary ? Resource::kFeeMediumBurdenRpc : Resource::kFeeHeavyBurdenRpc; + context_.loadType = binary ? resource::kFeeMediumBurdenRpc : resource::kFeeHeavyBurdenRpc; } if (*queue) @@ -162,10 +162,10 @@ LedgerHandler::writeResult(json::Value& value) value[jss::warnings] = std::move(warnings); } -} // namespace RPC +} // namespace rpc std::pair -doLedgerGrpc(RPC::GRPCContext& context) +doLedgerGrpc(rpc::GRPCContext& context) { auto begin = std::chrono::system_clock::now(); org::xrpl::rpc::v1::GetLedgerRequest const& request = context.params; @@ -173,7 +173,7 @@ doLedgerGrpc(RPC::GRPCContext& context) grpc::Status const status = grpc::Status::OK; std::shared_ptr ledger; - if (auto status = RPC::ledgerFromRequest(ledger, context)) + if (auto status = rpc::ledgerFromRequest(ledger, context)) { grpc::Status errorStatus; if (status.toErrorCode() == RpcInvalidParams) diff --git a/src/xrpld/rpc/handlers/ledger/Ledger.h b/src/xrpld/rpc/handlers/ledger/Ledger.h index 59b64832f7..07d24b497d 100644 --- a/src/xrpld/rpc/handlers/ledger/Ledger.h +++ b/src/xrpld/rpc/handlers/ledger/Ledger.h @@ -18,7 +18,7 @@ namespace json { class Object; } // namespace json -namespace xrpl::RPC { +namespace xrpl::rpc { struct JsonContext; @@ -42,9 +42,9 @@ public: // NOLINTBEGIN(readability-identifier-naming) static constexpr char name[] = "ledger"; - static constexpr unsigned minApiVer = RPC::kApiMinimumSupportedVersion; + static constexpr unsigned minApiVer = rpc::kApiMinimumSupportedVersion; - static constexpr unsigned maxApiVer = RPC::kApiMaximumValidVersion; + static constexpr unsigned maxApiVer = rpc::kApiMaximumValidVersion; static constexpr Role role = Role::USER; @@ -59,4 +59,4 @@ private: int options_ = 0; }; -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/handlers/ledger/LedgerClosed.cpp b/src/xrpld/rpc/handlers/ledger/LedgerClosed.cpp index 7ea314292f..def0a73cd3 100644 --- a/src/xrpld/rpc/handlers/ledger/LedgerClosed.cpp +++ b/src/xrpld/rpc/handlers/ledger/LedgerClosed.cpp @@ -10,7 +10,7 @@ namespace xrpl { json::Value -doLedgerClosed(RPC::JsonContext& context) +doLedgerClosed(rpc::JsonContext& context) { auto ledger = context.ledgerMaster.getClosedLedger(); XRPL_ASSERT(ledger, "xrpl::doLedgerClosed : non-null closed ledger"); diff --git a/src/xrpld/rpc/handlers/ledger/LedgerCurrent.cpp b/src/xrpld/rpc/handlers/ledger/LedgerCurrent.cpp index 1d05774163..ac04084848 100644 --- a/src/xrpld/rpc/handlers/ledger/LedgerCurrent.cpp +++ b/src/xrpld/rpc/handlers/ledger/LedgerCurrent.cpp @@ -8,7 +8,7 @@ namespace xrpl { json::Value -doLedgerCurrent(RPC::JsonContext& context) +doLedgerCurrent(rpc::JsonContext& context) { json::Value jvResult; jvResult[jss::ledger_current_index] = context.ledgerMaster.getCurrentLedgerIndex(); diff --git a/src/xrpld/rpc/handlers/ledger/LedgerData.cpp b/src/xrpld/rpc/handlers/ledger/LedgerData.cpp index 64ab30374b..697d8e52b8 100644 --- a/src/xrpld/rpc/handlers/ledger/LedgerData.cpp +++ b/src/xrpld/rpc/handlers/ledger/LedgerData.cpp @@ -36,12 +36,12 @@ namespace xrpl { // state: array of state nodes // marker: resume point, if any json::Value -doLedgerData(RPC::JsonContext& context) +doLedgerData(rpc::JsonContext& context) { std::shared_ptr lpLedger; auto const& params = context.params; - auto jvResult = RPC::lookupLedger(lpLedger, context); + auto jvResult = rpc::lookupLedger(lpLedger, context); if (!lpLedger) return jvResult; @@ -51,14 +51,14 @@ doLedgerData(RPC::JsonContext& context) { json::Value const& jMarker = params[jss::marker]; if (!(jMarker.isString() && key.parseHex(jMarker.asString()))) - return RPC::expectedFieldError(jss::marker, "valid"); + return rpc::expectedFieldError(jss::marker, "valid"); } bool isBinary = false; if (params.isMember(jss::binary)) { if (!params[jss::binary].isBool()) - return RPC::expectedFieldError(jss::binary, "boolean"); + return rpc::expectedFieldError(jss::binary, "boolean"); isBinary = params[jss::binary].asBool(); } @@ -67,12 +67,12 @@ doLedgerData(RPC::JsonContext& context) { json::Value const& jLimit = params[jss::limit]; if (!jLimit.isIntegral()) - return RPC::expectedFieldError(jss::limit, "integer"); + return rpc::expectedFieldError(jss::limit, "integer"); limit = jLimit.asInt(); } - auto maxLimit = RPC::Tuning::pageLength(isBinary); + auto maxLimit = rpc::tuning::pageLength(isBinary); if ((limit < 0) || ((limit > maxLimit) && (!isUnlimited(context.role)))) limit = maxLimit; @@ -86,7 +86,7 @@ doLedgerData(RPC::JsonContext& context) *lpLedger, &context, isBinary ? static_cast(LedgerFill::Options::Binary) : 0)); } - auto [rpcStatus, type] = RPC::chooseLedgerEntryType(params); + auto [rpcStatus, type] = rpc::chooseLedgerEntryType(params); if (rpcStatus) { jvResult.clear(); @@ -131,14 +131,14 @@ doLedgerData(RPC::JsonContext& context) } std::pair -doLedgerDataGrpc(RPC::GRPCContext& context) +doLedgerDataGrpc(rpc::GRPCContext& context) { org::xrpl::rpc::v1::GetLedgerDataRequest const& request = context.params; org::xrpl::rpc::v1::GetLedgerDataResponse response; grpc::Status const status = grpc::Status::OK; std::shared_ptr ledger; - if (auto status = RPC::ledgerFromRequest(ledger, context)) + if (auto status = rpc::ledgerFromRequest(ledger, context)) { grpc::Status errorStatus; if (status.toErrorCode() == RpcInvalidParams) @@ -177,7 +177,7 @@ doLedgerDataGrpc(RPC::GRPCContext& con e = ledger->sles.upperBound(*key); } - int maxLimit = RPC::Tuning::pageLength(true); + int maxLimit = rpc::tuning::pageLength(true); for (auto i = ledger->sles.upperBound(startKey); i != e; ++i) { diff --git a/src/xrpld/rpc/handlers/ledger/LedgerDiff.cpp b/src/xrpld/rpc/handlers/ledger/LedgerDiff.cpp index f1a9253de2..5e83bedf08 100644 --- a/src/xrpld/rpc/handlers/ledger/LedgerDiff.cpp +++ b/src/xrpld/rpc/handlers/ledger/LedgerDiff.cpp @@ -15,7 +15,7 @@ namespace xrpl { std::pair -doLedgerDiffGrpc(RPC::GRPCContext& context) +doLedgerDiffGrpc(rpc::GRPCContext& context) { org::xrpl::rpc::v1::GetLedgerDiffRequest const& request = context.params; org::xrpl::rpc::v1::GetLedgerDiffResponse response; @@ -24,13 +24,13 @@ doLedgerDiffGrpc(RPC::GRPCContext& con std::shared_ptr baseLedgerRv; std::shared_ptr desiredLedgerRv; - if (RPC::ledgerFromSpecifier(baseLedgerRv, request.base_ledger(), context)) + if (rpc::ledgerFromSpecifier(baseLedgerRv, request.base_ledger(), context)) { grpc::Status const errorStatus{grpc::StatusCode::NOT_FOUND, "base ledger not found"}; return {response, errorStatus}; } - if (RPC::ledgerFromSpecifier(desiredLedgerRv, request.desired_ledger(), context)) + if (rpc::ledgerFromSpecifier(desiredLedgerRv, request.desired_ledger(), context)) { grpc::Status const errorStatus{grpc::StatusCode::NOT_FOUND, "desired ledger not found"}; return {response, errorStatus}; diff --git a/src/xrpld/rpc/handlers/ledger/LedgerEntry.cpp b/src/xrpld/rpc/handlers/ledger/LedgerEntry.cpp index 784be779bb..0dd52b6776 100644 --- a/src/xrpld/rpc/handlers/ledger/LedgerEntry.cpp +++ b/src/xrpld/rpc/handlers/ledger/LedgerEntry.cpp @@ -66,11 +66,11 @@ parseObjectID( json::StaticString const fieldName, std::string const& expectedType = "hex string or object") { - if (auto const uNodeIndex = LedgerEntryHelpers::parse(params)) + if (auto const uNodeIndex = ledger_entry_helpers::parse(params)) { return *uNodeIndex; } - return LedgerEntryHelpers::invalidFieldError("malformedRequest", fieldName, expectedType); + return ledger_entry_helpers::invalidFieldError("malformedRequest", fieldName, expectedType); } static std::expected @@ -101,12 +101,12 @@ parseAccountRoot( json::StaticString const fieldName, [[maybe_unused]] unsigned const apiVersion) { - if (auto const account = LedgerEntryHelpers::parse(params)) + if (auto const account = ledger_entry_helpers::parse(params)) { return keylet::account(*account).key; } - return LedgerEntryHelpers::invalidFieldError("malformedAddress", fieldName, "AccountID"); + return ledger_entry_helpers::invalidFieldError("malformedAddress", fieldName, "AccountID"); } auto const parseAmendments = fixed(keylet::amendments()); @@ -122,17 +122,18 @@ parseAMM( return parseObjectID(params, fieldName); } - if (auto const value = LedgerEntryHelpers::hasRequired(params, {jss::asset, jss::asset2}); + if (auto const value = ledger_entry_helpers::hasRequired(params, {jss::asset, jss::asset2}); !value) { return std::unexpected(value.error()); } - auto const asset = LedgerEntryHelpers::requiredAsset(params, jss::asset, "malformedRequest"); + auto const asset = ledger_entry_helpers::requiredAsset(params, jss::asset, "malformedRequest"); if (!asset) return std::unexpected(asset.error()); - auto const asset2 = LedgerEntryHelpers::requiredAsset(params, jss::asset2, "malformedRequest"); + auto const asset2 = + ledger_entry_helpers::requiredAsset(params, jss::asset2, "malformedRequest"); if (!asset2) return std::unexpected(asset2.error()); @@ -147,7 +148,7 @@ parseBridge( { if (!params.isMember(jss::bridge)) { - return std::unexpected(LedgerEntryHelpers::missingFieldError(jss::bridge)); + return std::unexpected(ledger_entry_helpers::missingFieldError(jss::bridge)); } if (params[jss::bridge].isString()) @@ -155,11 +156,11 @@ parseBridge( return parseObjectID(params, fieldName); } - auto const bridge = LedgerEntryHelpers::parseBridgeFields(params[jss::bridge]); + auto const bridge = ledger_entry_helpers::parseBridgeFields(params[jss::bridge]); if (!bridge) return std::unexpected(bridge.error()); - auto const account = LedgerEntryHelpers::requiredAccountID( + auto const account = ledger_entry_helpers::requiredAccountID( params, jss::bridge_account, "malformedBridgeAccount"); if (!account) return std::unexpected(account.error()); @@ -167,7 +168,7 @@ parseBridge( STXChainBridge::ChainType const chainType = STXChainBridge::srcChain(account.value() == bridge->lockingChainDoor()); if (account.value() != bridge->door(chainType)) - return LedgerEntryHelpers::malformedError("malformedRequest", ""); + return ledger_entry_helpers::malformedError("malformedRequest", ""); return keylet::bridge(*bridge, chainType).key; } @@ -193,16 +194,16 @@ parseCredential( } auto const subject = - LedgerEntryHelpers::requiredAccountID(cred, jss::subject, "malformedRequest"); + ledger_entry_helpers::requiredAccountID(cred, jss::subject, "malformedRequest"); if (!subject) return std::unexpected(subject.error()); auto const issuer = - LedgerEntryHelpers::requiredAccountID(cred, jss::issuer, "malformedRequest"); + ledger_entry_helpers::requiredAccountID(cred, jss::issuer, "malformedRequest"); if (!issuer) return std::unexpected(issuer.error()); - auto const credType = LedgerEntryHelpers::requiredHexBlob( + auto const credType = ledger_entry_helpers::requiredHexBlob( cred, jss::credential_type, kMaxCredentialTypeLength, "malformedRequest"); if (!credType) return std::unexpected(credType.error()); @@ -222,12 +223,12 @@ parseDelegate( } auto const account = - LedgerEntryHelpers::requiredAccountID(params, jss::account, "malformedAddress"); + ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress"); if (!account) return std::unexpected(account.error()); auto const authorize = - LedgerEntryHelpers::requiredAccountID(params, jss::authorize, "malformedAddress"); + ledger_entry_helpers::requiredAccountID(params, jss::authorize, "malformedAddress"); if (!authorize) return std::unexpected(authorize.error()); @@ -239,7 +240,7 @@ parseAuthorizeCredentials(json::Value const& jv) { if (!jv.isArray()) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedAuthorizedCredentials", jss::authorized_credentials, "array"); } @@ -247,7 +248,7 @@ parseAuthorizeCredentials(json::Value const& jv) if (n > kMaxCredentialsArraySize) { return std::unexpected( - LedgerEntryHelpers::malformedError( + ledger_entry_helpers::malformedError( "malformedAuthorizedCredentials", "Invalid field '" + std::string(jss::authorized_credentials) + "', array too long.")); @@ -256,7 +257,7 @@ parseAuthorizeCredentials(json::Value const& jv) if (n == 0) { return std::unexpected( - LedgerEntryHelpers::malformedError( + ledger_entry_helpers::malformedError( "malformedAuthorizedCredentials", "Invalid field '" + std::string(jss::authorized_credentials) + "', array empty.")); } @@ -266,23 +267,23 @@ parseAuthorizeCredentials(json::Value const& jv) { if (!jo.isObject()) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedAuthorizedCredentials", jss::authorized_credentials, "array of objects"); } - if (auto const value = LedgerEntryHelpers::hasRequired( + if (auto const value = ledger_entry_helpers::hasRequired( jo, {jss::issuer, jss::credential_type}, "malformedAuthorizedCredentials"); !value) { return std::unexpected(value.error()); } - auto const issuer = LedgerEntryHelpers::requiredAccountID( + auto const issuer = ledger_entry_helpers::requiredAccountID( jo, jss::issuer, "malformedAuthorizedCredentials"); if (!issuer) return std::unexpected(issuer.error()); - auto const credentialType = LedgerEntryHelpers::requiredHexBlob( + auto const credentialType = ledger_entry_helpers::requiredHexBlob( jo, jss::credential_type, kMaxCredentialTypeLength, "malformedAuthorizedCredentials"); if (!credentialType) return std::unexpected(credentialType.error()); @@ -309,13 +310,13 @@ parseDepositPreauth( if ((dp.isMember(jss::authorized) == dp.isMember(jss::authorized_credentials))) { - return LedgerEntryHelpers::malformedError( + return ledger_entry_helpers::malformedError( "malformedRequest", "Must have exactly one of `authorized` and " "`authorized_credentials`."); } - auto const owner = LedgerEntryHelpers::requiredAccountID(dp, jss::owner, "malformedOwner"); + auto const owner = ledger_entry_helpers::requiredAccountID(dp, jss::owner, "malformedOwner"); if (!owner) { return std::unexpected(owner.error()); @@ -323,11 +324,11 @@ parseDepositPreauth( if (dp.isMember(jss::authorized)) { - if (auto const authorized = LedgerEntryHelpers::parse(dp[jss::authorized])) + if (auto const authorized = ledger_entry_helpers::parse(dp[jss::authorized])) { return keylet::depositPreauth(*owner, *authorized).key; } - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedAuthorized", jss::authorized, "AccountID"); } @@ -340,7 +341,7 @@ parseDepositPreauth( if (sorted.empty()) { // TODO: this error message is bad/inaccurate - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedAuthorizedCredentials", jss::authorized_credentials, "array"); } @@ -353,10 +354,10 @@ parseDID( json::StaticString const fieldName, [[maybe_unused]] unsigned const apiVersion) { - auto const account = LedgerEntryHelpers::parse(params); + auto const account = ledger_entry_helpers::parse(params); if (!account) { - return LedgerEntryHelpers::invalidFieldError("malformedAddress", fieldName, "AccountID"); + return ledger_entry_helpers::invalidFieldError("malformedAddress", fieldName, "AccountID"); } return keylet::did(*account).key; @@ -377,12 +378,13 @@ parseDirectoryNode( (!params[jss::sub_index].isConvertibleTo(json::ValueType::UInt) || params[jss::sub_index].isBool())) { - return LedgerEntryHelpers::invalidFieldError("malformedRequest", jss::sub_index, "number"); + return ledger_entry_helpers::invalidFieldError( + "malformedRequest", jss::sub_index, "number"); } if (params.isMember(jss::owner) == params.isMember(jss::dir_root)) { - return LedgerEntryHelpers::malformedError( + return ledger_entry_helpers::malformedError( "malformedRequest", "Must have exactly one of `owner` and `dir_root` fields."); } @@ -390,27 +392,27 @@ parseDirectoryNode( if (params.isMember(jss::dir_root)) { - if (auto const uDirRoot = LedgerEntryHelpers::parse(params[jss::dir_root])) + if (auto const uDirRoot = ledger_entry_helpers::parse(params[jss::dir_root])) { return keylet::page(*uDirRoot, uSubIndex).key; } - return LedgerEntryHelpers::invalidFieldError("malformedDirRoot", jss::dir_root, "hash"); + return ledger_entry_helpers::invalidFieldError("malformedDirRoot", jss::dir_root, "hash"); } if (params.isMember(jss::owner)) { - auto const ownerID = LedgerEntryHelpers::parse(params[jss::owner]); + auto const ownerID = ledger_entry_helpers::parse(params[jss::owner]); if (!ownerID) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedAddress", jss::owner, "AccountID"); } return keylet::page(keylet::ownerDir(*ownerID), uSubIndex).key; } - return LedgerEntryHelpers::malformedError("malformedRequest", ""); + return ledger_entry_helpers::malformedError("malformedRequest", ""); } static std::expected @@ -424,10 +426,10 @@ parseEscrow( return parseObjectID(params, fieldName); } - auto const id = LedgerEntryHelpers::requiredAccountID(params, jss::owner, "malformedOwner"); + auto const id = ledger_entry_helpers::requiredAccountID(params, jss::owner, "malformedOwner"); if (!id) return std::unexpected(id.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32(params, jss::seq, "malformedSeq"); + auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedSeq"); if (!seq) return std::unexpected(seq.error()); @@ -449,7 +451,7 @@ parseFixed( } if (!params.asBool()) { - return LedgerEntryHelpers::invalidFieldError("invalidParams", fieldName, "true"); + return ledger_entry_helpers::invalidFieldError("invalidParams", fieldName, "true"); } return keylet.key; @@ -486,10 +488,10 @@ parseLoanBroker( return parseObjectID(params, fieldName, "hex string"); } - auto const id = LedgerEntryHelpers::requiredAccountID(params, jss::owner, "malformedOwner"); + auto const id = ledger_entry_helpers::requiredAccountID(params, jss::owner, "malformedOwner"); if (!id) return std::unexpected(id.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32(params, jss::seq, "malformedSeq"); + auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedSeq"); if (!seq) return std::unexpected(seq.error()); @@ -508,10 +510,10 @@ parseLoan( } auto const id = - LedgerEntryHelpers::requiredUInt256(params, jss::loan_broker_id, "malformedBroker"); + ledger_entry_helpers::requiredUInt256(params, jss::loan_broker_id, "malformedBroker"); if (!id) return std::unexpected(id.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32(params, jss::loan_seq, "malformedSeq"); + auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::loan_seq, "malformedSeq"); if (!seq) return std::unexpected(seq.error()); @@ -529,13 +531,13 @@ parseMPToken( return parseObjectID(params, fieldName); } - auto const mptIssuanceID = - LedgerEntryHelpers::requiredUInt192(params, jss::mpt_issuance_id, "malformedMPTIssuanceID"); + auto const mptIssuanceID = ledger_entry_helpers::requiredUInt192( + params, jss::mpt_issuance_id, "malformedMPTIssuanceID"); if (!mptIssuanceID) return std::unexpected(mptIssuanceID.error()); auto const account = - LedgerEntryHelpers::requiredAccountID(params, jss::account, "malformedAccount"); + ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAccount"); if (!account) return std::unexpected(account.error()); @@ -548,10 +550,10 @@ parseMPTokenIssuance( json::StaticString const fieldName, [[maybe_unused]] unsigned const apiVersion) { - auto const mptIssuanceID = LedgerEntryHelpers::parse(params); + auto const mptIssuanceID = ledger_entry_helpers::parse(params); if (!mptIssuanceID) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedMPTokenIssuance", fieldName, "Hash192"); } @@ -589,11 +591,12 @@ parseOffer( return parseObjectID(params, fieldName); } - auto const id = LedgerEntryHelpers::requiredAccountID(params, jss::account, "malformedAddress"); + auto const id = + ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress"); if (!id) return std::unexpected(id.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32(params, jss::seq, "malformedRequest"); + auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedRequest"); if (!seq) return std::unexpected(seq.error()); @@ -611,12 +614,13 @@ parseOracle( return parseObjectID(params, fieldName); } - auto const id = LedgerEntryHelpers::requiredAccountID(params, jss::account, "malformedAccount"); + auto const id = + ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAccount"); if (!id) return std::unexpected(id.error()); - auto const seq = - LedgerEntryHelpers::requiredUInt32(params, jss::oracle_document_id, "malformedDocumentID"); + auto const seq = ledger_entry_helpers::requiredUInt32( + params, jss::oracle_document_id, "malformedDocumentID"); if (!seq) return std::unexpected(seq.error()); @@ -645,16 +649,16 @@ parsePermissionedDomain( if (!pd.isObject()) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedRequest", fieldName, "hex string or object"); } auto const account = - LedgerEntryHelpers::requiredAccountID(pd, jss::account, "malformedAddress"); + ledger_entry_helpers::requiredAccountID(pd, jss::account, "malformedAddress"); if (!account) return std::unexpected(account.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32(pd, jss::seq, "malformedRequest"); + auto const seq = ledger_entry_helpers::requiredUInt32(pd, jss::seq, "malformedRequest"); if (!seq) return std::unexpected(seq.error()); @@ -675,7 +679,7 @@ parseRippleState( } if (auto const value = - LedgerEntryHelpers::hasRequired(jvRippleState, {jss::currency, jss::accounts}); + ledger_entry_helpers::hasRequired(jvRippleState, {jss::currency, jss::accounts}); !value) { return std::unexpected(value.error()); @@ -683,27 +687,27 @@ parseRippleState( if (!jvRippleState[jss::accounts].isArray() || jvRippleState[jss::accounts].size() != 2) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedRequest", jss::accounts, "length-2 array of Accounts"); } - auto const id1 = LedgerEntryHelpers::parse(jvRippleState[jss::accounts][0u]); - auto const id2 = LedgerEntryHelpers::parse(jvRippleState[jss::accounts][1u]); + auto const id1 = ledger_entry_helpers::parse(jvRippleState[jss::accounts][0u]); + auto const id2 = ledger_entry_helpers::parse(jvRippleState[jss::accounts][1u]); if (!id1 || !id2) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedAddress", jss::accounts, "array of Accounts"); } if (id1 == id2) { - return LedgerEntryHelpers::malformedError( + return ledger_entry_helpers::malformedError( "malformedRequest", "Cannot have a trustline to self."); } if (!jvRippleState[jss::currency].isString() || jvRippleState[jss::currency] == "" || !toCurrency(uCurrency, jvRippleState[jss::currency].asString())) { - return LedgerEntryHelpers::invalidFieldError( + return ledger_entry_helpers::invalidFieldError( "malformedCurrency", jss::currency, "Currency"); } @@ -729,12 +733,12 @@ parseSponsorship( return parseObjectID(params, fieldName); auto const sponsorID = - LedgerEntryHelpers::requiredAccountID(params, jss::sponsor, "malformedSponsor"); + ledger_entry_helpers::requiredAccountID(params, jss::sponsor, "malformedSponsor"); if (!sponsorID) return std::unexpected(sponsorID.error()); auto const sponseeID = - LedgerEntryHelpers::requiredAccountID(params, jss::sponsee, "malformedSponsee"); + ledger_entry_helpers::requiredAccountID(params, jss::sponsee, "malformedSponsee"); if (!sponseeID) return std::unexpected(sponseeID.error()); @@ -752,12 +756,13 @@ parseTicket( return parseObjectID(params, fieldName); } - auto const id = LedgerEntryHelpers::requiredAccountID(params, jss::account, "malformedAddress"); + auto const id = + ledger_entry_helpers::requiredAccountID(params, jss::account, "malformedAddress"); if (!id) return std::unexpected(id.error()); auto const seq = - LedgerEntryHelpers::requiredUInt32(params, jss::ticket_seq, "malformedRequest"); + ledger_entry_helpers::requiredUInt32(params, jss::ticket_seq, "malformedRequest"); if (!seq) return std::unexpected(seq.error()); @@ -775,11 +780,11 @@ parseVault( return parseObjectID(params, fieldName); } - auto const id = LedgerEntryHelpers::requiredAccountID(params, jss::owner, "malformedOwner"); + auto const id = ledger_entry_helpers::requiredAccountID(params, jss::owner, "malformedOwner"); if (!id) return std::unexpected(id.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32(params, jss::seq, "malformedRequest"); + auto const seq = ledger_entry_helpers::requiredUInt32(params, jss::seq, "malformedRequest"); if (!seq) return std::unexpected(seq.error()); @@ -797,11 +802,11 @@ parseXChainOwnedClaimID( return parseObjectID(claimId, fieldName); } - auto const bridgeSpec = LedgerEntryHelpers::parseBridgeFields(claimId); + auto const bridgeSpec = ledger_entry_helpers::parseBridgeFields(claimId); if (!bridgeSpec) return std::unexpected(bridgeSpec.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32( + auto const seq = ledger_entry_helpers::requiredUInt32( claimId, jss::xchain_owned_claim_id, "malformedXChainOwnedClaimID"); if (!seq) { @@ -823,11 +828,11 @@ parseXChainOwnedCreateAccountClaimID( return parseObjectID(claimId, fieldName); } - auto const bridgeSpec = LedgerEntryHelpers::parseBridgeFields(claimId); + auto const bridgeSpec = ledger_entry_helpers::parseBridgeFields(claimId); if (!bridgeSpec) return std::unexpected(bridgeSpec.error()); - auto const seq = LedgerEntryHelpers::requiredUInt32( + auto const seq = ledger_entry_helpers::requiredUInt32( claimId, jss::xchain_owned_create_account_claim_id, "malformedXChainOwnedCreateAccountClaimID"); @@ -853,7 +858,7 @@ struct LedgerEntry // ... // } json::Value -doLedgerEntry(RPC::JsonContext& context) +doLedgerEntry(rpc::JsonContext& context) { static auto kLedgerEntryParsers = std::to_array({ #pragma push_macro("LEDGER_ENTRY") @@ -892,11 +897,11 @@ doLedgerEntry(RPC::JsonContext& context) if (hasMoreThanOneMember) { - return RPC::makeParamError("Too many fields provided."); + return rpc::makeParamError("Too many fields provided."); } std::shared_ptr lpLedger; - auto jvResult = RPC::lookupLedger(lpLedger, context); + auto jvResult = rpc::lookupLedger(lpLedger, context); if (!lpLedger) return jvResult; @@ -936,7 +941,7 @@ doLedgerEntry(RPC::JsonContext& context) jvResult[jss::error] = "unknownOption"; return jvResult; } - return RPC::makeParamError("No ledger_entry params provided."); + return rpc::makeParamError("No ledger_entry params provided."); } } catch (json::Error const& e) @@ -945,7 +950,7 @@ doLedgerEntry(RPC::JsonContext& context) { // For apiVersion 2 onwards, any parsing failures that throw // this exception return an invalidParam error. - return RPC::makeError(RpcInvalidParams); + return rpc::makeError(RpcInvalidParams); } throw; @@ -956,7 +961,7 @@ doLedgerEntry(RPC::JsonContext& context) if (uNodeIndex.isZero()) { - RPC::injectError(RpcEntryNotFound, jvResult); + rpc::injectError(RpcEntryNotFound, jvResult); return jvResult; } @@ -969,13 +974,13 @@ doLedgerEntry(RPC::JsonContext& context) if (!sleNode) { // Not found. - RPC::injectError(RpcEntryNotFound, jvResult); + rpc::injectError(RpcEntryNotFound, jvResult); return jvResult; } if ((expectedType != ltANY) && (expectedType != sleNode->getType())) { - RPC::injectError(RpcUnexpectedLedgerType, jvResult); + rpc::injectError(RpcUnexpectedLedgerType, jvResult); return jvResult; } @@ -996,14 +1001,14 @@ doLedgerEntry(RPC::JsonContext& context) } std::pair -doLedgerEntryGrpc(RPC::GRPCContext& context) +doLedgerEntryGrpc(rpc::GRPCContext& context) { org::xrpl::rpc::v1::GetLedgerEntryRequest const& request = context.params; org::xrpl::rpc::v1::GetLedgerEntryResponse response; grpc::Status const status = grpc::Status::OK; std::shared_ptr ledger; - if (auto status = RPC::ledgerFromRequest(ledger, context)) + if (auto status = rpc::ledgerFromRequest(ledger, context)) { grpc::Status errorStatus; if (status.toErrorCode() == RpcInvalidParams) diff --git a/src/xrpld/rpc/handlers/ledger/LedgerEntryHelpers.h b/src/xrpld/rpc/handlers/ledger/LedgerEntryHelpers.h index 57c4e58242..1b119db04e 100644 --- a/src/xrpld/rpc/handlers/ledger/LedgerEntryHelpers.h +++ b/src/xrpld/rpc/handlers/ledger/LedgerEntryHelpers.h @@ -22,7 +22,7 @@ #include #include -namespace xrpl::LedgerEntryHelpers { +namespace xrpl::ledger_entry_helpers { inline std::unexpected missingFieldError(json::StaticString const field, std::optional err = std::nullopt) @@ -30,7 +30,7 @@ missingFieldError(json::StaticString const field, std::optional err json::Value json = json::ValueType::Object; json[jss::error] = err.value_or("malformedRequest"); json[jss::error_code] = RpcInvalidParams; - json[jss::error_message] = RPC::missingFieldMessage(std::string(field.cStr())); + json[jss::error_message] = rpc::missingFieldMessage(std::string(field.cStr())); return std::unexpected(json); } @@ -40,7 +40,7 @@ invalidFieldError(std::string const& err, json::StaticString const field, std::s json::Value json = json::ValueType::Object; json[jss::error] = err; json[jss::error_code] = RpcInvalidParams; - json[jss::error_message] = RPC::expectedFieldMessage(field, type); + json[jss::error_message] = rpc::expectedFieldMessage(field, type); return std::unexpected(json); } @@ -291,4 +291,4 @@ parseBridgeFields(json::Value const& params) *lockingChainDoor, lockingChainIssue, *issuingChainDoor, issuingChainIssue); } -} // namespace xrpl::LedgerEntryHelpers +} // namespace xrpl::ledger_entry_helpers diff --git a/src/xrpld/rpc/handlers/ledger/LedgerHeader.cpp b/src/xrpld/rpc/handlers/ledger/LedgerHeader.cpp index e2cb80615b..ec3c9fe602 100644 --- a/src/xrpld/rpc/handlers/ledger/LedgerHeader.cpp +++ b/src/xrpld/rpc/handlers/ledger/LedgerHeader.cpp @@ -18,10 +18,10 @@ namespace xrpl { // ledger_index : // } json::Value -doLedgerHeader(RPC::JsonContext& context) +doLedgerHeader(rpc::JsonContext& context) { std::shared_ptr lpLedger; - auto jvResult = RPC::lookupLedger(lpLedger, context); + auto jvResult = rpc::lookupLedger(lpLedger, context); if (!lpLedger) return jvResult; diff --git a/src/xrpld/rpc/handlers/orderbook/AMMInfo.cpp b/src/xrpld/rpc/handlers/orderbook/AMMInfo.cpp index 7f54f81423..e95c51c483 100644 --- a/src/xrpld/rpc/handlers/orderbook/AMMInfo.cpp +++ b/src/xrpld/rpc/handlers/orderbook/AMMInfo.cpp @@ -61,13 +61,13 @@ toIso8601(NetClock::time_point tp) } json::Value -doAMMInfo(RPC::JsonContext& context) +doAMMInfo(rpc::JsonContext& context) { auto const& params(context.params); json::Value result; std::shared_ptr ledger; - result = RPC::lookupLedger(ledger, context); + result = rpc::lookupLedger(ledger, context); if (!ledger) return result; @@ -174,7 +174,7 @@ doAMMInfo(RPC::JsonContext& context) auto const r = getValuesFromContextParams(); if (!r) { - RPC::injectError(r.error(), result); + rpc::injectError(r.error(), result); return result; } diff --git a/src/xrpld/rpc/handlers/orderbook/BookChanges.cpp b/src/xrpld/rpc/handlers/orderbook/BookChanges.cpp index abad196246..0f796df3a4 100644 --- a/src/xrpld/rpc/handlers/orderbook/BookChanges.cpp +++ b/src/xrpld/rpc/handlers/orderbook/BookChanges.cpp @@ -11,15 +11,15 @@ namespace xrpl { json::Value -doBookChanges(RPC::JsonContext& context) +doBookChanges(rpc::JsonContext& context) { std::shared_ptr ledger; - json::Value result = RPC::lookupLedger(ledger, context); + json::Value result = rpc::lookupLedger(ledger, context); if (ledger == nullptr) return result; - return RPC::computeBookChanges(ledger); + return rpc::computeBookChanges(ledger); } } // namespace xrpl diff --git a/src/xrpld/rpc/handlers/orderbook/BookOffers.cpp b/src/xrpld/rpc/handlers/orderbook/BookOffers.cpp index 63dee76f1b..ae539a59f3 100644 --- a/src/xrpld/rpc/handlers/orderbook/BookOffers.cpp +++ b/src/xrpld/rpc/handlers/orderbook/BookOffers.cpp @@ -32,19 +32,19 @@ validateTakerJSON(json::Value const& taker, json::StaticString const& name) { if (!taker.isMember(jss::currency) && !taker.isMember(jss::mpt_issuance_id)) { - return RPC::missingFieldError((boost::format("%s.currency") % name.cStr()).str()); + return rpc::missingFieldError((boost::format("%s.currency") % name.cStr()).str()); } if (taker.isMember(jss::mpt_issuance_id) && (taker.isMember(jss::currency) || taker.isMember(jss::issuer))) { - return RPC::invalidFieldError(name.cStr()); + return rpc::invalidFieldError(name.cStr()); } if ((taker.isMember(jss::currency) && !taker[jss::currency].isString()) || (taker.isMember(jss::mpt_issuance_id) && !taker[jss::mpt_issuance_id].isString())) { - return RPC::expectedFieldError( + return rpc::expectedFieldError( (boost::format("%s.currency") % name.cStr()).str(), "string"); } @@ -71,7 +71,7 @@ parseTakerAssetJSON( if (!toCurrency(issue.currency, taker[jss::currency].asString())) { JLOG(j.info()) << boost::format("Bad %s currency.") % name.cStr(); - return RPC::makeError( + return rpc::makeError( assetError, (boost::format("Invalid field '%s.currency', bad currency.") % name.cStr()).str()); } @@ -82,7 +82,7 @@ parseTakerAssetJSON( MPTID mptid; if (!mptid.parseHex(taker[jss::mpt_issuance_id].asString())) { - return RPC::makeError( + return rpc::makeError( assetError, (boost::format("Invalid field '%s.mpt_issuance_id'") % name.cStr()).str()); } @@ -113,20 +113,20 @@ parseTakerIssuerJSON( { if (!taker[jss::issuer].isString()) { - return RPC::expectedFieldError( + return rpc::expectedFieldError( (boost::format("%s.issuer") % name.cStr()).str(), "string"); } if (!toIssuer(issue.account, taker[jss::issuer].asString())) { - return RPC::makeError( + return rpc::makeError( issuerError, (boost::format("Invalid field '%s.issuer', bad issuer.") % name.cStr()).str()); } if (issue.account == noAccount()) { - return RPC::makeError( + return rpc::makeError( issuerError, (boost::format("Invalid field '%s.issuer', bad issuer account one.") % name.cStr()) @@ -140,7 +140,7 @@ parseTakerIssuerJSON( if (isXRP(issue.currency) && !isXRP(issue.account)) { - return RPC::makeError( + return rpc::makeError( issuerError, (boost::format( "Unneeded field '%s.issuer' for XRP currency " @@ -151,7 +151,7 @@ parseTakerIssuerJSON( if (!isXRP(issue.currency) && isXRP(issue.account)) { - return RPC::makeError( + return rpc::makeError( issuerError, (boost::format("Invalid field '%s.issuer', expected non-XRP issuer.") % name.cStr()) .str()); @@ -162,7 +162,7 @@ parseTakerIssuerJSON( } json::Value -doBookOffers(RPC::JsonContext& context) +doBookOffers(rpc::JsonContext& context) { // VFALCO TODO Here is a terrible place for this kind of business // logic. It needs to be moved elsewhere and documented, @@ -171,25 +171,25 @@ doBookOffers(RPC::JsonContext& context) return rpcError(RpcTooBusy); std::shared_ptr lpLedger; - auto jvResult = RPC::lookupLedger(lpLedger, context); + auto jvResult = rpc::lookupLedger(lpLedger, context); if (!lpLedger) return jvResult; if (!context.params.isMember(jss::taker_pays)) - return RPC::missingFieldError(jss::taker_pays); + return rpc::missingFieldError(jss::taker_pays); if (!context.params.isMember(jss::taker_gets)) - return RPC::missingFieldError(jss::taker_gets); + return rpc::missingFieldError(jss::taker_gets); json::Value const& takerPays = context.params[jss::taker_pays]; json::Value const& takerGets = context.params[jss::taker_gets]; if (!takerPays.isObjectOrNull()) - return RPC::objectFieldError(jss::taker_pays); + return rpc::objectFieldError(jss::taker_pays); if (!takerGets.isObjectOrNull()) - return RPC::objectFieldError(jss::taker_gets); + return rpc::objectFieldError(jss::taker_gets); if (auto const err = validateTakerJSON(takerPays, jss::taker_pays)) return *err; @@ -215,11 +215,11 @@ doBookOffers(RPC::JsonContext& context) if (context.params.isMember(jss::taker)) { if (!context.params[jss::taker].isString()) - return RPC::expectedFieldError(jss::taker, "string"); + return rpc::expectedFieldError(jss::taker, "string"); takerID = parseBase58(context.params[jss::taker].asString()); if (!takerID) - return RPC::invalidFieldError(jss::taker); + return rpc::invalidFieldError(jss::taker); } std::optional domain; @@ -229,7 +229,7 @@ doBookOffers(RPC::JsonContext& context) if (!context.params[jss::domain].isString() || !num.parseHex(context.params[jss::domain].asString())) { - return RPC::makeError(RpcDomainMalformed, "Unable to parse domain."); + return rpc::makeError(RpcDomainMalformed, "Unable to parse domain."); } domain = num; @@ -238,11 +238,11 @@ doBookOffers(RPC::JsonContext& context) if (book.in == book.out) { JLOG(context.j.info()) << "taker_gets same as taker_pays."; - return RPC::makeError(RpcBadMarket); + return rpc::makeError(RpcBadMarket); } unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kBookOffers, context)) + if (auto err = readLimitField(limit, rpc::tuning::kBookOffers, context)) return *err; bool const bProof(context.params.isMember(jss::proof)); @@ -260,7 +260,7 @@ doBookOffers(RPC::JsonContext& context) jvMarker, jvResult); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; return jvResult; } diff --git a/src/xrpld/rpc/handlers/orderbook/DepositAuthorized.cpp b/src/xrpld/rpc/handlers/orderbook/DepositAuthorized.cpp index 343d539277..9d109fe16f 100644 --- a/src/xrpld/rpc/handlers/orderbook/DepositAuthorized.cpp +++ b/src/xrpld/rpc/handlers/orderbook/DepositAuthorized.cpp @@ -32,17 +32,17 @@ namespace xrpl { // } json::Value -doDepositAuthorized(RPC::JsonContext& context) +doDepositAuthorized(rpc::JsonContext& context) { json::Value const& params = context.params; // Validate source_account. if (!params.isMember(jss::source_account)) - return RPC::missingFieldError(jss::source_account); + return rpc::missingFieldError(jss::source_account); if (!params[jss::source_account].isString()) { - return RPC::makeError( - RpcInvalidParams, RPC::expectedFieldMessage(jss::source_account, "a string")); + return rpc::makeError( + RpcInvalidParams, rpc::expectedFieldMessage(jss::source_account, "a string")); } auto srcID = parseBase58(params[jss::source_account].asString()); @@ -52,11 +52,11 @@ doDepositAuthorized(RPC::JsonContext& context) // Validate destination_account. if (!params.isMember(jss::destination_account)) - return RPC::missingFieldError(jss::destination_account); + return rpc::missingFieldError(jss::destination_account); if (!params[jss::destination_account].isString()) { - return RPC::makeError( - RpcInvalidParams, RPC::expectedFieldMessage(jss::destination_account, "a string")); + return rpc::makeError( + RpcInvalidParams, rpc::expectedFieldMessage(jss::destination_account, "a string")); } auto dstID = parseBase58(params[jss::destination_account].asString()); @@ -66,7 +66,7 @@ doDepositAuthorized(RPC::JsonContext& context) // Validate ledger. std::shared_ptr ledger; - json::Value result = RPC::lookupLedger(ledger, context); + json::Value result = rpc::lookupLedger(ledger, context); if (!ledger) return result; @@ -74,7 +74,7 @@ doDepositAuthorized(RPC::JsonContext& context) // If source account is not in the ledger it can't be authorized. if (!ledger->exists(keylet::account(srcAcct))) { - RPC::injectError(RpcSrcActNotFound, result); + rpc::injectError(RpcSrcActNotFound, result); return result; } @@ -82,7 +82,7 @@ doDepositAuthorized(RPC::JsonContext& context) auto const sleDest = ledger->read(keylet::account(dstAcct)); if (!sleDest) { - RPC::injectError(RpcDstActNotFound, result); + rpc::injectError(RpcDstActNotFound, result); return result; } @@ -96,15 +96,15 @@ doDepositAuthorized(RPC::JsonContext& context) auto const& creds(params[jss::credentials]); if (!creds.isArray() || !creds) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, - RPC::expectedFieldMessage( + rpc::expectedFieldMessage( jss::credentials, "is non-empty array of CredentialID(hash256)")); } if (creds.size() > kMaxCredentialsArraySize) { - return RPC::makeError( - RpcInvalidParams, RPC::expectedFieldMessage(jss::credentials, "array too long")); + return rpc::makeError( + RpcInvalidParams, rpc::expectedFieldMessage(jss::credentials, "array too long")); } lifeExtender.reserve(creds.size()); @@ -112,9 +112,9 @@ doDepositAuthorized(RPC::JsonContext& context) { if (!jo.isString()) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, - RPC::expectedFieldMessage( + rpc::expectedFieldMessage( jss::credentials, "an array of CredentialID(hash256)")); } @@ -122,34 +122,34 @@ doDepositAuthorized(RPC::JsonContext& context) auto const credS = jo.asString(); if (!credH.parseHex(credS)) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, - RPC::expectedFieldMessage( + rpc::expectedFieldMessage( jss::credentials, "an array of CredentialID(hash256)")); } SLE::const_pointer sleCred = ledger->read(keylet::credential(credH)); if (!sleCred) { - RPC::injectError(RpcBadCredentials, "credentials don't exist", result); + rpc::injectError(RpcBadCredentials, "credentials don't exist", result); return result; } if (!sleCred->isFlag(lsfAccepted)) { - RPC::injectError(RpcBadCredentials, "credentials aren't accepted", result); + rpc::injectError(RpcBadCredentials, "credentials aren't accepted", result); return result; } if (credentials::checkExpired(*sleCred, ledger->header().parentCloseTime)) { - RPC::injectError(RpcBadCredentials, "credentials are expired", result); + rpc::injectError(RpcBadCredentials, "credentials are expired", result); return result; } if ((*sleCred)[sfSubject] != srcAcct) { - RPC::injectError( + rpc::injectError( RpcBadCredentials, "credentials doesn't belong to the root account", result); return result; } @@ -157,7 +157,7 @@ doDepositAuthorized(RPC::JsonContext& context) auto [it, ins] = sorted.emplace((*sleCred)[sfIssuer], (*sleCred)[sfCredentialType]); if (!ins) { - RPC::injectError(RpcBadCredentials, "duplicates in credentials", result); + rpc::injectError(RpcBadCredentials, "duplicates in credentials", result); return result; } lifeExtender.push_back(std::move(sleCred)); diff --git a/src/xrpld/rpc/handlers/orderbook/GetAggregatePrice.cpp b/src/xrpld/rpc/handlers/orderbook/GetAggregatePrice.cpp index 632456a3fa..f493000d0b 100644 --- a/src/xrpld/rpc/handlers/orderbook/GetAggregatePrice.cpp +++ b/src/xrpld/rpc/handlers/orderbook/GetAggregatePrice.cpp @@ -48,7 +48,7 @@ using Prices = bimap>, multiset_of const& f) { @@ -149,26 +149,26 @@ getStats(Prices::right_const_iterator const& begin, Prices::right_const_iterator * range - {most recent, most recent - time_threshold} [optional] */ json::Value -doGetAggregatePrice(RPC::JsonContext& context) +doGetAggregatePrice(rpc::JsonContext& context) { json::Value result; auto const& params(context.params); static constexpr std::uint16_t kMaxOracles = 200; if (!params.isMember(jss::oracles)) - return RPC::missingFieldError(jss::oracles); + return rpc::missingFieldError(jss::oracles); if (!params[jss::oracles].isArray() || params[jss::oracles].size() == 0 || params[jss::oracles].size() > kMaxOracles) { - RPC::injectError(RpcOracleMalformed, result); + rpc::injectError(RpcOracleMalformed, result); return result; } if (!params.isMember(jss::base_asset)) - return RPC::missingFieldError(jss::base_asset); + return rpc::missingFieldError(jss::base_asset); if (!params.isMember(jss::quote_asset)) - return RPC::missingFieldError(jss::quote_asset); + return rpc::missingFieldError(jss::quote_asset); // Lambda to validate uint type // support positive int, uint, and a number represented as a string @@ -213,38 +213,38 @@ doGetAggregatePrice(RPC::JsonContext& context) auto const trim = getField(jss::trim); if (std::holds_alternative(trim)) { - RPC::injectError(std::get(trim), result); + rpc::injectError(std::get(trim), result); return result; } if (params.isMember(jss::trim) && (std::get(trim) == 0 || std::get(trim) > kMaxTrim)) { - RPC::injectError(RpcInvalidParams, result); + rpc::injectError(RpcInvalidParams, result); return result; } auto const timeThreshold = getField(jss::time_threshold, 0); if (std::holds_alternative(timeThreshold)) { - RPC::injectError(std::get(timeThreshold), result); + rpc::injectError(std::get(timeThreshold), result); return result; } auto const baseAsset = getCurrency(sfBaseAsset, jss::base_asset); if (std::holds_alternative(baseAsset)) { - RPC::injectError(std::get(baseAsset), result); + rpc::injectError(std::get(baseAsset), result); return result; } auto const quoteAsset = getCurrency(sfQuoteAsset, jss::quote_asset); if (std::holds_alternative(quoteAsset)) { - RPC::injectError(std::get(quoteAsset), result); + rpc::injectError(std::get(quoteAsset), result); return result; } std::shared_ptr ledger; - result = RPC::lookupLedger(ledger, context); + result = rpc::lookupLedger(ledger, context); if (!ledger) return result; // LCOV_EXCL_LINE @@ -255,7 +255,7 @@ doGetAggregatePrice(RPC::JsonContext& context) { if (!oracle.isMember(jss::oracle_document_id) || !oracle.isMember(jss::account)) { - RPC::injectError(RpcOracleMalformed, result); + rpc::injectError(RpcOracleMalformed, result); return result; } auto const documentID = validUInt(oracle, jss::oracle_document_id) @@ -264,7 +264,7 @@ doGetAggregatePrice(RPC::JsonContext& context) auto const account = parseBase58(oracle[jss::account].asString()); if (!account || account->isZero() || !documentID) { - RPC::injectError(RpcInvalidParams, result); + rpc::injectError(RpcInvalidParams, result); return result; } @@ -298,7 +298,7 @@ doGetAggregatePrice(RPC::JsonContext& context) if (prices.empty()) { - RPC::injectError(RpcObjectNotFound, result); + rpc::injectError(RpcObjectNotFound, result); return result; } @@ -321,7 +321,7 @@ doGetAggregatePrice(RPC::JsonContext& context) if (prices.empty()) { // LCOV_EXCL_START - RPC::injectError(RpcInternal, result); + rpc::injectError(RpcInternal, result); return result; // LCOV_EXCL_STOP } diff --git a/src/xrpld/rpc/handlers/orderbook/NFTBuyOffers.cpp b/src/xrpld/rpc/handlers/orderbook/NFTBuyOffers.cpp index 88e4392dad..60b48ac5a6 100644 --- a/src/xrpld/rpc/handlers/orderbook/NFTBuyOffers.cpp +++ b/src/xrpld/rpc/handlers/orderbook/NFTBuyOffers.cpp @@ -10,15 +10,15 @@ namespace xrpl { json::Value -doNFTBuyOffers(RPC::JsonContext& context) +doNFTBuyOffers(rpc::JsonContext& context) { if (!context.params.isMember(jss::nft_id)) - return RPC::missingFieldError(jss::nft_id); + return rpc::missingFieldError(jss::nft_id); uint256 nftId; if (!nftId.parseHex(context.params[jss::nft_id].asString())) - return RPC::invalidFieldError(jss::nft_id); + return rpc::invalidFieldError(jss::nft_id); return enumerateNFTOffers(context, nftId, keylet::nftBuys(nftId)); } diff --git a/src/xrpld/rpc/handlers/orderbook/NFTOffersHelpers.h b/src/xrpld/rpc/handlers/orderbook/NFTOffersHelpers.h index 70bc258d77..e03830ae0d 100644 --- a/src/xrpld/rpc/handlers/orderbook/NFTOffersHelpers.h +++ b/src/xrpld/rpc/handlers/orderbook/NFTOffersHelpers.h @@ -52,15 +52,15 @@ appendNftOfferJson(Application const& app, SLE::const_ref offer, json::Value& of // marker: opaque // optional, resume previous query // } inline json::Value -enumerateNFTOffers(RPC::JsonContext& context, uint256 const& nftId, Keylet const& directory) +enumerateNFTOffers(rpc::JsonContext& context, uint256 const& nftId, Keylet const& directory) { unsigned int limit = 0; - if (auto err = readLimitField(limit, RPC::Tuning::kNftOffers, context)) + if (auto err = readLimitField(limit, rpc::tuning::kNftOffers, context)) return *err; std::shared_ptr ledger; - if (auto result = RPC::lookupLedger(ledger, context); !ledger) + if (auto result = rpc::lookupLedger(ledger, context); !ledger) return result; if (!ledger->exists(directory)) @@ -83,7 +83,7 @@ enumerateNFTOffers(RPC::JsonContext& context, uint256 const& nftId, Keylet const json::Value const& marker(context.params[jss::marker]); if (!marker.isString()) - return RPC::expectedFieldError(jss::marker, "string"); + return rpc::expectedFieldError(jss::marker, "string"); if (!startAfter.parseHex(marker.asString())) return rpcError(RpcInvalidParams); @@ -127,7 +127,7 @@ enumerateNFTOffers(RPC::JsonContext& context, uint256 const& nftId, Keylet const for (auto const& offer : offers) appendNftOfferJson(context.app, offer, jsonOffers); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; return result; } diff --git a/src/xrpld/rpc/handlers/orderbook/NFTSellOffers.cpp b/src/xrpld/rpc/handlers/orderbook/NFTSellOffers.cpp index 309df93605..8b09b42a34 100644 --- a/src/xrpld/rpc/handlers/orderbook/NFTSellOffers.cpp +++ b/src/xrpld/rpc/handlers/orderbook/NFTSellOffers.cpp @@ -10,15 +10,15 @@ namespace xrpl { json::Value -doNFTSellOffers(RPC::JsonContext& context) +doNFTSellOffers(rpc::JsonContext& context) { if (!context.params.isMember(jss::nft_id)) - return RPC::missingFieldError(jss::nft_id); + return rpc::missingFieldError(jss::nft_id); uint256 nftId; if (!nftId.parseHex(context.params[jss::nft_id].asString())) - return RPC::invalidFieldError(jss::nft_id); + return rpc::invalidFieldError(jss::nft_id); return enumerateNFTOffers(context, nftId, keylet::nftSells(nftId)); } diff --git a/src/xrpld/rpc/handlers/orderbook/PathFind.cpp b/src/xrpld/rpc/handlers/orderbook/PathFind.cpp index 3a6b52ee98..c46238a5a5 100644 --- a/src/xrpld/rpc/handlers/orderbook/PathFind.cpp +++ b/src/xrpld/rpc/handlers/orderbook/PathFind.cpp @@ -13,7 +13,7 @@ namespace xrpl { json::Value -doPathFind(RPC::JsonContext& context) +doPathFind(rpc::JsonContext& context) { if (context.app.config().pathSearchMax == 0) return rpcError(RpcNotSupported); @@ -34,7 +34,7 @@ doPathFind(RPC::JsonContext& context) if (sSubCommand == "create") { - context.loadType = Resource::kFeeHeavyBurdenRpc; + context.loadType = resource::kFeeHeavyBurdenRpc; context.infoSub->clearRequest(); return context.app.getPathRequestManager().makePathRequest( context.infoSub, lpLedger, context.params); diff --git a/src/xrpld/rpc/handlers/orderbook/RipplePathFind.cpp b/src/xrpld/rpc/handlers/orderbook/RipplePathFind.cpp index b7edfb6dbe..923cb0f7f5 100644 --- a/src/xrpld/rpc/handlers/orderbook/RipplePathFind.cpp +++ b/src/xrpld/rpc/handlers/orderbook/RipplePathFind.cpp @@ -21,12 +21,12 @@ namespace xrpl { // This interface is deprecated. json::Value -doRipplePathFind(RPC::JsonContext& context) +doRipplePathFind(rpc::JsonContext& context) { if (context.app.config().pathSearchMax == 0) return rpcError(RpcNotSupported); - context.loadType = Resource::kFeeHeavyBurdenRpc; + context.loadType = resource::kFeeHeavyBurdenRpc; std::shared_ptr lpLedger; json::Value jvResult; @@ -37,7 +37,7 @@ doRipplePathFind(RPC::JsonContext& context) // No ledger specified, use pathfinding defaults // and dispatch to pathfinding engine if (context.app.getLedgerMaster().getValidatedLedgerAge() > - RPC::Tuning::kMaxValidatedLedgerAge) + rpc::tuning::kMaxValidatedLedgerAge) { if (context.apiVersion == 1) return rpcError(RpcNoNetwork); @@ -146,11 +146,11 @@ doRipplePathFind(RPC::JsonContext& context) } // The caller specified a ledger - jvResult = RPC::lookupLedger(lpLedger, context); + jvResult = rpc::lookupLedger(lpLedger, context); if (!lpLedger) return jvResult; - RPC::LegacyPathFind const lpf(isUnlimited(context.role), context.app); + rpc::LegacyPathFind const lpf(isUnlimited(context.role), context.app); if (!lpf.isOk()) return rpcError(RpcTooBusy); diff --git a/src/xrpld/rpc/handlers/server_info/Feature.cpp b/src/xrpld/rpc/handlers/server_info/Feature.cpp index bd7198b61c..1906658106 100644 --- a/src/xrpld/rpc/handlers/server_info/Feature.cpp +++ b/src/xrpld/rpc/handlers/server_info/Feature.cpp @@ -18,7 +18,7 @@ namespace xrpl { // vetoed : true/false // } json::Value -doFeature(RPC::JsonContext& context) +doFeature(rpc::JsonContext& context) { if (context.params.isMember(jss::feature)) { diff --git a/src/xrpld/rpc/handlers/server_info/Fee.cpp b/src/xrpld/rpc/handlers/server_info/Fee.cpp index 1fe5476d50..2e4147fc1e 100644 --- a/src/xrpld/rpc/handlers/server_info/Fee.cpp +++ b/src/xrpld/rpc/handlers/server_info/Fee.cpp @@ -8,7 +8,7 @@ namespace xrpl { json::Value -doFee(RPC::JsonContext& context) +doFee(rpc::JsonContext& context) { auto result = context.app.getTxQ().doRPC(context.app); if (result.type() == json::ValueType::Object) @@ -16,7 +16,7 @@ doFee(RPC::JsonContext& context) // LCOV_EXCL_START UNREACHABLE("xrpl::doFee : invalid result type"); - RPC::injectError(RpcInternal, context.params); + rpc::injectError(RpcInternal, context.params); return context.params; // LCOV_EXCL_STOP } diff --git a/src/xrpld/rpc/handlers/server_info/Manifest.cpp b/src/xrpld/rpc/handlers/server_info/Manifest.cpp index cb1771750b..c0b29d8275 100644 --- a/src/xrpld/rpc/handlers/server_info/Manifest.cpp +++ b/src/xrpld/rpc/handlers/server_info/Manifest.cpp @@ -12,12 +12,12 @@ namespace xrpl { json::Value -doManifest(RPC::JsonContext& context) +doManifest(rpc::JsonContext& context) { auto& params = context.params; if (!params.isMember(jss::public_key)) - return RPC::missingFieldError(jss::public_key); + return rpc::missingFieldError(jss::public_key); auto const requested = params[jss::public_key].asString(); @@ -27,7 +27,7 @@ doManifest(RPC::JsonContext& context) auto const pk = parseBase58(TokenType::NodePublic, requested); if (!pk) { - RPC::injectError(RpcInvalidParams, ret); + rpc::injectError(RpcInvalidParams, ret); return ret; } diff --git a/src/xrpld/rpc/handlers/server_info/ServerDefinitions.cpp b/src/xrpld/rpc/handlers/server_info/ServerDefinitions.cpp index cce1b3e07f..b561ce6d38 100644 --- a/src/xrpld/rpc/handlers/server_info/ServerDefinitions.cpp +++ b/src/xrpld/rpc/handlers/server_info/ServerDefinitions.cpp @@ -382,7 +382,7 @@ getServerDefinitionsJson() } json::Value -doServerDefinitions(RPC::JsonContext& context) +doServerDefinitions(rpc::JsonContext& context) { auto& params = context.params; @@ -390,7 +390,7 @@ doServerDefinitions(RPC::JsonContext& context) if (params.isMember(jss::hash)) { if (!params[jss::hash].isString() || !hash.parseHex(params[jss::hash].asString())) - return RPC::invalidFieldError(jss::hash); + return rpc::invalidFieldError(jss::hash); } auto const& defs = detail::getDefinitions(); diff --git a/src/xrpld/rpc/handlers/server_info/ServerInfo.cpp b/src/xrpld/rpc/handlers/server_info/ServerInfo.cpp index aaad9d2b02..fd6e2f717f 100644 --- a/src/xrpld/rpc/handlers/server_info/ServerInfo.cpp +++ b/src/xrpld/rpc/handlers/server_info/ServerInfo.cpp @@ -9,7 +9,7 @@ namespace xrpl { json::Value -doServerInfo(RPC::JsonContext& context) +doServerInfo(rpc::JsonContext& context) { json::Value ret(json::ValueType::Object); diff --git a/src/xrpld/rpc/handlers/server_info/ServerState.cpp b/src/xrpld/rpc/handlers/server_info/ServerState.cpp index acf4e9eb43..e0d43d4053 100644 --- a/src/xrpld/rpc/handlers/server_info/ServerState.cpp +++ b/src/xrpld/rpc/handlers/server_info/ServerState.cpp @@ -8,7 +8,7 @@ namespace xrpl { json::Value -doServerState(RPC::JsonContext& context) +doServerState(rpc::JsonContext& context) { json::Value ret(json::ValueType::Object); diff --git a/src/xrpld/rpc/handlers/server_info/Version.h b/src/xrpld/rpc/handlers/server_info/Version.h index f25d8679ba..40ad4e5e71 100644 --- a/src/xrpld/rpc/handlers/server_info/Version.h +++ b/src/xrpld/rpc/handlers/server_info/Version.h @@ -9,7 +9,7 @@ #include #include -namespace xrpl::RPC { +namespace xrpl::rpc { class VersionHandler { @@ -34,9 +34,9 @@ public: // NOLINTBEGIN(readability-identifier-naming) static constexpr char const* name = "version"; - static constexpr unsigned minApiVer = RPC::kApiMinimumSupportedVersion; + static constexpr unsigned minApiVer = rpc::kApiMinimumSupportedVersion; - static constexpr unsigned maxApiVer = RPC::kApiMaximumValidVersion; + static constexpr unsigned maxApiVer = rpc::kApiMaximumValidVersion; static constexpr Role role = Role::USER; @@ -48,4 +48,4 @@ private: bool betaEnabled_; }; -} // namespace xrpl::RPC +} // namespace xrpl::rpc diff --git a/src/xrpld/rpc/handlers/subscribe/Subscribe.cpp b/src/xrpld/rpc/handlers/subscribe/Subscribe.cpp index 93840bb6d6..7ce432c49e 100644 --- a/src/xrpld/rpc/handlers/subscribe/Subscribe.cpp +++ b/src/xrpld/rpc/handlers/subscribe/Subscribe.cpp @@ -27,7 +27,7 @@ namespace xrpl { json::Value -doSubscribe(RPC::JsonContext& context) +doSubscribe(rpc::JsonContext& context) { InfoSub::pointer ispSub; json::Value jvResult(json::ValueType::Object); @@ -79,7 +79,7 @@ doSubscribe(RPC::JsonContext& context) } catch (std::runtime_error const& ex) { - return RPC::makeParamError(ex.what()); + return rpc::makeParamError(ex.what()); } } else @@ -174,7 +174,7 @@ doSubscribe(RPC::JsonContext& context) if (!context.params[accountsProposed].isArray()) return rpcError(RpcInvalidParams); - auto ids = RPC::parseAccountIds(context.params[accountsProposed]); + auto ids = rpc::parseAccountIds(context.params[accountsProposed]); if (ids.empty()) return rpcError(RpcActMalformed); context.netOps.subAccount(ispSub, ids, true); @@ -185,7 +185,7 @@ doSubscribe(RPC::JsonContext& context) if (!context.params[jss::accounts].isArray()) return rpcError(RpcInvalidParams); - auto ids = RPC::parseAccountIds(context.params[jss::accounts]); + auto ids = rpc::parseAccountIds(context.params[jss::accounts]); if (ids.empty()) return rpcError(RpcActMalformed); context.netOps.subAccount(ispSub, ids, false); @@ -197,7 +197,7 @@ doSubscribe(RPC::JsonContext& context) if (!context.app.config().useTxTables()) return rpcError(RpcNotEnabled); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; auto const& req = context.params[jss::account_history_tx_stream]; if (!req.isMember(jss::account) || !req[jss::account].isString()) return rpcError(RpcInvalidParams); @@ -230,11 +230,11 @@ doSubscribe(RPC::JsonContext& context) Book book; - if (auto const err = RPC::parseSubUnsubJson(book.in, j, jss::taker_pays, context.j); + if (auto const err = rpc::parseSubUnsubJson(book.in, j, jss::taker_pays, context.j); err != RpcSuccess) return rpcError(err); - if (auto const err = RPC::parseSubUnsubJson(book.out, j, jss::taker_gets, context.j); + if (auto const err = rpc::parseSubUnsubJson(book.out, j, jss::taker_gets, context.j); err != RpcSuccess) return rpcError(err); @@ -285,7 +285,7 @@ doSubscribe(RPC::JsonContext& context) if ((j.isMember(jss::snapshot) && j[jss::snapshot].asBool()) || (j.isMember(jss::state_now) && j[jss::state_now].asBool())) { - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; std::shared_ptr lpLedger = context.app.getLedgerMaster().getPublishedLedger(); if (lpLedger) @@ -299,7 +299,7 @@ doSubscribe(RPC::JsonContext& context) field == jss::asks ? reversed(book) : book, takerID ? *takerID : noAccount(), false, - RPC::Tuning::kBookOffers.rDefault, + rpc::tuning::kBookOffers.rDefault, jvMarker, jvOffers); diff --git a/src/xrpld/rpc/handlers/subscribe/Unsubscribe.cpp b/src/xrpld/rpc/handlers/subscribe/Unsubscribe.cpp index af42af2a55..33c785a9bb 100644 --- a/src/xrpld/rpc/handlers/subscribe/Unsubscribe.cpp +++ b/src/xrpld/rpc/handlers/subscribe/Unsubscribe.cpp @@ -18,7 +18,7 @@ namespace xrpl { json::Value -doUnsubscribe(RPC::JsonContext& context) +doUnsubscribe(rpc::JsonContext& context) { InfoSub::pointer ispSub; json::Value jvResult(json::ValueType::Object); @@ -106,7 +106,7 @@ doUnsubscribe(RPC::JsonContext& context) if (!context.params[accountsProposed].isArray()) return rpcError(RpcInvalidParams); - auto ids = RPC::parseAccountIds(context.params[accountsProposed]); + auto ids = rpc::parseAccountIds(context.params[accountsProposed]); if (ids.empty()) return rpcError(RpcActMalformed); context.netOps.unsubAccount(ispSub, ids, true); @@ -117,7 +117,7 @@ doUnsubscribe(RPC::JsonContext& context) if (!context.params[jss::accounts].isArray()) return rpcError(RpcInvalidParams); - auto ids = RPC::parseAccountIds(context.params[jss::accounts]); + auto ids = rpc::parseAccountIds(context.params[jss::accounts]); if (ids.empty()) return rpcError(RpcActMalformed); context.netOps.unsubAccount(ispSub, ids, false); @@ -161,11 +161,11 @@ doUnsubscribe(RPC::JsonContext& context) Book book; - if (auto const err = RPC::parseSubUnsubJson(book.in, jv, jss::taker_pays, context.j); + if (auto const err = rpc::parseSubUnsubJson(book.in, jv, jss::taker_pays, context.j); err != RpcSuccess) return rpcError(err); - if (auto const err = RPC::parseSubUnsubJson(book.out, jv, jss::taker_gets, context.j); + if (auto const err = rpc::parseSubUnsubJson(book.out, jv, jss::taker_gets, context.j); err != RpcSuccess) return rpcError(err); diff --git a/src/xrpld/rpc/handlers/transaction/Simulate.cpp b/src/xrpld/rpc/handlers/transaction/Simulate.cpp index 0f163c7356..8441add08b 100644 --- a/src/xrpld/rpc/handlers/transaction/Simulate.cpp +++ b/src/xrpld/rpc/handlers/transaction/Simulate.cpp @@ -44,7 +44,7 @@ namespace xrpl { static std::expected -getAutofillSequence(json::Value const& txJson, RPC::JsonContext& context) +getAutofillSequence(json::Value const& txJson, rpc::JsonContext& context) { // autofill Sequence bool const hasTicketSeq = txJson.isMember(sfTicketSequence.jsonName); @@ -53,14 +53,14 @@ getAutofillSequence(json::Value const& txJson, RPC::JsonContext& context) { // sanity check, should fail earlier // LCOV_EXCL_START - return std::unexpected(RPC::invalidFieldError("tx.Account")); + return std::unexpected(rpc::invalidFieldError("tx.Account")); // LCOV_EXCL_STOP } auto const srcAddressID = parseBase58(accountStr.asString()); if (!srcAddressID.has_value()) { return std::unexpected( - RPC::makeError(RpcSrcActMalformed, RPC::invalidFieldMessage("tx.Account"))); + rpc::makeError(RpcSrcActMalformed, rpc::invalidFieldMessage("tx.Account"))); } SLE::const_pointer const sle = context.app.getOpenLedger().current()->read(keylet::account(*srcAddressID)); @@ -88,7 +88,7 @@ autofillSignature(json::Value& sigObject, std::string const& fieldPrefix = "tx") if (sigObject.isMember(jss::Signers)) { if (!sigObject[jss::Signers].isArray()) - return RPC::invalidFieldError(fieldPrefix + ".Signers"); + return rpc::invalidFieldError(fieldPrefix + ".Signers"); // check multisigned signers for (unsigned index = 0; index < sigObject[jss::Signers].size(); index++) { @@ -96,7 +96,7 @@ autofillSignature(json::Value& sigObject, std::string const& fieldPrefix = "tx") if (!signer.isObject() || !signer.isMember(jss::Signer) || !signer[jss::Signer].isObject()) { - return RPC::invalidFieldError( + return rpc::invalidFieldError( fieldPrefix + ".Signers[" + std::to_string(index) + "]"); } @@ -133,7 +133,7 @@ autofillSignature(json::Value& sigObject, std::string const& fieldPrefix = "tx") } static std::optional -autofillTx(json::Value& txJson, RPC::JsonContext& context) +autofillTx(json::Value& txJson, rpc::JsonContext& context) { if (auto error = autofillSignature(txJson)) return error; @@ -142,7 +142,7 @@ autofillTx(json::Value& txJson, RPC::JsonContext& context) { auto& sponsorSignature = txJson[sfSponsorSignature.jsonName]; if (!sponsorSignature.isObject()) - return RPC::objectFieldError(sfSponsorSignature.jsonName); + return rpc::objectFieldError(sfSponsorSignature.jsonName); if (auto const error = autofillSignature(sponsorSignature, "tx.SponsorSignature")) return error; @@ -167,7 +167,7 @@ autofillTx(json::Value& txJson, RPC::JsonContext& context) { // Autofill Fee after normalizing nested signer fields so the fee // estimator sees the full transaction shape. - auto feeOrError = RPC::getCurrentNetworkFee( + auto feeOrError = rpc::getCurrentNetworkFee( context.role, context.app.config(), context.app.getFeeTrack(), @@ -191,18 +191,18 @@ getTxJsonFromParams(json::Value const& params) { if (params.isMember(jss::tx_json)) { - return RPC::makeParamError("Can only include one of `tx_blob` and `tx_json`."); + return rpc::makeParamError("Can only include one of `tx_blob` and `tx_json`."); } auto const txBlob = params[jss::tx_blob]; if (!txBlob.isString()) { - return RPC::invalidFieldError(jss::tx_blob); + return rpc::invalidFieldError(jss::tx_blob); } auto unHexed = strUnHex(txBlob.asString()); if (!unHexed || unHexed->empty()) - return RPC::invalidFieldError(jss::tx_blob); + return rpc::invalidFieldError(jss::tx_blob); try { @@ -211,7 +211,7 @@ getTxJsonFromParams(json::Value const& params) } catch (std::runtime_error const&) { - return RPC::invalidFieldError(jss::tx_blob); + return rpc::invalidFieldError(jss::tx_blob); } } else if (params.isMember(jss::tx_json)) @@ -219,30 +219,30 @@ getTxJsonFromParams(json::Value const& params) txJson = params[jss::tx_json]; if (!txJson.isObject()) { - return RPC::objectFieldError(jss::tx_json); + return rpc::objectFieldError(jss::tx_json); } } else { - return RPC::makeParamError("Neither `tx_blob` nor `tx_json` included."); + return rpc::makeParamError("Neither `tx_blob` nor `tx_json` included."); } // basic sanity checks for transaction shape if (!txJson.isMember(jss::TransactionType)) { - return RPC::missingFieldError("tx.TransactionType"); + return rpc::missingFieldError("tx.TransactionType"); } if (!txJson.isMember(jss::Account)) { - return RPC::missingFieldError("tx.Account"); + return rpc::missingFieldError("tx.Account"); } return txJson; } static json::Value -simulateTxn(RPC::JsonContext& context, std::shared_ptr transaction) +simulateTxn(rpc::JsonContext& context, std::shared_ptr transaction) { json::Value jvResult; // Process the transaction @@ -290,11 +290,11 @@ simulateTxn(RPC::JsonContext& context, std::shared_ptr transaction) else { jvResult[jss::meta] = result.metadata->getJson(JsonOptions::Values::None); - RPC::insertDeliveredAmount( + rpc::insertDeliveredAmount( jvResult[jss::meta], view, transaction->getSTransaction(), *result.metadata); - RPC::insertNFTSyntheticInJson( + rpc::insertNFTSyntheticInJson( jvResult, transaction->getSTransaction(), *result.metadata); - RPC::insertMPTokenIssuanceID( + rpc::insertMPTokenIssuanceID( jvResult[jss::meta], transaction->getSTransaction(), *result.metadata); } } @@ -317,23 +317,23 @@ simulateTxn(RPC::JsonContext& context, std::shared_ptr transaction) // binary: // } json::Value -doSimulate(RPC::JsonContext& context) +doSimulate(rpc::JsonContext& context) { - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; json::Value txJson; // the tx as a JSON // check validity of `binary` param if (context.params.isMember(jss::binary) && !context.params[jss::binary].isBool()) { - return RPC::invalidFieldError(jss::binary); + return rpc::invalidFieldError(jss::binary); } for (auto const field : {jss::secret, jss::seed, jss::seed_hex, jss::passphrase}) { if (context.params.isMember(field)) { - return RPC::invalidFieldError(field); + return rpc::invalidFieldError(field); } } @@ -365,13 +365,13 @@ doSimulate(RPC::JsonContext& context) if (stTx->getTxnType() == ttBATCH) { - return RPC::makeError(RpcNotImpl); + return rpc::makeError(RpcNotImpl); } // Reject transactions with the tfInnerBatchTxn flag. if (stTx->isFlag(tfInnerBatchTxn)) { - return RPC::makeError( + return rpc::makeError( RpcInvalidParams, "tfInnerBatchTxn flag is not allowed on top-level transactions."); } diff --git a/src/xrpld/rpc/handlers/transaction/Submit.cpp b/src/xrpld/rpc/handlers/transaction/Submit.cpp index 79f3680684..05a1552221 100644 --- a/src/xrpld/rpc/handlers/transaction/Submit.cpp +++ b/src/xrpld/rpc/handlers/transaction/Submit.cpp @@ -27,11 +27,11 @@ namespace xrpl { static std::expected -getFailHard(RPC::JsonContext const& context) +getFailHard(rpc::JsonContext const& context) { if (context.params.isMember(jss::fail_hard) && !context.params[jss::fail_hard].isBool()) { - return std::unexpected(RPC::expectedFieldError(jss::fail_hard, "boolean")); + return std::unexpected(rpc::expectedFieldError(jss::fail_hard, "boolean")); } return NetworkOPs::doFailHard( context.params.isMember(jss::fail_hard) && context.params[jss::fail_hard].asBool()); @@ -42,9 +42,9 @@ getFailHard(RPC::JsonContext const& context) // secret: // } json::Value -doSubmit(RPC::JsonContext& context) +doSubmit(rpc::JsonContext& context) { - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; if (!context.params.isMember(jss::tx_blob)) { @@ -53,16 +53,16 @@ doSubmit(RPC::JsonContext& context) return failType.error(); if (context.role != Role::ADMIN && !context.app.config().canSign()) - return RPC::makeError(RpcNotSupported, "Signing is not supported by this server."); + return rpc::makeError(RpcNotSupported, "Signing is not supported by this server."); - auto ret = RPC::transactionSubmit( + auto ret = rpc::transactionSubmit( context.params, context.apiVersion, *failType, context.role, context.ledgerMaster.getValidatedLedgerAge(), context.app, - RPC::getProcessTxnFn(context.netOps)); + rpc::getProcessTxnFn(context.netOps)); ret[jss::deprecated] = "Signing support in the 'submit' command has been " diff --git a/src/xrpld/rpc/handlers/transaction/SubmitMultiSigned.cpp b/src/xrpld/rpc/handlers/transaction/SubmitMultiSigned.cpp index cc04ed073e..09301ca8a6 100644 --- a/src/xrpld/rpc/handlers/transaction/SubmitMultiSigned.cpp +++ b/src/xrpld/rpc/handlers/transaction/SubmitMultiSigned.cpp @@ -13,20 +13,20 @@ namespace xrpl { // tx_json: , // } json::Value -doSubmitMultiSigned(RPC::JsonContext& context) +doSubmitMultiSigned(rpc::JsonContext& context) { - context.loadType = Resource::kFeeHeavyBurdenRpc; + context.loadType = resource::kFeeHeavyBurdenRpc; auto const failHard = context.params[jss::fail_hard].asBool(); auto const failType = NetworkOPs::doFailHard(failHard); - return RPC::transactionSubmitMultiSigned( + return rpc::transactionSubmitMultiSigned( context.params, context.apiVersion, failType, context.role, context.ledgerMaster.getValidatedLedgerAge(), context.app, - RPC::getProcessTxnFn(context.netOps)); + rpc::getProcessTxnFn(context.netOps)); } } // namespace xrpl diff --git a/src/xrpld/rpc/handlers/transaction/TransactionEntry.cpp b/src/xrpld/rpc/handlers/transaction/TransactionEntry.cpp index bb68226334..2bd97e852f 100644 --- a/src/xrpld/rpc/handlers/transaction/TransactionEntry.cpp +++ b/src/xrpld/rpc/handlers/transaction/TransactionEntry.cpp @@ -21,10 +21,10 @@ namespace xrpl { // XXX In this case, not specify either ledger does not mean ledger current. It // means any ledger. json::Value -doTransactionEntry(RPC::JsonContext& context) +doTransactionEntry(rpc::JsonContext& context) { std::shared_ptr lpLedger; - json::Value jvResult = RPC::lookupLedger(lpLedger, context); + json::Value jvResult = rpc::lookupLedger(lpLedger, context); if (!lpLedger) return jvResult; @@ -84,7 +84,7 @@ doTransactionEntry(RPC::JsonContext& context) jvResult[jss::tx_json] = sttx->getJson(JsonOptions::Values::None); } - RPC::insertDeliverMax(jvResult[jss::tx_json], sttx->getTxnType(), context.apiVersion); + rpc::insertDeliverMax(jvResult[jss::tx_json], sttx->getTxnType(), context.apiVersion); auto const jsonMeta = (context.apiVersion > 1 ? jss::meta : jss::metadata); if (stobj) diff --git a/src/xrpld/rpc/handlers/transaction/Tx.cpp b/src/xrpld/rpc/handlers/transaction/Tx.cpp index c065bc268e..ee7110bf6b 100644 --- a/src/xrpld/rpc/handlers/transaction/Tx.cpp +++ b/src/xrpld/rpc/handlers/transaction/Tx.cpp @@ -68,8 +68,8 @@ struct TxArgs std::optional> ledgerRange; }; -std::pair -doTxHelp(RPC::Context& context, TxArgs args) +std::pair +doTxHelp(rpc::Context& context, TxArgs args) { TxResult result; @@ -169,7 +169,7 @@ doTxHelp(RPC::Context& context, TxArgs args) uint32_t const netID = context.app.getNetworkIDService().getNetworkID(); if (txnIdx <= 0xFFFFU && netID < 0xFFFFU && lgrSeq < 0x0FFF'FFFFUL) - result.ctid = RPC::encodeCTID(lgrSeq, txnIdx, netID); + result.ctid = rpc::encodeCTID(lgrSeq, txnIdx, netID); } } @@ -178,12 +178,12 @@ doTxHelp(RPC::Context& context, TxArgs args) json::Value populateJsonResponse( - std::pair const& res, + std::pair const& res, TxArgs const& args, - RPC::JsonContext const& context) + rpc::JsonContext const& context) { json::Value response; - RPC::Status const& error = res.second; + rpc::Status const& error = res.second; TxResult const& result = res.first; // handle errors if (error.toErrorCode() != RpcSuccess) @@ -215,7 +215,7 @@ populateJsonResponse( else { response[jss::tx_json] = result.txn->getJson(kOptionsJson); - RPC::insertDeliverMax( + rpc::insertDeliverMax( response[jss::tx_json], sttx->getTxnType(), context.apiVersion); } @@ -236,7 +236,7 @@ populateJsonResponse( { response = result.txn->getJson(JsonOptions::Values::IncludeDate, args.binary); if (!args.binary) - RPC::insertDeliverMax(response, sttx->getTxnType(), context.apiVersion); + rpc::insertDeliverMax(response, sttx->getTxnType(), context.apiVersion); } // populate binary metadata @@ -254,8 +254,8 @@ populateJsonResponse( { response[jss::meta] = meta->getJson(JsonOptions::Values::None); insertDeliveredAmount(response[jss::meta], context, result.txn, *meta); - RPC::insertNFTSyntheticInJson(response, sttx, *meta); - RPC::insertMPTokenIssuanceID(response[jss::meta], sttx, *meta); + rpc::insertNFTSyntheticInJson(response, sttx, *meta); + rpc::insertMPTokenIssuanceID(response[jss::meta], sttx, *meta); } } response[jss::validated] = result.validated; @@ -267,7 +267,7 @@ populateJsonResponse( } json::Value -doTxJson(RPC::JsonContext& context) +doTxJson(rpc::JsonContext& context) { if (!context.app.config().useTxTables()) return rpcError(RpcNotEnabled); @@ -291,7 +291,7 @@ doTxJson(RPC::JsonContext& context) } else if (context.params.isMember(jss::ctid)) { - auto ctid = RPC::decodeCTID(context.params[jss::ctid].asString()); + auto ctid = rpc::decodeCTID(context.params[jss::ctid].asString()); if (!ctid) return rpcError(RpcInvalidParams); @@ -302,7 +302,7 @@ doTxJson(RPC::JsonContext& context) out << "Wrong network. You should submit this request to a node " "running on NetworkID: " << net_id; - return RPC::makeError(RpcWrongNetwork, out.str()); + return rpc::makeError(RpcWrongNetwork, out.str()); } args.ctid = {lgr_seq, txn_idx}; } @@ -327,7 +327,7 @@ doTxJson(RPC::JsonContext& context) } } - std::pair const res = doTxHelp(context, args); + std::pair const res = doTxHelp(context, args); return populateJsonResponse(res, args, context); } diff --git a/src/xrpld/rpc/handlers/transaction/TxHistory.cpp b/src/xrpld/rpc/handlers/transaction/TxHistory.cpp index a45046773c..2d5ad8cbe5 100644 --- a/src/xrpld/rpc/handlers/transaction/TxHistory.cpp +++ b/src/xrpld/rpc/handlers/transaction/TxHistory.cpp @@ -16,12 +16,12 @@ namespace xrpl { // start: // } json::Value -doTxHistory(RPC::JsonContext& context) +doTxHistory(rpc::JsonContext& context) { if (!context.app.config().useTxTables()) return rpcError(RpcNotEnabled); - context.loadType = Resource::kFeeMediumBurdenRpc; + context.loadType = resource::kFeeMediumBurdenRpc; if (!context.params.isMember(jss::start)) return rpcError(RpcInvalidParams); @@ -40,7 +40,7 @@ doTxHistory(RPC::JsonContext& context) for (auto const& t : trans) { json::Value txJson = t->getJson(JsonOptions::Values::None); - RPC::insertDeliverMax(txJson, t->getSTransaction()->getTxnType(), context.apiVersion); + rpc::insertDeliverMax(txJson, t->getSTransaction()->getTxnType(), context.apiVersion); txs.append(txJson); } diff --git a/src/xrpld/rpc/handlers/transaction/TxReduceRelay.cpp b/src/xrpld/rpc/handlers/transaction/TxReduceRelay.cpp index edc4eff057..8956603012 100644 --- a/src/xrpld/rpc/handlers/transaction/TxReduceRelay.cpp +++ b/src/xrpld/rpc/handlers/transaction/TxReduceRelay.cpp @@ -7,7 +7,7 @@ namespace xrpl { json::Value -doTxReduceRelay(RPC::JsonContext& context) +doTxReduceRelay(rpc::JsonContext& context) { return context.app.getOverlay().txMetrics(); } diff --git a/src/xrpld/rpc/handlers/utility/Ping.cpp b/src/xrpld/rpc/handlers/utility/Ping.cpp index 0d34b9e0fc..68fb06456e 100644 --- a/src/xrpld/rpc/handlers/utility/Ping.cpp +++ b/src/xrpld/rpc/handlers/utility/Ping.cpp @@ -6,12 +6,12 @@ namespace xrpl { -namespace RPC { +namespace rpc { struct JsonContext; -} // namespace RPC +} // namespace rpc json::Value -doPing(RPC::JsonContext& context) +doPing(rpc::JsonContext& context) { json::Value ret(json::ValueType::Object); switch (context.role) diff --git a/src/xrpld/rpc/handlers/utility/Random.cpp b/src/xrpld/rpc/handlers/utility/Random.cpp index 56df442cf1..d3428ee6e9 100644 --- a/src/xrpld/rpc/handlers/utility/Random.cpp +++ b/src/xrpld/rpc/handlers/utility/Random.cpp @@ -10,16 +10,16 @@ namespace xrpl { -namespace RPC { +namespace rpc { struct JsonContext; -} // namespace RPC +} // namespace rpc // Result: // { // random: // } json::Value -doRandom(RPC::JsonContext& context) +doRandom(rpc::JsonContext& context) { // TODO(tom): the try/catch is almost certainly redundant, we catch at the // top level too. diff --git a/src/xrpld/shamap/NodeFamily.h b/src/xrpld/shamap/NodeFamily.h index d532f13ecc..1307d76886 100644 --- a/src/xrpld/shamap/NodeFamily.h +++ b/src/xrpld/shamap/NodeFamily.h @@ -33,13 +33,13 @@ public: NodeFamily(Application& app, CollectorManager& cm); - NodeStore::Database& + node_store::Database& db() override { return db_; } - [[nodiscard]] NodeStore::Database const& + [[nodiscard]] node_store::Database const& db() const override { return db_; @@ -80,7 +80,7 @@ public: private: Application& app_; - NodeStore::Database& db_; + node_store::Database& db_; beast::Journal const j_; std::shared_ptr fbCache_; diff --git a/tests/conan/src/example.cpp b/tests/conan/src/example.cpp index acfb253a7d..4720af4384 100644 --- a/tests/conan/src/example.cpp +++ b/tests/conan/src/example.cpp @@ -5,6 +5,6 @@ int main(int argc, char const** argv) { - std::printf("%s\n", xrpl::BuildInfo::getVersionString().c_str()); + std::printf("%s\n", xrpl::build_info::getVersionString().c_str()); return 0; }