Compare commits
82
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
777322c66b | ||
|
|
629dff22fe | ||
|
|
1d000e037b | ||
|
|
c0df4371f7 | ||
|
|
e2be7ff6de | ||
|
|
88d9b99165 | ||
|
|
361a6f4587 | ||
|
|
e19cc7a66b | ||
|
|
d496742500 | ||
|
|
9d705ec44f | ||
|
|
17e73f246a | ||
|
|
017a0eb22c | ||
|
|
a4f499ce12 | ||
|
|
b9686af9c8 | ||
|
|
e8faf398f5 | ||
|
|
0c9731a8f9 | ||
|
|
148a0ce5e4 | ||
|
|
23a67af7db | ||
|
|
709d07775b | ||
|
|
b59791e0c5 | ||
|
|
5501dfd66d | ||
|
|
ffaa51a092 | ||
|
|
a226191c1c | ||
|
|
9d84b7164e | ||
|
|
e460bdc139 | ||
|
|
a951509350 | ||
|
|
46460fcec3 | ||
|
|
5f6e7dd40d | ||
|
|
c93de9fe41 | ||
|
|
4922e49853 | ||
|
|
c82693e9c1 | ||
|
|
772c4aad11 | ||
|
|
79c6c72fd6 | ||
|
|
e83ced0e20 | ||
|
|
5faeb77411 | ||
|
|
1d0c275a04 | ||
|
|
ea8030b707 | ||
|
|
5aab1e0131 | ||
|
|
f1b4780e2c | ||
|
|
f6889940c3 | ||
|
|
31caf922b8 | ||
|
|
c0f892c539 | ||
|
|
a5fe65021b | ||
|
|
b1eb641729 | ||
|
|
ebea5f5cfd | ||
|
|
024df31e66 | ||
|
|
11c068e03e | ||
|
|
db198c05f6 | ||
|
|
2e6bcd51a1 | ||
|
|
9e01f2d5d3 | ||
|
|
f1f7662afa | ||
|
|
b17c9826c7 | ||
|
|
086ad87296 | ||
|
|
58eeaf1dd4 | ||
|
|
c7df11210a | ||
|
|
c48e7b616d | ||
|
|
c9e5f22d03 | ||
|
|
4cea798651 | ||
|
|
11adff8278 | ||
|
|
ea6dbf3e6c | ||
|
|
bb8e6b3872 | ||
|
|
3d7de8ffb7 | ||
|
|
19a3408912 | ||
|
|
970cf5bb9d | ||
|
|
a2d0365813 | ||
|
|
e9c3f36970 | ||
|
|
98e3b9d6a5 | ||
|
|
5d352139dd | ||
|
|
d3599245f8 | ||
|
|
d1495ff2d5 | ||
|
|
12c00693f8 | ||
|
|
2e88fdb937 | ||
|
|
f49083ee96 | ||
|
|
2c58237e78 | ||
|
|
7a7a5b1612 | ||
|
|
7e687e0d5d | ||
|
|
c5f669b3c0 | ||
|
|
751d8c0bda | ||
|
|
212cb30b97 | ||
|
|
3efc252059 | ||
|
|
bd52429276 | ||
|
|
a404927a78 |
@@ -0,0 +1,5 @@
|
||||
[source.crates-io]
|
||||
replace-with = "vendored-sources"
|
||||
|
||||
[source.vendored-sources]
|
||||
directory = "vendor"
|
||||
@@ -0,0 +1,37 @@
|
||||
name: public-benchmark-pages
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: [main-release-gate]
|
||||
types: [completed]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: alkane-os-public-benchmark-pages
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: alkane-pages
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install SSH client
|
||||
run: |
|
||||
if ! command -v ssh >/dev/null 2>&1; then
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq --no-install-recommends openssh-client
|
||||
fi
|
||||
- name: Render measured benchmark history
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/bench_history.py render
|
||||
- name: Publish public benchmark page
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
BENCH_PAGES_DEPLOY_KEY: ${{ secrets.BENCH_PAGES_DEPLOY_KEY }}
|
||||
run: python3 ci/publish-bench-pages.py os
|
||||
- name: Clean generated page
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -0,0 +1,24 @@
|
||||
name: completed-ci-badge
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: [main-release-gate, next-build]
|
||||
types: [completed]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: alkane-os-completed-ci-badge
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: alkane-fast
|
||||
permissions:
|
||||
actions: read
|
||||
contents: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Publish last completed attempts
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/last-attempt-badge.py
|
||||
@@ -0,0 +1,54 @@
|
||||
name: source-pr
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [main, next]
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: alkane-os-source-pr-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
source:
|
||||
runs-on: alkane-fast
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Detect README-only change
|
||||
id: surface
|
||||
run: |
|
||||
surface=full
|
||||
if [ "$GITHUB_EVENT_NAME" = pull_request ] &&
|
||||
{ [ "$GITHUB_BASE_REF" = main ] || [ "$GITHUB_BASE_REF" = next ]; } &&
|
||||
base="$(git merge-base HEAD "origin/$GITHUB_BASE_REF" 2>/dev/null)" &&
|
||||
changed="$(git diff --no-renames --name-only "$base" HEAD)" &&
|
||||
[ "$changed" = README.md ]; then
|
||||
surface=documentation
|
||||
fi
|
||||
echo "surface=$surface" >> "$GITHUB_OUTPUT"
|
||||
- name: Check README-only change
|
||||
if: steps.surface.outputs.surface == 'documentation'
|
||||
run: |
|
||||
base="$(git merge-base HEAD "origin/$GITHUB_BASE_REF")"
|
||||
git diff --check "$base" HEAD
|
||||
if git grep -nIE '^[[:space:]]*-----BEGIN (RSA |EC |OPENSSH )?PRIVATE KEY-----[[:space:]]*$' --; then
|
||||
echo "private key material found in tracked source" >&2
|
||||
exit 1
|
||||
else
|
||||
status=$?
|
||||
[ "$status" -eq 1 ]
|
||||
fi
|
||||
- name: Check source
|
||||
if: steps.surface.outputs.surface != 'documentation'
|
||||
run: |
|
||||
rustup component add rustfmt
|
||||
cargo fmt --check
|
||||
cargo test --locked
|
||||
- name: Clean generated source check
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
@@ -4,19 +4,48 @@ on:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
concurrency:
|
||||
group: alkane-os-main
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
verify-and-release:
|
||||
runs-on: alkane-ci
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
permissions:
|
||||
contents: write
|
||||
actions: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Install Pages SSH client
|
||||
run: |
|
||||
if ! command -v ssh >/dev/null 2>&1; then
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq --no-install-recommends openssh-client
|
||||
fi
|
||||
- name: Install Rust quality tools
|
||||
run: rustup component add rustfmt clippy
|
||||
- name: Formatting
|
||||
run: cargo fmt --check
|
||||
- name: Last-attempt badge checks
|
||||
run: python3 ci/test-last-attempt-badge.py
|
||||
- name: Benchmark history checks
|
||||
run: python3 ci/test-bench-history.py
|
||||
- name: Metric badge checks
|
||||
run: python3 ci/test-metrics.py
|
||||
- name: Release evidence gate checks
|
||||
run: python3 ci/test-conformance.py
|
||||
- name: Release publication checks
|
||||
run: python3 ci/test-release.py
|
||||
- name: CI cleanup containment checks
|
||||
run: python3 ci/test-cleanup-generated.py
|
||||
- name: Quality
|
||||
run: cargo clippy --all-targets --locked -- -D warnings
|
||||
run: |
|
||||
cargo clippy --all-targets --locked -- -D warnings
|
||||
printf '{"format":true,"clippy_warnings_denied":true}\n' > quality-current.json
|
||||
- name: Build
|
||||
run: cargo build --release --locked
|
||||
- name: Unit and workflow tests
|
||||
@@ -26,13 +55,28 @@ jobs:
|
||||
- name: Microbenchmarks
|
||||
run: |
|
||||
set -o pipefail
|
||||
cargo test --release --locked -- --ignored --nocapture | tee bench-current.txt
|
||||
cargo test --release --locked -- --ignored --nocapture --test-threads=1 | tee bench-current.txt
|
||||
- name: Restore coverage tool
|
||||
id: coverage-cache
|
||||
uses: actions/cache/restore@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cargo/bin/cargo-llvm-cov
|
||||
key: cargo-llvm-cov-0.8.7-rust-1.97.1-${{ runner.os }}
|
||||
- name: Coverage tool
|
||||
run: |
|
||||
rustup component add llvm-tools-preview
|
||||
if ! command -v cargo-llvm-cov >/dev/null 2>&1; then
|
||||
cargo install --locked cargo-llvm-cov --version 0.8.7
|
||||
if [ "$(cargo llvm-cov --version 2>/dev/null || true)" != 'cargo-llvm-cov 0.8.7' ]; then
|
||||
cargo install --locked --force cargo-llvm-cov --version 0.8.7
|
||||
fi
|
||||
[ "$(cargo llvm-cov --version)" = 'cargo-llvm-cov 0.8.7' ]
|
||||
- name: Save coverage tool
|
||||
if: steps.coverage-cache.outputs.cache-hit != 'true'
|
||||
uses: actions/cache/save@v4
|
||||
continue-on-error: true
|
||||
with:
|
||||
path: /root/.cargo/bin/cargo-llvm-cov
|
||||
key: cargo-llvm-cov-0.8.7-rust-1.97.1-${{ runner.os }}
|
||||
- name: Unit test coverage
|
||||
run: |
|
||||
mkdir -p coverage
|
||||
@@ -42,19 +86,36 @@ jobs:
|
||||
run: |
|
||||
CARGO_TARGET_DIR=target/coverage-bench cargo llvm-cov --json \
|
||||
--output-path coverage/bench.json --locked -- --ignored
|
||||
- name: Load previous main benchmark measurement
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/bench_history.py prepare
|
||||
- name: Generate measured badges
|
||||
run: |
|
||||
python3 ci/metrics.py --tests-output tests-current.txt \
|
||||
--tests-coverage coverage/tests.json --bench-output bench-current.txt \
|
||||
--bench-coverage coverage/bench.json
|
||||
- uses: actions/upload-artifact@v4
|
||||
--bench-coverage coverage/bench.json --quality-gate quality-current.json
|
||||
- uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7
|
||||
with:
|
||||
name: alkane-os-metrics
|
||||
path: |
|
||||
badges/metrics.json
|
||||
quality-current.json
|
||||
coverage/tests.json
|
||||
coverage/bench.json
|
||||
bench-current.txt
|
||||
- name: Publish measured badges privately
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/publish-metrics.py
|
||||
- name: Record private benchmark history
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
run: python3 ci/bench_history.py publish
|
||||
- uses: https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7
|
||||
with:
|
||||
name: alkane-os-benchmark-site-preview
|
||||
path: bench-site/index.html
|
||||
- name: Pairing and simulator conformance
|
||||
run: python3 ci/check-conformance.py
|
||||
- name: Select release version
|
||||
@@ -72,3 +133,6 @@ jobs:
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: python3 ci/release.py "${{ steps.version.outputs.tag }}" false "dist/alkane-os-${{ steps.version.outputs.version }}-source.tar.gz" dist/alkane-os dist/bench-current.txt
|
||||
- name: Clean generated build and benchmark work
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
|
||||
@@ -4,9 +4,16 @@ on:
|
||||
push:
|
||||
branches: [next]
|
||||
|
||||
concurrency:
|
||||
group: alkane-os-next
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: alkane-ci
|
||||
runs-on: alkane-fast
|
||||
env:
|
||||
CARGO_TARGET_DIR: target
|
||||
CARGO_BUILD_JOBS: 2
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
@@ -27,3 +34,6 @@ jobs:
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: python3 ci/release.py "${{ steps.version.outputs.tag }}" true "dist/alkane-os-${{ steps.version.outputs.version }}-source.tar.gz" dist/alkane-os
|
||||
- name: Clean generated candidate build
|
||||
if: always()
|
||||
run: python3 ci/cleanup-generated.py
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# Alkane OS working rules
|
||||
|
||||
- Keep the project MIT-licensed and preserve third-party licenses in vendored dependencies.
|
||||
- Use run-scoped Cargo output for one-off work. In CI, set `CARGO_TARGET_DIR=target` and remove generated target, coverage, benchmark, and release staging after artifact publication, including failed runs.
|
||||
- Keep the main release gate on all seven boards, all 64 same-vendor pairings, unmodified stock firmware evidence, and genuine pool acceptance from real captured silicon nonces. Do not promote host-only probes to board conformance.
|
||||
- Keep mining, tuning, HAL, API, protocol parsing, and on-device runtime logic in Rust. Use Python only for host tests, CI, release work, and disposable diagnostics unless a documented interface requires it.
|
||||
- No emojis in files or persisted text.
|
||||
Generated
+98
@@ -5,3 +5,101 @@ version = 4
|
||||
[[package]]
|
||||
name = "alkane-os"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.154"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "3.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
|
||||
+3
-1
@@ -8,7 +8,9 @@ description = "Portable mining and tuning core for Alkane control boards"
|
||||
[lib]
|
||||
name = "alkane_os"
|
||||
|
||||
[dependencies]
|
||||
serde_json = { version = "=1.0.154", features = ["arbitrary_precision"] }
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
|
||||
|
||||
@@ -1,51 +1,62 @@
|
||||
# Alkane OS
|
||||
|
||||
[](https://gitea.bluefla.re/blueflare/alkane-os/actions)
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
[](https://gitea.bluefla.re/blueflare/alkane-os/actions)
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
Portable mining, tuning, and management logic for Alkane control boards. The
|
||||
source is MIT licensed. `alkane-sdk` owns the board images and supplies the
|
||||
hardware backend at build time. This repository has no on-board web frontend;
|
||||
the intended management surfaces are SSH, cgminer-compatible port 4028, and a
|
||||
minimal API.
|
||||
Rust mining and tuning core for Alkane control boards. The
|
||||
[SDK](https://gitea.bluefla.re/blueflare/alkane-sdk) builds board images and
|
||||
provides the hardware backend. The miner has no on-board web frontend.
|
||||
|
||||
## Status
|
||||
|
||||
This is the initial interface and release tooling. It does not mine yet. A
|
||||
target in the matrix is an obligation to implement and validate, not a claim
|
||||
of support. All 64 theoretical same-vendor controller/chip pairings are in
|
||||
scope: 52 Bitmain and 12 WhatsMiner. For pairings a stock image rejects, the
|
||||
simulator must record the first genuine rejection gate and Alkane OS must mine.
|
||||
No live board is required by CI. Bench devices supply captures and independent
|
||||
validation of the simulator.
|
||||
The host miner supports BM1362 and BM1366 through the SDK's simulator HAL and
|
||||
Stratum V1. Tests have replayed 33 captured BM1366 shares and one BM1362
|
||||
share to a loopback pool with matching ACCEPT responses. Production board
|
||||
control is still under development.
|
||||
|
||||
The current executable lists target obligations (`alkane-os --list-targets`)
|
||||
and its version. It does not start a miner or a network service.
|
||||
The HAL client requests PSU shutdown on sensor faults and reserves work IDs
|
||||
until a confirmed reset or shutdown. A production backend still needs a
|
||||
power lease, watchdog, and validated temperature limits. SHA-256d and
|
||||
Litecoin Scrypt proof verification are implemented; Scrypt chip report
|
||||
handling and mining remain open.
|
||||
|
||||
The `main` branch has full tests and a conformance gate; releases are created
|
||||
only after every required cell passes. `next` runs a build check and produces
|
||||
release candidates. `ci/version-x.txt` is the manually edited major version.
|
||||
For this repository, Y changes when the mining protocol, HAL contract, tuning
|
||||
policy or API changes; Z changes for compatible fixes and packaging updates.
|
||||
Run `alkane-os --list-targets` for the target matrix. Set `POOL_PASSWORD` to
|
||||
the pool password, then mine with a simulator HAL socket and Stratum V1 pool:
|
||||
|
||||
```sh
|
||||
ALKANE_POOL_PASSWORD="$POOL_PASSWORD" alkane-os --mine-simulator HAL_SOCKET POOL_ADDRESS WORKER BM1366
|
||||
```
|
||||
|
||||
Use `BM1362` for that chip. Set `ALKANE_API_LISTEN=127.0.0.1:4028` for
|
||||
read-only `summary` and `version` commands; reach it remotely through SSH.
|
||||
|
||||
## Release gate
|
||||
|
||||
`main` runs tests, benchmarks, and conformance before release; `next` builds
|
||||
release candidates. The release gate is red. It requires all 64 same-vendor
|
||||
pairings (52 Bitmain, 12 WhatsMiner), unmodified stock firmware, captured
|
||||
silicon nonces, difficulty-1 pool acceptance, and bench-validated models.
|
||||
Stock firmware may faithfully reject a pairing; Alkane OS must mine it.
|
||||
See the [evidence contract](ci/CONFORMANCE.md).
|
||||
|
||||
Badges cover host code, not board conformance. See the public
|
||||
[benchmark history](https://blueflare.gitea.bluefla.re/alkane-benchmarks/os/).
|
||||
|
||||
## Local checks
|
||||
|
||||
```sh
|
||||
cargo fmt --check
|
||||
cargo clippy --all-targets -- -D warnings
|
||||
cargo test
|
||||
python3 ci/check-conformance.py
|
||||
CARGO_TARGET_DIR=/var/tmp/alkane-os-target cargo test --locked
|
||||
```
|
||||
|
||||
The last command currently fails because simulator and device evidence are
|
||||
still pending. It is a release gate, not a unit test.
|
||||
Use an executable target directory if the source mount is `noexec`.
|
||||
|
||||
The badges record measured host-side checks for this initial core. They do
|
||||
not imply that any board image or mining target has passed conformance.
|
||||
## License
|
||||
|
||||
[MIT](LICENSE). Vendored dependencies keep their own licenses.
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
# Release conformance evidence
|
||||
|
||||
`check-conformance.py` is a release gate for all 64 same-vendor cells. It
|
||||
currently fails because the simulator and bench evidence producer is not yet
|
||||
implemented. A green Rust build, host HAL replay probe, or rootfs archive does
|
||||
not fill any cell.
|
||||
|
||||
Each CI run must put a JSON report at
|
||||
`ci/evidence/<GITHUB_RUN_ID>/<board>/<chip>.json`. The report binds its
|
||||
`run_id`, `source_commit`, `os_binary_sha256`, `board`, and `chip` to the
|
||||
current workflow. It records `stock_firmware_modified: false`,
|
||||
`simulator_booted_stock: true`, and a `stock_result` of `mines` or `rejects`.
|
||||
For a rejection, `stock_first_rejection` names the first genuine firmware
|
||||
gate. Reports for stock mining also reference `stock_pool_accept_capture`.
|
||||
|
||||
The report references files under `ci/evidence/` with objects containing
|
||||
`path` and `sha256`: `sdk_image`, `stock_image`, `simulator_trace`,
|
||||
`simulator_model`, `control_capture`, `chip_capture`,
|
||||
`alkane_share_accept_capture`, and `bench_validation`.
|
||||
The checker opens and hashes each file. It loads the trace and capture JSON,
|
||||
checks the current run/image/cell identities, verifies SHA-256d or Litecoin
|
||||
Scrypt nonces against
|
||||
a pool target at difficulty 1 or higher, and requires a matching
|
||||
`mining.submit` and pool ACCEPT response ID. A SHA-256 capture must include
|
||||
the matching `subscription` (`extranonce1`, `extranonce2_size`), full
|
||||
`notify` message, exact decimal `difficulty`, and a `configure_response`
|
||||
with an accepted BIP310 mask when version bits were submitted. A later
|
||||
`version_mask_event` may supply the effective `mining.set_version_mask`.
|
||||
The checker reconstructs
|
||||
the coinbase, merkle root, and complete submitted header; its 80 bytes and
|
||||
calculated difficulty target must match the capture. The trace binds
|
||||
the capture digests.
|
||||
The Scrypt verifier uses [Litecoin's 80-byte, N=1024, r=1, p=1 proof hash](https://github.com/litecoin-project/litecoin/blob/master/src/crypto/scrypt.cpp).
|
||||
Equihash proof verification remains pending, so its cells fail closed.
|
||||
Scrypt cells also remain pending until real silicon reports, stock behavior,
|
||||
and pool acceptance are captured for those chips.
|
||||
Bench calibration names the physical control-board and chip devices, their
|
||||
capture digests, and the simulator model digest. It can be reused across OS
|
||||
commits until the model or a physical capture changes.
|
||||
The fixed digests and artifact paths also appear in `conformance.json` so
|
||||
source review can track what each cell claims.
|
||||
|
||||
The simulator runner must generate these reports by booting pristine stock
|
||||
firmware and Alkane OS in a machine model, with nonce data replayed from
|
||||
real-silicon captures. A rejected stock pairing must show its first rejection
|
||||
in the simulator trace while Alkane OS reaches pool ACCEPT. Bench devices
|
||||
provide independent observations for model validation, not CI dependencies.
|
||||
The checker verifies its input artifacts and reported nonce. It cannot prove
|
||||
that a self-asserted pool response came from a real pool or turn a report into
|
||||
physical evidence. The capture producer must preserve raw request/response
|
||||
bytes and independent provenance. Capture provenance and faithful machine
|
||||
models remain release blockers until the runner and bench validation are built.
|
||||
@@ -0,0 +1,54 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Write small badge artifacts to the private Gitea CI badge branch."""
|
||||
|
||||
import base64
|
||||
import json
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import quote
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
BADGE_BRANCH = "ci-badges"
|
||||
|
||||
|
||||
def request_json(url: str, token: str, method: str = "GET",
|
||||
payload: dict | None = None) -> dict:
|
||||
body = None if payload is None else json.dumps(payload).encode()
|
||||
request = Request(url, data=body, method=method,
|
||||
headers={"Authorization": f"token {token}",
|
||||
"Content-Type": "application/json"})
|
||||
with urlopen(request, timeout=20) as response:
|
||||
return json.load(response)
|
||||
|
||||
|
||||
def publish_file(api: str, repo: str, filename: str, data: bytes,
|
||||
token: str, message: str, max_bytes: int = 65_536) -> None:
|
||||
if not filename or filename.startswith("/") or any(
|
||||
part in ("", ".", "..") for part in filename.split("/")):
|
||||
raise ValueError("invalid badge path")
|
||||
if len(data) > max_bytes:
|
||||
raise ValueError("artifact exceeds size limit")
|
||||
path = f"{api}/repos/{repo}/contents/{quote(filename, safe='/')}"
|
||||
encoded = base64.b64encode(data).decode("ascii")
|
||||
for attempt in range(3):
|
||||
try:
|
||||
current = request_json(path + "?ref=" + BADGE_BRANCH, token)
|
||||
if base64.b64decode(current["content"]) == data:
|
||||
return
|
||||
sha = current["sha"]
|
||||
method = "PUT"
|
||||
except HTTPError as error:
|
||||
if error.code != 404:
|
||||
raise
|
||||
sha = None
|
||||
method = "POST"
|
||||
payload = {"branch": BADGE_BRANCH, "content": encoded,
|
||||
"message": message + " [skip ci]"}
|
||||
if sha is not None:
|
||||
payload["sha"] = sha
|
||||
try:
|
||||
request_json(path, token, method, payload)
|
||||
return
|
||||
except HTTPError as error:
|
||||
if error.code not in (409, 422) or attempt == 2:
|
||||
raise
|
||||
raise RuntimeError("badge write did not complete")
|
||||
@@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Keep measured main-branch benchmarks in the private CI artifact branch."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
from datetime import datetime, timezone
|
||||
import html
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
from urllib.error import HTTPError
|
||||
from urllib.parse import quote
|
||||
|
||||
from badge_store import BADGE_BRANCH, publish_file, request_json
|
||||
from bench_host import identity as benchmark_host
|
||||
|
||||
INDEX = "history/index.json"
|
||||
MAX_INDEX_ENTRIES = 50
|
||||
|
||||
|
||||
def read_artifact(api: str, repo: str, token: str, filename: str):
|
||||
url = f"{api}/repos/{repo}/contents/{quote(filename, safe='/')}?ref={BADGE_BRANCH}"
|
||||
try:
|
||||
value = request_json(url, token)
|
||||
except HTTPError as error:
|
||||
if error.code == 404:
|
||||
return None
|
||||
raise
|
||||
return json.loads(base64.b64decode(value["content"]))
|
||||
|
||||
|
||||
def valid_benches(metrics: dict) -> dict[str, float]:
|
||||
values = metrics.get("bench_ns_per_op")
|
||||
if not isinstance(values, dict) or not values:
|
||||
raise ValueError("benchmark measurements are missing")
|
||||
if any(not isinstance(key, str) or not re.fullmatch(r"[A-Za-z0-9_]+", key)
|
||||
or not isinstance(value, (float, int)) or isinstance(value, bool)
|
||||
or not math.isfinite(value) or value <= 0 for key, value in values.items()):
|
||||
raise ValueError("benchmark measurements are invalid")
|
||||
return values
|
||||
|
||||
|
||||
def git_distance(ancestor: str, sha: str) -> int | None:
|
||||
result = subprocess.run(("git", "rev-list", "--count", f"{ancestor}..{sha}"),
|
||||
capture_output=True, text=True, check=False)
|
||||
if result.returncode != 0:
|
||||
return None
|
||||
ancestor_check = subprocess.run(("git", "merge-base", "--is-ancestor", ancestor, sha),
|
||||
capture_output=True, check=False)
|
||||
return int(result.stdout.strip()) if ancestor_check.returncode == 0 else None
|
||||
|
||||
|
||||
def previous_measurement(index: dict, sha: str, distance=None, host=None) -> dict:
|
||||
distance = distance or git_distance
|
||||
choices = []
|
||||
for entry in index.get("entries", []):
|
||||
ancestor = entry.get("sha")
|
||||
if ancestor == sha or not isinstance(ancestor, str):
|
||||
continue
|
||||
if host is not None and entry.get("metrics", {}).get("bench_host") != host:
|
||||
continue
|
||||
gap = distance(ancestor, sha)
|
||||
if gap is not None and gap > 0:
|
||||
choices.append((gap, entry))
|
||||
if not choices:
|
||||
return {}
|
||||
return valid_benches(min(choices, key=lambda item: item[0])[1]["metrics"])
|
||||
|
||||
|
||||
def updated_index(index: dict, sha: str, metrics: dict, timestamp: str) -> dict:
|
||||
if not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||
raise ValueError("invalid commit SHA")
|
||||
valid_benches(metrics)
|
||||
entries = [entry for entry in index.get("entries", []) if entry.get("sha") != sha]
|
||||
entries.append({"sha": sha, "timestamp": timestamp, "metrics": metrics})
|
||||
return {"schema": 1, "entries": entries[-MAX_INDEX_ENTRIES:]}
|
||||
|
||||
|
||||
def render_html(index: dict) -> str:
|
||||
entries = list(reversed(index.get("entries", [])))
|
||||
names = sorted({name for entry in entries
|
||||
for name in valid_benches(entry["metrics"])})
|
||||
rows = []
|
||||
for entry in entries:
|
||||
sha = entry["sha"]
|
||||
if not re.fullmatch(r"[0-9a-f]{40}", sha):
|
||||
raise ValueError("invalid commit SHA in history")
|
||||
cells = [f'<td><a href="https://gitea.bluefla.re/blueflare/alkane-os/commit/{sha}">{sha[:10]}</a></td>',
|
||||
f'<td>{html.escape(entry["timestamp"])}</td>']
|
||||
values = valid_benches(entry["metrics"])
|
||||
host = entry["metrics"].get("bench_host", {})
|
||||
host_label = host.get("cpu_model", "unknown") if isinstance(host, dict) else "unknown"
|
||||
cells.append(f'<td>{html.escape(str(host_label))}</td>')
|
||||
cells += [f'<td>{values[name]:.3f}</td>' if name in values else '<td>new</td>'
|
||||
for name in names]
|
||||
rows.append("<tr>" + "".join(cells) + "</tr>")
|
||||
headings = "".join(f"<th scope=\"col\">{html.escape(name)} (ns/op)</th>" for name in names)
|
||||
return ("<!doctype html>\n<html lang=\"en\"><head><meta charset=\"utf-8\">"
|
||||
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
|
||||
"<title>Alkane OS benchmarks</title><style>"
|
||||
"body{font:16px system-ui,sans-serif;max-width:1100px;margin:2rem auto;padding:0 1rem;color:#17212b}"
|
||||
"h1{font-size:2rem}p{max-width:70ch;line-height:1.5}"
|
||||
"table{border-collapse:collapse;min-width:100%}th,td{padding:.5rem;border-bottom:1px solid #ccd3da;text-align:right}"
|
||||
"th:first-child,td:first-child{text-align:left}th:nth-child(2),td:nth-child(2){text-align:left}"
|
||||
"th{background:#edf2f6;position:sticky;top:0}a{color:#145a9a}"
|
||||
".scroll{overflow-x:auto}small{color:#55616d}</style></head><body>"
|
||||
"<h1>Alkane OS microbenchmarks</h1>"
|
||||
"<p>Measured on main CI. Lower ns/op is faster. Results depend on runner load and are "
|
||||
"not board performance. A new benchmark has no earlier comparison.</p>"
|
||||
f"<p><small>Most recent {len(entries)} measured commits. Full per-commit JSON is retained "
|
||||
"in the private ci-badges branch.</small></p>"
|
||||
f"<div class=\"scroll\"><table><thead><tr><th scope=\"col\">Commit</th>"
|
||||
f"<th scope=\"col\">UTC</th><th scope=\"col\">CPU</th>{headings}</tr></thead><tbody>{''.join(rows)}"
|
||||
"</tbody></table></div></body></html>\n")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("action", choices=("prepare", "publish", "render"))
|
||||
args = parser.parse_args()
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
sha = os.environ["GITHUB_SHA"]
|
||||
index = read_artifact(api, repo, token, INDEX) or {"schema": 1, "entries": []}
|
||||
if index.get("schema") != 1 or not isinstance(index.get("entries"), list):
|
||||
raise ValueError("unsupported benchmark history index")
|
||||
if args.action == "prepare":
|
||||
baseline = previous_measurement(index, sha, host=benchmark_host())
|
||||
Path("badges").mkdir(exist_ok=True)
|
||||
Path("badges/bench-baseline.json").write_text(json.dumps(baseline) + "\n")
|
||||
print(f"Loaded {len(baseline)} prior benchmark measurements")
|
||||
return
|
||||
if args.action == "render":
|
||||
if not index["entries"]:
|
||||
raise ValueError("no measured benchmark history to publish")
|
||||
Path("bench-site").mkdir(exist_ok=True)
|
||||
Path("bench-site/index.html").write_text(render_html(index))
|
||||
print(f"Rendered {len(index['entries'])} measured benchmark commits")
|
||||
return
|
||||
metrics = json.loads(Path("badges/metrics.json").read_text())
|
||||
timestamp = datetime.now(timezone.utc).isoformat(timespec="seconds")
|
||||
index = updated_index(index, sha, metrics, timestamp)
|
||||
record = json.dumps(index["entries"][-1], indent=2).encode() + b"\n"
|
||||
publish_file(api, repo, f"history/{sha}.json", record, token,
|
||||
"Record Alkane OS benchmarks", max_bytes=1_048_576)
|
||||
publish_file(api, repo, INDEX, json.dumps(index, indent=2).encode() + b"\n",
|
||||
token, "Update Alkane OS benchmark index", max_bytes=1_048_576)
|
||||
Path("bench-site").mkdir(exist_ok=True)
|
||||
Path("bench-site/index.html").write_text(render_html(index))
|
||||
print(f"Recorded benchmarks for {sha[:10]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Identify the CPU class used for comparable CI microbenchmark baselines."""
|
||||
|
||||
import os
|
||||
import platform
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def identity() -> dict[str, object]:
|
||||
model = "unknown"
|
||||
cpuinfo = Path("/proc/cpuinfo")
|
||||
if cpuinfo.is_file():
|
||||
for line in cpuinfo.read_text(errors="replace").splitlines():
|
||||
if line.startswith(("model name\t", "Hardware\t", "Processor\t")):
|
||||
model = line.partition(":")[2].strip()[:160]
|
||||
break
|
||||
return {"architecture": platform.machine(), "cpu_model": model,
|
||||
"logical_cpus": os.cpu_count() or 1}
|
||||
+394
-26
@@ -1,33 +1,401 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fail the final release gate until every target has traceable evidence."""
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Require the binary target matrix and traceable per-cell release evidence."""
|
||||
|
||||
import argparse
|
||||
from decimal import Decimal, InvalidOperation
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
manifest = json.loads(pathlib.Path("ci/conformance.json").read_text())
|
||||
rows = manifest["targets"]
|
||||
expected = {(board, chip) for board in manifest["boards"] for chip in manifest["chips"]
|
||||
if manifest["vendors"][board] == manifest["vendors"][chip]}
|
||||
actual = {(row["board"], row["chip"]) for row in rows}
|
||||
if actual != expected or len(rows) != len(expected):
|
||||
sys.exit(f"matrix mismatch: {len(actual)} present, {len(expected)} expected")
|
||||
|
||||
required = ("sdk_image_sha256", "stock_image_sha256", "stock_result",
|
||||
"alkane_share_accept_capture", "simulator_trace_sha256",
|
||||
"bench_validation")
|
||||
incomplete = []
|
||||
for row in rows:
|
||||
missing = [field for field in required if not row.get(field)]
|
||||
if row.get("stock_result") not in (None, "mines", "rejects"):
|
||||
missing.append("stock_result invalid")
|
||||
if row.get("stock_result") == "rejects" and not row.get("stock_first_rejection"):
|
||||
missing.append("stock_first_rejection")
|
||||
if missing:
|
||||
incomplete.append((row["board"], row["chip"], missing))
|
||||
if incomplete:
|
||||
print(f"Conformance pending: {len(incomplete)}/{len(rows)} targets")
|
||||
for board, chip, missing in incomplete[:8]:
|
||||
print(f" {board} x {chip}: {', '.join(missing)}")
|
||||
sys.exit(1)
|
||||
print(f"Conformance complete: {len(rows)} targets")
|
||||
REQUIRED_BOARDS = {
|
||||
"am335x-bb": "bitmain",
|
||||
"zynq-7010": "bitmain",
|
||||
"cv1835": "bitmain",
|
||||
"a113d": "bitmain",
|
||||
"allwinner-h3": "whatsminer",
|
||||
"allwinner-h6": "whatsminer",
|
||||
"allwinner-h616": "whatsminer",
|
||||
}
|
||||
REQUIRED_CHIPS = {
|
||||
**{chip: "bitmain" for chip in (
|
||||
"BM1384", "BM1387", "BM1360", "BM1362", "BM1366", "BM1368",
|
||||
"BM1370", "BM1396", "BM1397", "BM1398", "BM1489", "BM1491", "BM1746",
|
||||
)},
|
||||
**{chip: "whatsminer" for chip in (
|
||||
"KF1950", "ID-1968", "ID-1980", "ID-1990",
|
||||
)},
|
||||
}
|
||||
REQUIRED_ALGORITHMS = {
|
||||
**{chip: "Sha256" for chip in REQUIRED_CHIPS if chip not in
|
||||
("BM1489", "BM1491", "BM1746")},
|
||||
"BM1489": "Scrypt", "BM1491": "Scrypt", "BM1746": "Equihash",
|
||||
}
|
||||
DIGEST = re.compile(r"[0-9a-f]{64}\Z")
|
||||
COMMIT = re.compile(r"[0-9a-f]{40}\Z")
|
||||
DIFF_ONE_TARGET = 0xffff << (8 * (0x1d - 3))
|
||||
MAX_REPORT_BYTES = 1_048_576
|
||||
|
||||
|
||||
def sha256_file(path: pathlib.Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for chunk in iter(lambda: source.read(1_048_576), b""):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def load_report(path: pathlib.Path) -> dict:
|
||||
if path.stat().st_size > MAX_REPORT_BYTES:
|
||||
raise ValueError("evidence report exceeds 1 MiB")
|
||||
value = json.loads(path.read_text())
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError("evidence report is not an object")
|
||||
return value
|
||||
|
||||
|
||||
def artifact(root: pathlib.Path, value: object) -> pathlib.Path:
|
||||
if not isinstance(value, dict) or not isinstance(value.get("path"), str):
|
||||
raise ValueError("artifact reference is missing")
|
||||
digest = value.get("sha256")
|
||||
if not isinstance(digest, str) or not DIGEST.fullmatch(digest):
|
||||
raise ValueError("artifact digest is invalid")
|
||||
path = (root / value["path"]).resolve()
|
||||
if not path.is_relative_to(root.resolve()) or not path.is_file():
|
||||
raise ValueError("artifact is missing or outside evidence directory")
|
||||
if sha256_file(path) != digest:
|
||||
raise ValueError("artifact digest mismatch")
|
||||
return path
|
||||
|
||||
|
||||
def hex_field(value: object, size: int | None = None) -> bytes:
|
||||
if (not isinstance(value, str) or len(value) & 1
|
||||
or len(value) > 131_072 or not re.fullmatch(r"[0-9a-fA-F]*", value)
|
||||
or (size is not None and len(value) != size * 2)):
|
||||
raise ValueError("invalid Stratum hex field")
|
||||
return bytes.fromhex(value)
|
||||
|
||||
|
||||
def u32_hex(value: object) -> int:
|
||||
return int.from_bytes(hex_field(value, 4), "big")
|
||||
|
||||
|
||||
def reconstructed_header(capture: dict, params: list[str]) -> tuple[bytes, int]:
|
||||
subscription = capture.get("subscription")
|
||||
notify = capture.get("notify")
|
||||
if not isinstance(subscription, dict) or not isinstance(notify, dict):
|
||||
raise ValueError("subscription and notify are required to bind the share")
|
||||
extra1 = hex_field(subscription.get("extranonce1"))
|
||||
extra_size = subscription.get("extranonce2_size")
|
||||
if (len(extra1) > 32 or type(extra_size) is not int
|
||||
or not 0 <= extra_size <= 32):
|
||||
raise ValueError("invalid subscription extranonce")
|
||||
extra2 = hex_field(params[2], extra_size)
|
||||
job = notify.get("params")
|
||||
if (notify.get("method") != "mining.notify" or not isinstance(job, list)
|
||||
or len(job) != 9 or job[0] != params[1]
|
||||
or not isinstance(job[4], list) or len(job[4]) > 64
|
||||
or type(job[8]) is not bool):
|
||||
raise ValueError("submission is not bound to a matching notify")
|
||||
prevhash = hex_field(job[1], 32)
|
||||
coinbase = hex_field(job[2]) + extra1 + extra2 + hex_field(job[3])
|
||||
if len(coinbase) > 65_536:
|
||||
raise ValueError("coinbase exceeds supported size")
|
||||
root = hashlib.sha256(hashlib.sha256(coinbase).digest()).digest()
|
||||
for branch in job[4]:
|
||||
pair = root + hex_field(branch, 32)
|
||||
root = hashlib.sha256(hashlib.sha256(pair).digest()).digest()
|
||||
base_version = u32_hex(job[5])
|
||||
nbits = u32_hex(job[6])
|
||||
u32_hex(job[7])
|
||||
submitted_ntime = u32_hex(params[3])
|
||||
submitted_nonce = u32_hex(params[4])
|
||||
if len(params) == 6:
|
||||
configure = capture.get("configure_response")
|
||||
result = configure.get("result") if isinstance(configure, dict) else None
|
||||
if not isinstance(result, dict) or result.get("version-rolling") is not True:
|
||||
raise ValueError("version rolling was not negotiated")
|
||||
mask_text = result.get("version-rolling.mask")
|
||||
mask_event = capture.get("version_mask_event")
|
||||
if mask_event is not None:
|
||||
fields = mask_event.get("params") if isinstance(mask_event, dict) else None
|
||||
if (not isinstance(mask_event, dict)
|
||||
or mask_event.get("method") != "mining.set_version_mask"
|
||||
or not isinstance(fields, list) or len(fields) != 1):
|
||||
raise ValueError("invalid effective version mask event")
|
||||
mask_text = fields[0]
|
||||
mask = u32_hex(mask_text)
|
||||
bits = u32_hex(params[5])
|
||||
if bits & ~mask:
|
||||
raise ValueError("submitted version bits exceed negotiated mask")
|
||||
version = (base_version & ~mask) | bits
|
||||
else:
|
||||
if capture.get("configure_response") is not None or capture.get("version_mask_event") is not None:
|
||||
raise ValueError("negotiated version mask requires submission bits")
|
||||
version = base_version
|
||||
header = bytearray()
|
||||
header.extend(version.to_bytes(4, "little"))
|
||||
for offset in range(0, 32, 4):
|
||||
header.extend(prevhash[offset:offset + 4][::-1])
|
||||
header.extend(root)
|
||||
header.extend(submitted_ntime.to_bytes(4, "little"))
|
||||
header.extend(nbits.to_bytes(4, "little"))
|
||||
header.extend(submitted_nonce.to_bytes(4, "little"))
|
||||
return bytes(header), submitted_nonce
|
||||
|
||||
|
||||
def proof_digest(header: bytes, algorithm: str) -> bytes:
|
||||
if algorithm == "Sha256":
|
||||
return hashlib.sha256(hashlib.sha256(header).digest()).digest()
|
||||
if algorithm == "Scrypt":
|
||||
return hashlib.scrypt(header, salt=header, n=1024, r=1, p=1, dklen=32)
|
||||
raise ValueError(f"{algorithm} share verifier is not implemented")
|
||||
|
||||
|
||||
def verify_accept(capture: dict, algorithm: str) -> None:
|
||||
if algorithm not in ("Sha256", "Scrypt"):
|
||||
raise ValueError(f"{algorithm} share verifier is not implemented")
|
||||
if capture.get("nonce_source") != "real_silicon_capture":
|
||||
raise ValueError("share was not sourced from captured silicon")
|
||||
header_hex = capture.get("header80")
|
||||
target_hex = capture.get("pool_target_le")
|
||||
nonce = capture.get("nonce")
|
||||
if (not isinstance(header_hex, str) or len(header_hex) != 160
|
||||
or not isinstance(target_hex, str) or not DIGEST.fullmatch(target_hex)
|
||||
or type(nonce) is not int or not 0 <= nonce <= 0xffffffff):
|
||||
raise ValueError("invalid accepted share work")
|
||||
try:
|
||||
header = bytearray.fromhex(header_hex)
|
||||
except ValueError as error:
|
||||
raise ValueError("invalid accepted share header") from error
|
||||
if int.from_bytes(header[76:80], "little") != nonce:
|
||||
raise ValueError("nonce differs from submitted header")
|
||||
submission = capture.get("submission")
|
||||
response = capture.get("pool_response")
|
||||
params = submission.get("params") if isinstance(submission, dict) else None
|
||||
if (not isinstance(params, list) or len(params) not in (5, 6)
|
||||
or not all(isinstance(value, str) for value in params)
|
||||
or not params[0] or not params[1]
|
||||
or params[3].lower() != f"{int.from_bytes(header[68:72], 'little'):08x}"
|
||||
or params[4].lower() != f"{nonce:08x}"
|
||||
or (len(params) == 6 and not re.fullmatch(r"[0-9a-f]{8}", params[5].lower()))
|
||||
or not isinstance(response, dict)
|
||||
or submission.get("method") != "mining.submit"
|
||||
or type(submission.get("id")) is not int
|
||||
or response.get("id") != submission["id"]
|
||||
or response.get("result") is not True
|
||||
or response.get("error") is not None):
|
||||
raise ValueError("no matching pool ACCEPT for submitted nonce")
|
||||
target = int.from_bytes(bytes.fromhex(target_hex), "little")
|
||||
if not 0 < target <= DIFF_ONE_TARGET:
|
||||
raise ValueError("pool target is below difficulty one")
|
||||
difficulty_text = capture.get("difficulty")
|
||||
if not isinstance(difficulty_text, str) or len(difficulty_text) > 128:
|
||||
raise ValueError("effective pool difficulty is missing")
|
||||
try:
|
||||
difficulty = Decimal(difficulty_text)
|
||||
except InvalidOperation as error:
|
||||
raise ValueError("invalid pool difficulty") from error
|
||||
if not difficulty.is_finite() or difficulty < 1:
|
||||
raise ValueError("pool difficulty is below one")
|
||||
if abs(difficulty.as_tuple().exponent) > 64:
|
||||
raise ValueError("pool difficulty exceeds supported range")
|
||||
numerator, denominator = difficulty.as_integer_ratio()
|
||||
expected_target = DIFF_ONE_TARGET * denominator // numerator
|
||||
if target != expected_target:
|
||||
raise ValueError("pool target differs from effective difficulty")
|
||||
rebuilt, submitted_nonce = reconstructed_header(capture, params)
|
||||
if submitted_nonce != nonce or rebuilt != header:
|
||||
raise ValueError("submitted Stratum job differs from captured header")
|
||||
hash_bytes = proof_digest(header, algorithm)
|
||||
if int.from_bytes(hash_bytes, "little") > target:
|
||||
raise ValueError("captured nonce misses the pool target")
|
||||
|
||||
|
||||
def verify_cell(row: dict, root: pathlib.Path, run_id: str,
|
||||
source_commit: str, binary_digest: str,
|
||||
algorithm: str) -> None:
|
||||
board, chip = row["board"], row["chip"]
|
||||
report_path = (root / run_id / board / f"{chip}.json").resolve()
|
||||
if not report_path.is_relative_to(root.resolve()):
|
||||
raise ValueError("evidence report is outside evidence directory")
|
||||
report = load_report(report_path)
|
||||
identity = {"schema": 1, "run_id": run_id, "source_commit": source_commit,
|
||||
"os_binary_sha256": binary_digest, "board": board, "chip": chip,
|
||||
"stock_firmware_modified": False, "algorithm": algorithm}
|
||||
if any(report.get(key) != value for key, value in identity.items()):
|
||||
raise ValueError("evidence belongs to a different run, source, binary, or cell")
|
||||
if report.get("stock_result") != row.get("stock_result"):
|
||||
raise ValueError("stock result differs from manifest")
|
||||
if report.get("stock_first_rejection") != row.get("stock_first_rejection"):
|
||||
raise ValueError("stock rejection differs from manifest")
|
||||
if report["stock_result"] == "rejects" and not report.get("stock_first_rejection"):
|
||||
raise ValueError("stock rejection gate is missing")
|
||||
if report.get("simulator_booted_stock") is not True:
|
||||
raise ValueError("unmodified stock boot is unverified")
|
||||
paths = {}
|
||||
for field, digest_field in (("sdk_image", "sdk_image_sha256"),
|
||||
("stock_image", "stock_image_sha256"),
|
||||
("simulator_trace", "simulator_trace_sha256")):
|
||||
paths[field] = artifact(root, report.get(field))
|
||||
if report[field]["sha256"] != row.get(digest_field):
|
||||
raise ValueError(f"{field} digest differs from manifest")
|
||||
artifact(root, report.get("simulator_model"))
|
||||
model_digest = report["simulator_model"]["sha256"]
|
||||
for field in ("control_capture", "chip_capture"):
|
||||
artifact(root, report.get(field))
|
||||
trace = load_report(paths["simulator_trace"])
|
||||
trace_identity = {"run_id": run_id, "source_commit": source_commit,
|
||||
"board": board, "chip": chip,
|
||||
"sdk_image_sha256": row["sdk_image_sha256"],
|
||||
"stock_image_sha256": row["stock_image_sha256"],
|
||||
"stock_firmware_modified": False,
|
||||
"stock_result": row["stock_result"],
|
||||
"algorithm": algorithm,
|
||||
"simulator_model_sha256": model_digest}
|
||||
if any(trace.get(key) != value for key, value in trace_identity.items()):
|
||||
raise ValueError("simulator trace does not bind stock image and current run")
|
||||
if report["stock_result"] == "rejects":
|
||||
if trace.get("first_rejection") != row["stock_first_rejection"]:
|
||||
raise ValueError("stock rejection gate is absent from simulator trace")
|
||||
else:
|
||||
stock_capture = load_report(artifact(root, report.get("stock_pool_accept_capture")))
|
||||
if trace.get("stock_pool_accept_capture_sha256") != report["stock_pool_accept_capture"]["sha256"]:
|
||||
raise ValueError("stock pool acceptance is not linked to simulator trace")
|
||||
if (stock_capture.get("board") != board or stock_capture.get("chip") != chip
|
||||
or stock_capture.get("firmware") != "stock"):
|
||||
raise ValueError("stock pool acceptance belongs to another firmware or cell")
|
||||
verify_accept(stock_capture, algorithm)
|
||||
capture = load_report(artifact(root, report.get("alkane_share_accept_capture")))
|
||||
if report["alkane_share_accept_capture"]["path"] != row.get("alkane_share_accept_capture"):
|
||||
raise ValueError("accepted share artifact differs from manifest")
|
||||
if trace.get("alkane_share_accept_capture_sha256") != report["alkane_share_accept_capture"]["sha256"]:
|
||||
raise ValueError("Alkane pool acceptance is not linked to simulator trace")
|
||||
if (capture.get("board") != board or capture.get("chip") != chip
|
||||
or capture.get("firmware") != "alkane"):
|
||||
raise ValueError("accepted share belongs to another cell")
|
||||
verify_accept(capture, algorithm)
|
||||
bench = load_report(artifact(root, report.get("bench_validation")))
|
||||
if report["bench_validation"]["path"] != row.get("bench_validation"):
|
||||
raise ValueError("bench artifact differs from manifest")
|
||||
if (bench.get("board") != board or bench.get("chip") != chip
|
||||
or bench.get("validated") is not True
|
||||
or bench.get("simulator_model_sha256") != model_digest
|
||||
or any(bench.get(f"{field}_sha256") != report[field]["sha256"]
|
||||
for field in ("control_capture", "chip_capture"))
|
||||
or any(not isinstance(bench.get(f"{name}_device_id"), str)
|
||||
or not bench[f"{name}_device_id"]
|
||||
for name in ("control", "chip"))):
|
||||
raise ValueError("bench validation is missing or belongs to another cell")
|
||||
|
||||
|
||||
def binary_targets(path: pathlib.Path) -> dict[tuple[str, str], str]:
|
||||
output = subprocess.run(
|
||||
[str(path), "--list-targets"],
|
||||
check=True, capture_output=True, text=True, timeout=15,
|
||||
).stdout.splitlines()
|
||||
if not output or output[0] != "control_board,hash_chip,algorithm":
|
||||
raise ValueError("Alkane OS binary has no target matrix header")
|
||||
targets = {}
|
||||
for line in output[1:]:
|
||||
parts = line.split(",")
|
||||
if len(parts) != 3 or not all(parts):
|
||||
raise ValueError(f"invalid binary target row: {line!r}")
|
||||
cell = (parts[0], parts[1])
|
||||
if cell in targets:
|
||||
raise ValueError("duplicate binary target")
|
||||
targets[cell] = parts[2]
|
||||
return targets
|
||||
|
||||
|
||||
def validate(manifest: dict, built_targets: dict[tuple[str, str], str],
|
||||
evidence_root: pathlib.Path = pathlib.Path("ci/evidence"),
|
||||
run_id: str = "", source_commit: str = "",
|
||||
binary_digest: str = "") -> list[str]:
|
||||
errors = []
|
||||
if manifest.get("schema") != 1:
|
||||
errors.append("unsupported conformance manifest schema")
|
||||
boards = set(manifest["boards"])
|
||||
chips = set(manifest["chips"])
|
||||
vendors = manifest["vendors"]
|
||||
if len(boards) != len(manifest["boards"]) or len(chips) != len(manifest["chips"]):
|
||||
errors.append("duplicate board or chip in manifest")
|
||||
for name, vendor in {**REQUIRED_BOARDS, **REQUIRED_CHIPS}.items():
|
||||
if name not in boards | chips or vendors.get(name) != vendor:
|
||||
errors.append(f"required target identity changed: {name}")
|
||||
if set(vendors) != boards | chips:
|
||||
errors.append("vendor map differs from board and chip lists")
|
||||
expected = {(board, chip) for board in boards for chip in chips
|
||||
if vendors.get(board) == vendors.get(chip)}
|
||||
rows = manifest["targets"]
|
||||
actual = {(row["board"], row["chip"]) for row in rows}
|
||||
if len(expected) < 64 or actual != expected or len(rows) != len(expected):
|
||||
errors.append(f"manifest matrix mismatch: {len(actual)} present, {len(expected)} expected")
|
||||
if set(built_targets) != expected:
|
||||
errors.append(f"binary matrix mismatch: {len(built_targets)} built, {len(expected)} expected")
|
||||
for (board, chip), algorithm in built_targets.items():
|
||||
if chip in REQUIRED_ALGORITHMS and algorithm != REQUIRED_ALGORITHMS[chip]:
|
||||
errors.append(f"binary algorithm mismatch: {board} x {chip}")
|
||||
if (not run_id.isdecimal() or not COMMIT.fullmatch(source_commit)
|
||||
or not DIGEST.fullmatch(binary_digest)):
|
||||
errors.append("current run, source commit, or binary digest is unbound")
|
||||
required = ("sdk_image_sha256", "stock_image_sha256", "stock_result",
|
||||
"alkane_share_accept_capture", "simulator_trace_sha256",
|
||||
"bench_validation")
|
||||
for row in rows:
|
||||
cell = f"{row['board']} x {row['chip']}"
|
||||
missing = [field for field in required if not row.get(field)]
|
||||
if row.get("stock_result") not in (None, "mines", "rejects"):
|
||||
missing.append("stock_result invalid")
|
||||
if row.get("stock_result") == "rejects" and not row.get("stock_first_rejection"):
|
||||
missing.append("stock_first_rejection")
|
||||
for field in ("sdk_image_sha256", "stock_image_sha256", "simulator_trace_sha256"):
|
||||
value = row.get(field)
|
||||
if value and (not isinstance(value, str) or not DIGEST.fullmatch(value)):
|
||||
missing.append(f"{field} invalid")
|
||||
if missing:
|
||||
errors.append(f"{cell}: {', '.join(missing)}")
|
||||
elif run_id.isdecimal() and COMMIT.fullmatch(source_commit) and DIGEST.fullmatch(binary_digest):
|
||||
try:
|
||||
verify_cell(row, evidence_root, run_id, source_commit, binary_digest,
|
||||
built_targets.get((row["board"], row["chip"]), "unknown"))
|
||||
except (OSError, ValueError, KeyError, TypeError, json.JSONDecodeError) as error:
|
||||
errors.append(f"{cell}: evidence invalid: {error}")
|
||||
return errors
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--manifest", type=pathlib.Path,
|
||||
default=pathlib.Path("ci/conformance.json"))
|
||||
parser.add_argument("--binary", type=pathlib.Path,
|
||||
default=pathlib.Path(os.environ.get("CARGO_TARGET_DIR", "target"))
|
||||
/ "release/alkane-os")
|
||||
parser.add_argument("--evidence-dir", type=pathlib.Path,
|
||||
default=pathlib.Path("ci/evidence"))
|
||||
parser.add_argument("--run-id", default=os.environ.get("GITHUB_RUN_ID", ""))
|
||||
args = parser.parse_args()
|
||||
manifest = json.loads(args.manifest.read_text())
|
||||
try:
|
||||
built_targets = binary_targets(args.binary.resolve())
|
||||
except (OSError, subprocess.SubprocessError, ValueError) as error:
|
||||
sys.exit(f"cannot verify Alkane OS binary target matrix: {error}")
|
||||
source_commit = subprocess.run(["git", "rev-parse", "HEAD"], check=True,
|
||||
capture_output=True, text=True).stdout.strip()
|
||||
errors = validate(manifest, built_targets, args.evidence_dir, args.run_id,
|
||||
source_commit, sha256_file(args.binary.resolve()))
|
||||
if errors:
|
||||
print(f"Conformance pending: {len(errors)} issues across {len(manifest['targets'])} targets")
|
||||
for error in errors[:8]:
|
||||
print(f" {error}")
|
||||
sys.exit(1)
|
||||
print(f"Conformance complete: {len(manifest['targets'])} targets")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Remove generated Alkane OS CI files after tests and release upload."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DIRECTORIES = ("target", "coverage", "dist", "bench-site")
|
||||
FILES = ("tests-current.txt", "bench-current.txt", "quality-current.json")
|
||||
|
||||
|
||||
def cleanup() -> None:
|
||||
if not os.environ.get("GITHUB_RUN_ID"):
|
||||
raise ValueError("cleanup is restricted to CI runs")
|
||||
for name in DIRECTORIES:
|
||||
path = ROOT / name
|
||||
if path.is_symlink() or path.parent.resolve() != ROOT.resolve():
|
||||
raise ValueError(f"unsafe generated directory: {path}")
|
||||
if path.is_dir():
|
||||
shutil.rmtree(path)
|
||||
print(f"Removed generated directory: {name}")
|
||||
elif path.exists():
|
||||
raise ValueError(f"expected a generated directory: {path}")
|
||||
for name in FILES:
|
||||
path = ROOT / name
|
||||
if path.is_symlink() or path.parent.resolve() != ROOT.resolve():
|
||||
raise ValueError(f"unsafe generated file: {path}")
|
||||
if path.is_file():
|
||||
path.unlink()
|
||||
print(f"Removed generated file: {name}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
cleanup()
|
||||
except (OSError, ValueError) as error:
|
||||
print(f"ERROR: Alkane OS cleanup: {error}", file=sys.stderr)
|
||||
raise SystemExit(1)
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish the last completed main/next CI result to a private badge branch."""
|
||||
|
||||
import os
|
||||
from urllib.parse import quote, urlencode
|
||||
|
||||
from badge_store import publish_file, request_json
|
||||
|
||||
|
||||
WORKFLOWS = {"main.yml": "main", "next.yml": "next"}
|
||||
|
||||
|
||||
def badge(branch: str, conclusion: str, run_id: int) -> bytes:
|
||||
passed = conclusion == "success"
|
||||
word = "pass" if passed else "fail"
|
||||
color = "#2ea043" if passed else "#d1242f"
|
||||
title = f"CI {branch}: last completed attempt {word} (run {run_id})"
|
||||
return (f'<svg xmlns="http://www.w3.org/2000/svg" width="102" height="20" '
|
||||
f'role="img" aria-label="{title}"><title>{title}</title>'
|
||||
'<rect width="102" height="20" rx="3" fill="#555"/>'
|
||||
f'<path d="M39 0h60a3 3 0 0 1 3 3v14a3 3 0 0 1-3 3H39z" fill="{color}"/>'
|
||||
'<text x="20" y="14" fill="#fff" text-anchor="middle" '
|
||||
'font-family="sans-serif" font-size="11">CI</text>'
|
||||
f'<text x="70" y="14" fill="#fff" text-anchor="middle" '
|
||||
f'font-family="sans-serif" font-size="11">{word}</text></svg>\n').encode()
|
||||
|
||||
|
||||
def latest_completed(api: str, repo: str, workflow: str, branch: str,
|
||||
token: str) -> tuple[int, str]:
|
||||
path = quote(workflow, safe="")
|
||||
latest = None
|
||||
for page in range(1, 21):
|
||||
query = urlencode({"branch": branch, "limit": 50, "page": page})
|
||||
url = f"{api}/repos/{repo}/actions/workflows/{path}/runs?{query}"
|
||||
runs = request_json(url, token).get("workflow_runs", [])
|
||||
for run in runs:
|
||||
if run.get("head_branch") != branch or run.get("status") != "completed":
|
||||
continue
|
||||
if run.get("conclusion") not in ("success", "failure"):
|
||||
continue
|
||||
candidate = (int(run["id"]), run["conclusion"])
|
||||
if latest is None or candidate[0] > latest[0]:
|
||||
latest = candidate
|
||||
if len(runs) < 50:
|
||||
break
|
||||
if latest is None:
|
||||
raise RuntimeError(f"no completed {branch} run found for {workflow}")
|
||||
return latest
|
||||
|
||||
|
||||
def publish(api: str, repo: str, branch: str, data: bytes, token: str) -> None:
|
||||
publish_file(api, repo, f"{branch}.svg", data, token,
|
||||
f"Record completed {branch} CI attempt")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if os.environ["GITHUB_EVENT_NAME"] not in ("workflow_run", "workflow_dispatch"):
|
||||
raise RuntimeError("badge publisher must follow CI completion or manual dispatch")
|
||||
if os.environ["GITHUB_REF"] != "refs/heads/main":
|
||||
raise RuntimeError("badge publisher must use the main branch")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
completed = {branch: latest_completed(api, repo, workflow, branch, token)
|
||||
for workflow, branch in WORKFLOWS.items()}
|
||||
for branch, (run_id, conclusion) in completed.items():
|
||||
publish(api, repo, branch, badge(branch, conclusion, run_id), token)
|
||||
print(f"{branch} badge records completed run {run_id}: {conclusion}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+61
-10
@@ -4,9 +4,25 @@
|
||||
import argparse
|
||||
import html
|
||||
import json
|
||||
import math
|
||||
import pathlib
|
||||
import re
|
||||
import tomllib
|
||||
from bench_host import identity as benchmark_host
|
||||
|
||||
EXPECTED_BENCHES = frozenset({
|
||||
"target_matrix", "power_policy", "share_verification",
|
||||
"scrypt_share_verification", "bm1366_work_encoding", "bm1362_commands",
|
||||
"bm1362_job_encoding", "bm1366_report_decode", "stratum_header",
|
||||
"stratum_submission", "stratum_authorize_format", "stratum_notify_parse",
|
||||
"stratum_control_parse", "stratum_configure_parse", "difficulty_target",
|
||||
"stratum_job_issue", "bm1366_session_cycle", "bm1362_work_encoding",
|
||||
"stratum_configure_format", "stratum_subscribe_format",
|
||||
"stratum_subscription_parse", "stratum_accept_parse",
|
||||
"hal_socket_exchange", "stratum_socket_round_trip",
|
||||
"bm13xx_crc5", "bm13xx_crc16", "bm1366_captured_report_poll",
|
||||
})
|
||||
BENCH_LINE = re.compile(r"\bBENCH ([A-Za-z0-9_]+) ([0-9]+(?:\.[0-9]+)?) ns/op\Z")
|
||||
|
||||
|
||||
def coverage(path):
|
||||
@@ -15,8 +31,40 @@ def coverage(path):
|
||||
|
||||
|
||||
def bench_results(path):
|
||||
text = pathlib.Path(path).read_text()
|
||||
return {name: float(ns) for name, ns in re.findall(r"BENCH (\w+) ([0-9.]+) ns/op", text)}
|
||||
results = {}
|
||||
for line in pathlib.Path(path).read_text().splitlines():
|
||||
if "BENCH " not in line:
|
||||
continue
|
||||
match = BENCH_LINE.search(line)
|
||||
if match is None:
|
||||
raise ValueError("malformed microbenchmark result")
|
||||
name, value = match.groups()
|
||||
measurement = float(value)
|
||||
if name in results or not math.isfinite(measurement) or measurement <= 0:
|
||||
raise ValueError("duplicate or invalid microbenchmark result")
|
||||
results[name] = measurement
|
||||
return results
|
||||
|
||||
|
||||
def benchmark_delta(current, baseline):
|
||||
if set(current) != EXPECTED_BENCHES:
|
||||
raise ValueError("microbenchmark set differs from the declared suite")
|
||||
if not isinstance(baseline, dict) or not set(baseline).issubset(current):
|
||||
raise ValueError("baseline contains retired or invalid benchmarks")
|
||||
if any(not isinstance(value, (int, float)) or isinstance(value, bool)
|
||||
or not math.isfinite(value) or value <= 0 for value in baseline.values()):
|
||||
raise ValueError("baseline contains invalid measurements")
|
||||
if not baseline:
|
||||
return None
|
||||
return sum((baseline[name] - current[name]) / baseline[name]
|
||||
for name in baseline) / len(baseline) * 100
|
||||
|
||||
|
||||
def quality_grade(path):
|
||||
checks = json.loads(pathlib.Path(path).read_text())
|
||||
if checks != {"format": True, "clippy_warnings_denied": True}:
|
||||
raise ValueError("format and strict Clippy must pass before publishing quality")
|
||||
return "A"
|
||||
|
||||
|
||||
def compact(number):
|
||||
@@ -51,19 +99,17 @@ def main():
|
||||
parser.add_argument("--tests-coverage", required=True)
|
||||
parser.add_argument("--bench-output", required=True)
|
||||
parser.add_argument("--bench-coverage", required=True)
|
||||
parser.add_argument("--quality-gate", required=True)
|
||||
parser.add_argument("--bench-baseline", default="badges/bench-baseline.json")
|
||||
args = parser.parse_args()
|
||||
test_output = pathlib.Path(args.tests_output).read_text()
|
||||
tests = sum(map(int, re.findall(r"test result: ok\. (\d+) passed", test_output)))
|
||||
if tests < 1:
|
||||
raise SystemExit("no passing tests parsed")
|
||||
current = bench_results(args.bench_output)
|
||||
if not current or any(value <= 0 for value in current.values()):
|
||||
raise SystemExit("microbenchmark result missing or zero")
|
||||
base_path = pathlib.Path("badges/bench-baseline.json")
|
||||
base_path = pathlib.Path(args.bench_baseline)
|
||||
baseline = json.loads(base_path.read_text()) if base_path.exists() else {}
|
||||
common = [name for name in current if name in baseline and baseline[name] > 0]
|
||||
delta = (sum((baseline[name] - current[name]) / baseline[name] for name in common)
|
||||
/ len(common) * 100) if common else None
|
||||
delta = benchmark_delta(current, baseline)
|
||||
line_coverage = coverage(args.tests_coverage)
|
||||
bench_coverage = coverage(args.bench_coverage)
|
||||
loc = sum(len(path.read_text().splitlines()) for parent in (pathlib.Path("src"), pathlib.Path("ci"))
|
||||
@@ -73,8 +119,11 @@ def main():
|
||||
metrics = {"tests": tests, "line_coverage_pct": round(line_coverage, 1),
|
||||
"bench_coverage_pct": round(bench_coverage, 1),
|
||||
"bench_ns_per_op": current, "bench_improvement_pct": delta,
|
||||
"bench_host": benchmark_host(),
|
||||
"lines_of_code": loc, "dependencies": dependencies,
|
||||
"code_quality": "A", "license": "MIT"}
|
||||
"code_quality": quality_grade(args.quality_gate),
|
||||
"quality_scope": "main CI passed cargo fmt and warnings-denied Clippy on all targets",
|
||||
"license": "MIT"}
|
||||
pathlib.Path("badges").mkdir(exist_ok=True)
|
||||
pathlib.Path("badges/metrics.json").write_text(json.dumps(metrics, indent=2) + "\n")
|
||||
badges = {
|
||||
@@ -84,7 +133,9 @@ def main():
|
||||
"bench": ("bench", "baseline set" if delta is None else f"{delta:+.1f}% vs prev", "#007ec6"),
|
||||
"loc": ("lines of code", compact(loc), "#007ec6"),
|
||||
"dependencies": ("dependencies", str(dependencies), "#007ec6"),
|
||||
"quality": ("code quality", "A", "#4c1"),
|
||||
"quality": ("code quality", metrics["code_quality"],
|
||||
{"A": "#4c1", "B": "#97ca00", "C": "#dfb317",
|
||||
"D": "#fe7d37", "F": "#e05d44"}[metrics["code_quality"]]),
|
||||
"license": ("license", "MIT", "#007ec6"),
|
||||
}
|
||||
for name, (label, value, color) in badges.items():
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish a measured benchmark page to the public, data-only Pages repo."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shlex
|
||||
import subprocess
|
||||
import tempfile
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
PAGES_REPO = "ssh://git@gitea.bluefla.re:2222/blueflare/alkane-benchmarks.git"
|
||||
GUARD_REPO = "https://gitea.bluefla.re/blueflare/alkane-benchmarks.git"
|
||||
GUARD_COMMIT = "ea160545c49bc419a245692deda8dd7db7a611eb"
|
||||
PAGES_HOST = "gitea.bluefla.re"
|
||||
PAGES_HOST_FINGERPRINT = "SHA256:lqB5cmC9E5HNeg/lkzdOwQ++I8A70Wx7lnNs6liN24g"
|
||||
PAGES_HOST_KEY = "[gitea.bluefla.re]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO1II6NkmRW+QAd+Wj+7qbNrA8Oo+/N/WRog4hELskn/"
|
||||
|
||||
|
||||
def run(*args: str, env: dict[str, str] | None = None,
|
||||
cwd: Path | None = None) -> subprocess.CompletedProcess[str]:
|
||||
result = subprocess.run(args, env=env, cwd=cwd, text=True,
|
||||
capture_output=True, check=False)
|
||||
if result.returncode:
|
||||
detail = result.stderr.strip()[:2000]
|
||||
raise RuntimeError(f"{args[0]} {args[1]} exited {result.returncode}: {detail}")
|
||||
return result
|
||||
|
||||
|
||||
def current_main_sha() -> str:
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
request = Request(f"{api}/repos/{repo}/branches/main",
|
||||
headers={"Authorization": f"token {token}"})
|
||||
with urlopen(request, timeout=20) as response:
|
||||
branch = json.load(response)
|
||||
return branch["commit"]["id"]
|
||||
|
||||
|
||||
def publish(site: str) -> None:
|
||||
if os.environ.get("GITHUB_REF") != "refs/heads/main":
|
||||
raise ValueError("benchmark Pages publication requires main")
|
||||
sha = os.environ["GITHUB_SHA"]
|
||||
if current_main_sha() != sha:
|
||||
print("Skipping benchmark page from a superseded main commit")
|
||||
return
|
||||
page = Path("bench-site/index.html").read_bytes()
|
||||
if not page.startswith(b"<!doctype html>") or len(page) > 1_048_576:
|
||||
raise ValueError("invalid benchmark page")
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-pages-") as directory:
|
||||
temporary = Path(directory)
|
||||
guard_checkout = temporary / "guard"
|
||||
run("git", "clone", "--quiet", "--single-branch", "--branch", "next",
|
||||
GUARD_REPO, str(guard_checkout))
|
||||
guard_source = run("git", "show", f"{GUARD_COMMIT}:ci/validate_site.py",
|
||||
cwd=guard_checkout).stdout
|
||||
guard_script = temporary / "validate_site.py"
|
||||
guard_script.write_text(guard_source)
|
||||
run("python3", str(guard_script), "--page", site,
|
||||
str(Path("bench-site/index.html").resolve()))
|
||||
key = os.environ["BENCH_PAGES_DEPLOY_KEY"]
|
||||
if not key.startswith("-----BEGIN OPENSSH PRIVATE KEY-----"):
|
||||
raise ValueError("missing benchmark Pages deploy key")
|
||||
key_path = temporary / "deploy_key"
|
||||
key_path.write_text(key.rstrip("\n") + "\n")
|
||||
key_path.chmod(0o600)
|
||||
known_hosts = temporary / "known_hosts"
|
||||
known_hosts.write_text(PAGES_HOST_KEY + "\n")
|
||||
fingerprint = run("ssh-keygen", "-lf", str(known_hosts)).stdout.split()[1]
|
||||
if fingerprint != PAGES_HOST_FINGERPRINT:
|
||||
raise ValueError("benchmark Pages SSH host key changed")
|
||||
git_env = os.environ.copy()
|
||||
ssh_command = (f"ssh -i {shlex.quote(str(key_path))} -o IdentitiesOnly=yes "
|
||||
f"-o UserKnownHostsFile={shlex.quote(str(known_hosts))} "
|
||||
"-o StrictHostKeyChecking=yes -o ConnectTimeout=10")
|
||||
git_env["GIT_SSH_COMMAND"] = ssh_command
|
||||
git_env["GIT_TERMINAL_PROMPT"] = "0"
|
||||
checkout = temporary / "site"
|
||||
run("git", "clone", "--quiet", "--single-branch", "--branch", "gh-pages",
|
||||
PAGES_REPO, str(checkout), env=git_env)
|
||||
run("git", "config", "user.name", "Alkane CI", cwd=checkout)
|
||||
run("git", "config", "user.email", "ci@bluefla.re", cwd=checkout)
|
||||
for attempt in range(3):
|
||||
if current_main_sha() != sha:
|
||||
print("Skipping benchmark page from a superseded main commit")
|
||||
return
|
||||
run("git", "fetch", "--quiet", "origin", "gh-pages", env=git_env,
|
||||
cwd=checkout)
|
||||
run("git", "checkout", "-q", "-B", "bench-publish", "origin/gh-pages",
|
||||
cwd=checkout)
|
||||
destination = checkout / site / "index.html"
|
||||
destination.parent.mkdir(exist_ok=True)
|
||||
destination.write_bytes(page)
|
||||
run("git", "add", f"{site}/index.html", cwd=checkout)
|
||||
changed = subprocess.run(("git", "diff", "--cached", "--quiet"),
|
||||
cwd=checkout, check=False)
|
||||
if changed.returncode == 0:
|
||||
print(f"{site} benchmark page is already current")
|
||||
return
|
||||
if changed.returncode != 1:
|
||||
raise RuntimeError("cannot compare benchmark page")
|
||||
run("git", "commit", "-q", "-m",
|
||||
f"Update {site} benchmarks for {sha[:10]} [skip ci]", cwd=checkout)
|
||||
pushed = subprocess.run(("git", "push", "--quiet", "origin",
|
||||
"HEAD:gh-pages"), env=git_env, cwd=checkout,
|
||||
text=True, capture_output=True, check=False)
|
||||
if pushed.returncode == 0:
|
||||
print(f"Published {site} benchmark page for {sha[:10]}")
|
||||
return
|
||||
if attempt == 2:
|
||||
raise RuntimeError(f"benchmark Pages push failed: {pushed.stderr.strip()}")
|
||||
raise RuntimeError("benchmark Pages publication did not complete")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("site", choices=("sdk", "os", "toolkit"))
|
||||
publish(parser.parse_args().site)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish measured main-branch metric badges to the private badge branch."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from badge_store import publish_file
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
NAMES = ("tests", "coverage", "bench", "bench-coverage", "loc",
|
||||
"dependencies", "quality", "license")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
api = os.environ["GITHUB_API_URL"].rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
token = os.environ["GITEA_TOKEN"]
|
||||
for name in NAMES:
|
||||
data = (ROOT / "badges" / f"{name}.svg").read_bytes()
|
||||
if not data.lstrip().startswith(b"<svg"):
|
||||
raise ValueError(f"invalid {name} SVG")
|
||||
publish_file(api, repo, f"badges/{name}.svg", data, token,
|
||||
f"Update measured {name} badge")
|
||||
data = (ROOT / "badges/metrics.json").read_bytes()
|
||||
publish_file(api, repo, "badges/metrics.json", data, token,
|
||||
"Update measured badge data")
|
||||
print(f"Published {len(NAMES)} measured badge SVGs and metrics.json")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+138
-33
@@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Create one Gitea release and attach files using the runner's scoped token."""
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Publish a complete Gitea release after all assets are attached to a draft."""
|
||||
|
||||
import json
|
||||
import hashlib
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
@@ -10,45 +12,148 @@ import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
|
||||
def request(url, payload, content_type):
|
||||
headers = {
|
||||
"Authorization": "token " + os.environ["GITEA_TOKEN"],
|
||||
"Content-Type": content_type,
|
||||
def request(url, method="GET", payload=None, content_type=None):
|
||||
headers = {"Authorization": "token " + os.environ["GITEA_TOKEN"]}
|
||||
if content_type:
|
||||
headers["Content-Type"] = content_type
|
||||
operation = urllib.request.Request(url, data=payload, headers=headers, method=method)
|
||||
with urllib.request.urlopen(operation, timeout=60) as response:
|
||||
body = response.read()
|
||||
return json.loads(body) if body else None
|
||||
|
||||
|
||||
def json_request(url, method, value):
|
||||
return request(url, method, json.dumps(value).encode(), "application/json")
|
||||
|
||||
|
||||
def files_to_publish(names):
|
||||
paths = [pathlib.Path(name) for name in names]
|
||||
if not paths or len({path.name for path in paths}) != len(paths):
|
||||
raise ValueError("release assets must have distinct basenames")
|
||||
for path in paths:
|
||||
if not path.is_file() or path.stat().st_size == 0:
|
||||
raise ValueError(f"release asset missing or empty: {path}")
|
||||
return paths
|
||||
|
||||
|
||||
def sha256_file(path):
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for chunk in iter(lambda: source.read(1_048_576), b""):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
||||
def redirect_request(self, request, fp, code, message, headers, new_url):
|
||||
return None
|
||||
|
||||
|
||||
def asset_digest(asset, api):
|
||||
url = asset["browser_download_url"]
|
||||
remote = urllib.parse.urlsplit(url)
|
||||
expected = urllib.parse.urlsplit(api)
|
||||
if remote.scheme != "https" or remote.netloc != expected.netloc:
|
||||
raise ValueError("asset download is outside the trusted Gitea host")
|
||||
operation = urllib.request.Request(
|
||||
url, headers={"Authorization": "token " + os.environ["GITEA_TOKEN"]})
|
||||
digest = hashlib.sha256()
|
||||
with urllib.request.build_opener(NoRedirect).open(operation, timeout=60) as response:
|
||||
for chunk in iter(lambda: response.read(1_048_576), b""):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def get_release(url):
|
||||
try:
|
||||
return request(url)
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code == 404:
|
||||
return None
|
||||
raise
|
||||
|
||||
|
||||
def check_tag_commit(api, repo, tag, sha):
|
||||
url = f"{api}/repos/{repo}/tags/{urllib.parse.quote(tag, safe='')}"
|
||||
try:
|
||||
remote_tag = request(url)
|
||||
except urllib.error.HTTPError as error:
|
||||
if error.code == 404:
|
||||
return False
|
||||
raise
|
||||
if not isinstance(remote_tag, dict) or remote_tag.get("commit", {}).get("sha") != sha:
|
||||
raise ValueError("Git tag points to a different or unknown commit")
|
||||
return True
|
||||
|
||||
|
||||
def publish(tag, prerelease, paths, api, repo, sha):
|
||||
base = f"{api}/repos/{repo}/releases"
|
||||
tag_url = f"{base}/tags/{urllib.parse.quote(tag, safe='')}"
|
||||
metadata = {
|
||||
"tag_name": tag,
|
||||
"target_commitish": sha,
|
||||
"name": tag,
|
||||
"body": f"Automated Alkane OS build from {sha}. Conformance results are in the repository.",
|
||||
"prerelease": prerelease,
|
||||
}
|
||||
req = urllib.request.Request(url, data=payload, headers=headers, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=60) as response:
|
||||
return json.load(response)
|
||||
check_tag_commit(api, repo, tag, sha)
|
||||
release = get_release(tag_url)
|
||||
if release is None:
|
||||
release = json_request(base, "POST", {**metadata, "draft": True})
|
||||
elif release.get("target_commitish") != sha:
|
||||
raise ValueError("release tag already belongs to another source revision")
|
||||
release_url = f"{base}/{release['id']}"
|
||||
assets_url = f"{release_url}/assets"
|
||||
existing = request(assets_url)
|
||||
wanted = {path.name: path.stat().st_size for path in paths}
|
||||
digests = {path.name: sha256_file(path) for path in paths}
|
||||
present = {asset["name"]: asset for asset in existing}
|
||||
if not release.get("draft") and all(
|
||||
(asset := present.get(name)) is not None and asset["size"] == size
|
||||
and asset_digest(asset, api) == digests[name]
|
||||
for name, size in wanted.items()
|
||||
):
|
||||
if not check_tag_commit(api, repo, tag, sha):
|
||||
raise ValueError("published release has no Git tag")
|
||||
print(f"Release {tag} is already complete")
|
||||
return
|
||||
if not release.get("draft"):
|
||||
release = json_request(release_url, "PATCH", {**metadata, "draft": True})
|
||||
if not release.get("draft"):
|
||||
raise ValueError("incomplete release could not be returned to draft")
|
||||
for asset in existing:
|
||||
if asset["name"] in wanted:
|
||||
request(f"{assets_url}/{asset['id']}", "DELETE")
|
||||
for path in paths:
|
||||
query = urllib.parse.urlencode({"name": path.name})
|
||||
request(f"{assets_url}?{query}", "POST", path.read_bytes(),
|
||||
"application/octet-stream")
|
||||
uploaded = request(assets_url)
|
||||
actual = {asset["name"]: asset for asset in uploaded}
|
||||
if any((asset := actual.get(name)) is None or asset["size"] != size
|
||||
or asset_digest(asset, api) != digests[name]
|
||||
for name, size in wanted.items()):
|
||||
raise ValueError("draft release asset verification failed")
|
||||
check_tag_commit(api, repo, tag, sha)
|
||||
release = json_request(release_url, "PATCH", {**metadata, "draft": False})
|
||||
if release.get("draft"):
|
||||
raise ValueError("release remained a draft after publishing")
|
||||
if not check_tag_commit(api, repo, tag, sha):
|
||||
raise ValueError("published release did not create the expected Git tag")
|
||||
print(f"Published {tag} with {len(paths)} assets")
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 4:
|
||||
sys.exit("usage: release.py TAG PRERELEASE FILE [FILE ...]")
|
||||
tag, prerelease, *files = sys.argv[1:]
|
||||
api = os.environ.get("GITHUB_API_URL", "https://gitea.bluefla.re/api/v1").rstrip("/")
|
||||
repo = os.environ["GITHUB_REPOSITORY"]
|
||||
sha = os.environ["GITHUB_SHA"]
|
||||
body = f"Automated Alkane OS build from {sha}. Conformance results are in the repository."
|
||||
metadata = {
|
||||
"tag_name": tag,
|
||||
"target_commitish": sha,
|
||||
"name": tag,
|
||||
"body": body,
|
||||
"prerelease": prerelease.lower() == "true",
|
||||
}
|
||||
tag, prerelease, *names = sys.argv[1:]
|
||||
try:
|
||||
release = request(f"{api}/repos/{repo}/releases", json.dumps(metadata).encode(), "application/json")
|
||||
except urllib.error.HTTPError as exc:
|
||||
sys.exit(f"release creation failed: HTTP {exc.code}")
|
||||
release_id = release["id"]
|
||||
for name in files:
|
||||
path = pathlib.Path(name)
|
||||
query = urllib.parse.urlencode({"name": path.name})
|
||||
try:
|
||||
request(f"{api}/repos/{repo}/releases/{release_id}/assets?{query}",
|
||||
path.read_bytes(), "application/octet-stream")
|
||||
except urllib.error.HTTPError as exc:
|
||||
sys.exit(f"asset upload failed for {path.name}: HTTP {exc.code}")
|
||||
print(f"Created {tag} with {len(files)} assets")
|
||||
paths = files_to_publish(names)
|
||||
api = os.environ.get("GITHUB_API_URL", "https://gitea.bluefla.re/api/v1").rstrip("/")
|
||||
publish(tag, prerelease.lower() == "true", paths, api,
|
||||
os.environ["GITHUB_REPOSITORY"], os.environ["GITHUB_SHA"])
|
||||
except (ValueError, OSError, urllib.error.HTTPError) as error:
|
||||
sys.exit(f"release publication failed: {error}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Exercise benchmark history selection and safe site rendering."""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
from bench_history import previous_measurement, render_html, updated_index
|
||||
|
||||
|
||||
class HistoryTests(unittest.TestCase):
|
||||
def test_retry_uses_previous_commit(self):
|
||||
first = "a" * 40
|
||||
second = "b" * 40
|
||||
index = {"schema": 1, "entries": [
|
||||
{"sha": first, "timestamp": "2026-01-01T00:00:00+00:00",
|
||||
"metrics": {"bench_ns_per_op": {"hash": 10.0}}},
|
||||
{"sha": second, "timestamp": "2026-01-02T00:00:00+00:00",
|
||||
"metrics": {"bench_ns_per_op": {"hash": 8.0}}}]}
|
||||
self.assertEqual(previous_measurement(index, second,
|
||||
lambda ancestor, sha: 1 if ancestor == first else None),
|
||||
{"hash": 10.0})
|
||||
updated = updated_index(index, second, {"bench_ns_per_op": {"hash": 7.5}},
|
||||
"2026-01-03T00:00:00+00:00")
|
||||
self.assertEqual(len(updated["entries"]), 2)
|
||||
self.assertEqual(updated["entries"][-1]["metrics"]["bench_ns_per_op"]["hash"], 7.5)
|
||||
|
||||
def test_invalid_measurement_and_sha_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
updated_index({}, "bad", {"bench_ns_per_op": {"hash": 1}}, "now")
|
||||
with self.assertRaises(ValueError):
|
||||
updated_index({}, "a" * 40, {"bench_ns_per_op": {"hash": -1}}, "now")
|
||||
|
||||
def test_baseline_must_match_cpu_class(self):
|
||||
first = "a" * 40
|
||||
host = {"architecture": "x86_64", "cpu_model": "A", "logical_cpus": 8}
|
||||
other = {"architecture": "x86_64", "cpu_model": "B", "logical_cpus": 8}
|
||||
index = {"entries": [{"sha": first, "metrics": {
|
||||
"bench_ns_per_op": {"hash": 10.0}, "bench_host": host}}]}
|
||||
distance = lambda _ancestor, _sha: 1
|
||||
self.assertEqual(previous_measurement(index, "b" * 40, distance, host),
|
||||
{"hash": 10.0})
|
||||
self.assertEqual(previous_measurement(index, "b" * 40, distance, other), {})
|
||||
|
||||
def test_site_escapes_history_content(self):
|
||||
entry = {"sha": "a" * 40, "timestamp": "<script>alert(1)</script>",
|
||||
"metrics": {"bench_ns_per_op": {"hash": 10.0},
|
||||
"bench_host": {"cpu_model": "<script>cpu</script>"}}}
|
||||
page = render_html({"entries": [entry]})
|
||||
self.assertIn("<script>", page)
|
||||
self.assertNotIn("<script>", page)
|
||||
self.assertIn("10.000", page)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Verify cleanup removes only generated files inside the CI checkout."""
|
||||
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location(
|
||||
"cleanup_generated", Path(__file__).resolve().with_name("cleanup-generated.py"))
|
||||
CLEANUP = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(CLEANUP)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
with tempfile.TemporaryDirectory(prefix="alkane-os-cleanup-") as temporary:
|
||||
root = Path(temporary)
|
||||
outside = root.parent / (root.name + "-outside")
|
||||
outside.mkdir()
|
||||
(outside / "keep").write_bytes(b"safe")
|
||||
previous = CLEANUP.ROOT
|
||||
CLEANUP.ROOT = root
|
||||
try:
|
||||
with patch.dict(os.environ, {"GITHUB_RUN_ID": "123"}):
|
||||
(root / "target").mkdir()
|
||||
(root / "target/build").write_bytes(b"generated")
|
||||
(root / "bench-current.txt").write_bytes(b"generated")
|
||||
CLEANUP.cleanup()
|
||||
assert not (root / "target").exists()
|
||||
assert not (root / "bench-current.txt").exists()
|
||||
(root / "target").symlink_to(outside, target_is_directory=True)
|
||||
try:
|
||||
CLEANUP.cleanup()
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("cleanup followed a symlink")
|
||||
assert (outside / "keep").read_bytes() == b"safe"
|
||||
finally:
|
||||
CLEANUP.ROOT = previous
|
||||
(outside / "keep").unlink()
|
||||
outside.rmdir()
|
||||
print("Alkane OS CI cleanup containment: PASS")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Regression checks for the fail-closed release evidence gate."""
|
||||
|
||||
import copy
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
import unittest
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).with_name("check-conformance.py")
|
||||
spec = importlib.util.spec_from_file_location("check_conformance", SCRIPT)
|
||||
gate = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(gate)
|
||||
MANIFEST = json.loads(Path(__file__).with_name("conformance.json").read_text())
|
||||
TARGETS = {(row["board"], row["chip"]): gate.REQUIRED_ALGORITHMS[row["chip"]]
|
||||
for row in MANIFEST["targets"]}
|
||||
|
||||
|
||||
class ConformanceGateTest(unittest.TestCase):
|
||||
def test_placeholder_matrix_cannot_pass_without_current_run_artifacts(self):
|
||||
manifest = copy.deepcopy(MANIFEST)
|
||||
for row in manifest["targets"]:
|
||||
row.update(sdk_image_sha256="0" * 64,
|
||||
stock_image_sha256="0" * 64,
|
||||
simulator_trace_sha256="0" * 64,
|
||||
stock_result="mines",
|
||||
alkane_share_accept_capture="does-not-exist.json",
|
||||
bench_validation="not-run")
|
||||
with TemporaryDirectory() as directory:
|
||||
errors = gate.validate(manifest, TARGETS, Path(directory), "123",
|
||||
"a" * 40, "b" * 64)
|
||||
self.assertEqual(len(errors), 64)
|
||||
self.assertTrue(all("evidence invalid" in error for error in errors))
|
||||
|
||||
def test_stale_source_identity_is_rejected(self):
|
||||
row = copy.deepcopy(MANIFEST["targets"][0])
|
||||
row["stock_result"] = "mines"
|
||||
with TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
report = root / "123" / row["board"] / f"{row['chip']}.json"
|
||||
report.parent.mkdir(parents=True)
|
||||
report.write_text(json.dumps({"schema": 1, "run_id": "123",
|
||||
"source_commit": "f" * 40,
|
||||
"os_binary_sha256": "b" * 64,
|
||||
"board": row["board"], "chip": row["chip"],
|
||||
"stock_firmware_modified": False}))
|
||||
with self.assertRaisesRegex(ValueError, "different run, source"):
|
||||
gate.verify_cell(row, root, "123", "a" * 40, "b" * 64,
|
||||
TARGETS[(row["board"], row["chip"])])
|
||||
|
||||
def test_share_accept_requires_real_difficulty_one_work(self):
|
||||
capture = {"nonce_source": "real_silicon_capture",
|
||||
"header80": "00" * 80,
|
||||
"pool_target_le": "ff" * 32, "nonce": 0,
|
||||
"submission": {"id": 4, "method": "mining.submit",
|
||||
"params": ["worker", "job", "", "00000000", "00000000"]},
|
||||
"pool_response": {"id": 4, "result": True, "error": None}}
|
||||
with self.assertRaisesRegex(ValueError, "below difficulty one"):
|
||||
gate.verify_accept(capture, "Sha256")
|
||||
capture["submission"]["params"][4] = "00000001"
|
||||
with self.assertRaisesRegex(ValueError, "submitted nonce"):
|
||||
gate.verify_accept(capture, "Sha256")
|
||||
|
||||
def test_scrypt_proof_matches_litecoin_and_independent_vectors(self):
|
||||
header = bytes.fromhex(
|
||||
"01000000f615f7ce3b4fc6b8f61e8f89aedb1d0852507650533a9e3b10b9bbcc30639f27"
|
||||
"9fcaa86746e1ef52d3edb3c4ad8259920d509bd073605c9bf1d59983752a6b06b817bb4"
|
||||
"ea78e011d012d59d4"
|
||||
)
|
||||
self.assertEqual(len(header), 80)
|
||||
self.assertEqual(
|
||||
gate.proof_digest(header, "Scrypt")[::-1].hex(),
|
||||
"0000000110c8357966576df46f3b802ca897deb7ad18b12f1c24ecff6386ebd9",
|
||||
)
|
||||
self.assertEqual(
|
||||
gate.proof_digest(bytes(range(80)), "Scrypt").hex(),
|
||||
"bc540a1a801df96e493005c71e010e2d387607fbf0fec416fd3c2645aa1ba9d2",
|
||||
)
|
||||
|
||||
def test_equihash_cells_still_need_an_algorithm_specific_verifier(self):
|
||||
with self.assertRaisesRegex(ValueError, "Equihash share verifier"):
|
||||
gate.verify_accept({}, "Equihash")
|
||||
|
||||
def test_submitted_header_reconstruction_rejects_version_and_extranonce_changes(self):
|
||||
capture = {
|
||||
"subscription": {"extranonce1": "aa", "extranonce2_size": 1},
|
||||
"notify": {"method": "mining.notify", "params": [
|
||||
"job", "11" * 32, "01", "02", [],
|
||||
"20000000", "1d00ffff", "12345678", True]},
|
||||
"configure_response": {"result": {
|
||||
"version-rolling": True, "version-rolling.mask": "1fffe000"}},
|
||||
}
|
||||
params = ["worker", "job", "bb", "12345678", "00000001", "00002000"]
|
||||
header, _ = gate.reconstructed_header(capture, params)
|
||||
self.assertEqual(len(header), 80)
|
||||
self.assertEqual(header[:4], (0x20002000).to_bytes(4, "little"))
|
||||
self.assertEqual(header[4:8], b"\x11" * 4)
|
||||
changed = params.copy()
|
||||
changed[5] = "ffffffff"
|
||||
with self.assertRaisesRegex(ValueError, "version bits"):
|
||||
gate.reconstructed_header(capture, changed)
|
||||
changed[5] = params[5]
|
||||
changed[2] = "f"
|
||||
with self.assertRaisesRegex(ValueError, "Stratum hex"):
|
||||
gate.reconstructed_header(capture, changed)
|
||||
changed[2] = "cc"
|
||||
rebuilt, _ = gate.reconstructed_header(capture, changed)
|
||||
self.assertNotEqual(rebuilt, header)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Check that queued CI runs do not displace the last completed result."""
|
||||
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from badge_store import publish_file
|
||||
|
||||
path = Path(__file__).with_name("last-attempt-badge.py")
|
||||
spec = importlib.util.spec_from_file_location("last_attempt_badge", path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
|
||||
def runs(_url, _token):
|
||||
return {"workflow_runs": [
|
||||
{"id": 105, "head_branch": "main", "status": "completed", "conclusion": "cancelled"},
|
||||
{"id": 104, "head_branch": "main", "status": "queued", "conclusion": None},
|
||||
{"id": 103, "head_branch": "main", "status": "completed", "conclusion": "failure"},
|
||||
{"id": 102, "head_branch": "main", "status": "completed", "conclusion": "success"},
|
||||
]}
|
||||
|
||||
|
||||
with patch.object(module, "request_json", runs):
|
||||
assert module.latest_completed("https://example.invalid/api/v1", "owner/repo",
|
||||
"main.yml", "main", "token") == (103, "failure")
|
||||
|
||||
svg = module.badge("main", "failure", 103)
|
||||
root = ET.fromstring(svg)
|
||||
assert root.attrib["aria-label"] == "CI main: last completed attempt fail (run 103)"
|
||||
assert b">fail</text>" in svg
|
||||
assert b"#d1242f" in svg
|
||||
assert b">pass</text>" in module.badge("next", "success", 108)
|
||||
environment = {
|
||||
"GITHUB_EVENT_NAME": "workflow_run",
|
||||
"GITHUB_REF": "refs/heads/main",
|
||||
"GITHUB_REPOSITORY": "owner/repo",
|
||||
"GITHUB_API_URL": "https://example.invalid/api/v1",
|
||||
"GITEA_TOKEN": "test-token",
|
||||
}
|
||||
with patch.dict(os.environ, environment), \
|
||||
patch.object(module, "latest_completed", side_effect=[(201, "success"),
|
||||
(202, "failure")]) as latest, \
|
||||
patch.object(module, "publish") as published:
|
||||
module.main()
|
||||
assert latest.call_count == 2
|
||||
assert [call.args[2] for call in published.call_args_list] == ["main", "next"]
|
||||
with patch.dict(os.environ, {**environment, "GITHUB_EVENT_NAME": "workflow_dispatch"}), \
|
||||
patch.object(module, "latest_completed", side_effect=[(201, "success"),
|
||||
(202, "failure")]), \
|
||||
patch.object(module, "publish") as published:
|
||||
module.main()
|
||||
assert published.call_count == 2
|
||||
with patch.dict(os.environ, {**environment, "GITHUB_EVENT_NAME": "schedule"}):
|
||||
try:
|
||||
module.main()
|
||||
except RuntimeError as error:
|
||||
assert "CI completion or manual dispatch" in str(error)
|
||||
else:
|
||||
raise AssertionError("scheduled event entered badge publisher")
|
||||
for invalid in ("", "/main.svg", "../main.svg", "badges//main.svg"):
|
||||
try:
|
||||
publish_file("https://example.invalid/api/v1", "owner/repo", invalid,
|
||||
b"<svg/>", "token", "test")
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError(f"unsafe badge path accepted: {invalid}")
|
||||
print("last completed CI badge: queued run ignored, pass/fail SVGs valid")
|
||||
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Regression checks for measured benchmark and quality badges."""
|
||||
|
||||
import sys
|
||||
import json
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
from metrics import EXPECTED_BENCHES, bench_results, benchmark_delta, compact, quality_grade
|
||||
|
||||
|
||||
class MetricTests(unittest.TestCase):
|
||||
def test_quality_requires_both_static_checks(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "quality.json"
|
||||
path.write_text(json.dumps({"format": True, "clippy_warnings_denied": True}))
|
||||
self.assertEqual(quality_grade(path), "A")
|
||||
path.write_text(json.dumps({"format": True, "clippy_warnings_denied": False}))
|
||||
with self.assertRaises(ValueError):
|
||||
quality_grade(path)
|
||||
|
||||
def test_benchmark_parser_and_short_si_badge(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "bench.txt"
|
||||
path.write_text("test bench_sha ... BENCH sha 42.125 ns/op\n")
|
||||
self.assertEqual(bench_results(path), {"sha": 42.125})
|
||||
path.write_text("BENCH sha 42.125 ns/op\nBENCH sha 40.000 ns/op\n")
|
||||
with self.assertRaisesRegex(ValueError, "duplicate"):
|
||||
bench_results(path)
|
||||
path.write_text("BENCH sha 42.125 ns/op\nBENCH broken nan ns/op\n")
|
||||
with self.assertRaisesRegex(ValueError, "malformed"):
|
||||
bench_results(path)
|
||||
self.assertEqual(compact(1234567), "1.23M")
|
||||
|
||||
def test_benchmark_suite_and_baseline_must_be_complete(self):
|
||||
current = {name: 10.0 for name in EXPECTED_BENCHES}
|
||||
self.assertIsNone(benchmark_delta(current, {}))
|
||||
self.assertEqual(benchmark_delta(current, {"target_matrix": 20.0}), 50.0)
|
||||
with self.assertRaisesRegex(ValueError, "declared suite"):
|
||||
benchmark_delta({"target_matrix": 10.0}, {})
|
||||
with self.assertRaisesRegex(ValueError, "retired"):
|
||||
benchmark_delta(current, {"removed_bench": 20.0})
|
||||
with self.assertRaisesRegex(ValueError, "invalid measurements"):
|
||||
benchmark_delta(current, {"target_matrix": float("inf")})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,133 @@
|
||||
#!/usr/bin/env python3
|
||||
# SPDX-License-Identifier: MIT
|
||||
"""Check draft release recovery without making network requests."""
|
||||
|
||||
import importlib.util
|
||||
import hashlib
|
||||
import urllib.error
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).with_name("release.py")
|
||||
spec = importlib.util.spec_from_file_location("release", SCRIPT)
|
||||
release = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(release)
|
||||
|
||||
|
||||
class FakeGitea:
|
||||
def __init__(self):
|
||||
self.release = None
|
||||
self.assets = []
|
||||
self.blobs = {}
|
||||
self.fail_second_upload = True
|
||||
self.uploads = 0
|
||||
self.tag_sha = None
|
||||
|
||||
def request(self, url, method="GET", payload=None, content_type=None):
|
||||
if "/tags/" in url and "/releases/" not in url:
|
||||
if self.tag_sha is None:
|
||||
raise urllib.error.HTTPError(url, 404, "missing", {}, None)
|
||||
return {"commit": {"sha": self.tag_sha}}
|
||||
if "/assets" in url:
|
||||
if method == "GET":
|
||||
return list(self.assets)
|
||||
if method == "DELETE":
|
||||
asset_id = int(url.rsplit("/", 1)[1])
|
||||
self.assets = [item for item in self.assets if item["id"] != asset_id]
|
||||
self.blobs.pop(asset_id, None)
|
||||
return None
|
||||
self.uploads += 1
|
||||
if self.fail_second_upload and self.uploads == 2:
|
||||
raise OSError("injected upload failure")
|
||||
name = release.urllib.parse.parse_qs(url.split("?", 1)[1])["name"][0]
|
||||
item = {"id": self.uploads, "name": name, "size": len(payload),
|
||||
"browser_download_url": f"https://api/download/{self.uploads}"}
|
||||
self.assets.append(item)
|
||||
self.blobs[self.uploads] = payload
|
||||
return item
|
||||
if method == "GET":
|
||||
return self.release
|
||||
if method == "POST":
|
||||
assert self.release is None
|
||||
self.release = {**release.json.loads(payload), "id": 7}
|
||||
return dict(self.release)
|
||||
if method == "PATCH":
|
||||
self.release.update(release.json.loads(payload))
|
||||
if not self.release["draft"] and self.tag_sha is None:
|
||||
self.tag_sha = self.release["target_commitish"]
|
||||
return dict(self.release)
|
||||
raise AssertionError((method, url))
|
||||
|
||||
|
||||
class ReleaseTest(unittest.TestCase):
|
||||
def test_retry_after_partial_upload_keeps_release_draft_until_complete(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
paths = [Path(directory) / "source.tar.gz", Path(directory) / "bench.txt"]
|
||||
for path in paths:
|
||||
path.write_bytes(path.name.encode())
|
||||
fake = FakeGitea()
|
||||
def digest(asset, _api):
|
||||
return hashlib.sha256(fake.blobs[asset["id"]]).hexdigest()
|
||||
|
||||
with patch.object(release, "request", fake.request), \
|
||||
patch.object(release, "asset_digest", digest):
|
||||
with self.assertRaisesRegex(OSError, "injected upload failure"):
|
||||
release.publish("v1.2.3", False, paths, "https://api", "org/repo", "a" * 40)
|
||||
self.assertTrue(fake.release["draft"])
|
||||
self.assertEqual(len(fake.assets), 1)
|
||||
fake.fail_second_upload = False
|
||||
release.publish("v1.2.3", False, paths, "https://api", "org/repo", "a" * 40)
|
||||
self.assertFalse(fake.release["draft"])
|
||||
self.assertEqual({item["name"] for item in fake.assets},
|
||||
{path.name for path in paths})
|
||||
fake.assets[0]["size"] = len(fake.blobs[fake.assets[0]["id"]])
|
||||
fake.blobs[fake.assets[0]["id"]] = b"x" * fake.assets[0]["size"]
|
||||
release.publish("v1.2.3", False, paths, "https://api", "org/repo", "a" * 40)
|
||||
self.assertFalse(fake.release["draft"])
|
||||
self.assertEqual(digest(fake.assets[0], "https://api"),
|
||||
release.sha256_file(paths[0]))
|
||||
|
||||
def test_preflight_rejects_missing_and_duplicate_assets(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "one"
|
||||
with self.assertRaisesRegex(ValueError, "missing or empty"):
|
||||
release.files_to_publish([str(path)])
|
||||
path.write_bytes(b"x")
|
||||
with self.assertRaisesRegex(ValueError, "distinct"):
|
||||
release.files_to_publish([str(path), str(path)])
|
||||
|
||||
def test_existing_git_tag_mismatch_blocks_release_without_changes(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "image.tar.gz"
|
||||
path.write_bytes(b"image")
|
||||
fake = FakeGitea()
|
||||
fake.tag_sha = "b" * 40
|
||||
fake.release = {"id": 7, "tag_name": "v1.2.3",
|
||||
"target_commitish": "a" * 40, "draft": False}
|
||||
with patch.object(release, "request", fake.request):
|
||||
with self.assertRaisesRegex(ValueError, "Git tag points"):
|
||||
release.publish("v1.2.3", False, [path], "https://api",
|
||||
"org/repo", "a" * 40)
|
||||
self.assertEqual(fake.uploads, 0)
|
||||
self.assertFalse(fake.release["draft"])
|
||||
|
||||
def test_complete_release_without_git_tag_is_not_accepted(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
path = Path(directory) / "image.tar.gz"
|
||||
path.write_bytes(b"image")
|
||||
fake = FakeGitea()
|
||||
fake.release = {"id": 7, "tag_name": "v1.2.3",
|
||||
"target_commitish": "a" * 40, "draft": False}
|
||||
fake.assets = [{"id": 1, "name": path.name, "size": path.stat().st_size}]
|
||||
with patch.object(release, "request", fake.request), \
|
||||
patch.object(release, "asset_digest", lambda *_: release.sha256_file(path)):
|
||||
with self.assertRaisesRegex(ValueError, "no Git tag"):
|
||||
release.publish("v1.2.3", False, [path], "https://api",
|
||||
"org/repo", "a" * 40)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,116 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Bounded host-only check: captured BM13xx report through HAL to a strict pool.
|
||||
|
||||
use alkane_os::hal_wire::HalClient;
|
||||
use alkane_os::miner::Bm1366Miner;
|
||||
use alkane_os::stratum_client::{PoolConnection, PoolEvent, ReportEvent};
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const CAPTURED_HEADER: [u8; 80] = [
|
||||
0x00, 0x00, 0x00, 0x20, 0xfc, 0x3a, 0xb8, 0xd4, 0x30, 0x21, 0xf4, 0x5e, 0xb6, 0x62, 0x38, 0x9d,
|
||||
0x0f, 0x79, 0x1f, 0x65, 0x7d, 0xbf, 0x75, 0xa5, 0xcb, 0x93, 0x67, 0x0b, 0x57, 0xb6, 0xc2, 0x25,
|
||||
0x03, 0xb4, 0x21, 0x3a, 0x43, 0xae, 0x10, 0x52, 0xeb, 0xd4, 0x44, 0x43, 0x06, 0x8f, 0xe1, 0x78,
|
||||
0xe2, 0xc6, 0x1c, 0xa4, 0x99, 0x48, 0x46, 0xfd, 0x13, 0x3a, 0xdd, 0xb0, 0x4a, 0xab, 0x2b, 0x38,
|
||||
0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01, 0x2e, 0x6a, 0x8f, 0x06, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
|
||||
const BM1362_HEADER_HEX: &str = concat!(
|
||||
"00000020fc3ab8d43021f45eb662389d0f791f657dbf75a5cb93670b57b6c22503",
|
||||
"b4213a0503969b7384e1215aace8cfc65dae22d8493ad48fec0b7ac3b0ad7b4e7",
|
||||
"fb0c5c0012e6a8f06021700000000"
|
||||
);
|
||||
|
||||
fn bm1362_header() -> Result<[u8; 80], io::Error> {
|
||||
let mut header = [0_u8; 80];
|
||||
for (index, pair) in BM1362_HEADER_HEX.as_bytes().chunks_exact(2).enumerate() {
|
||||
let digits = std::str::from_utf8(pair).map_err(io::Error::other)?;
|
||||
header[index] = u8::from_str_radix(digits, 16).map_err(io::Error::other)?;
|
||||
}
|
||||
Ok(header)
|
||||
}
|
||||
|
||||
fn failure(message: impl Into<String>) -> io::Error {
|
||||
io::Error::other(message.into())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let socket = args.next().ok_or_else(|| {
|
||||
failure("usage: simulator_pool_replay_probe HAL_SOCKET POOL_ADDRESS [SHARES] [CHIP]")
|
||||
})?;
|
||||
let address: SocketAddr = args
|
||||
.next()
|
||||
.ok_or_else(|| failure("missing pool address"))?
|
||||
.parse()?;
|
||||
let expected_accepts: usize = args.next().map_or(Ok(1), |value| value.parse())?;
|
||||
if !(1..=64).contains(&expected_accepts) {
|
||||
return Err(failure("SHARES must be between 1 and 64").into());
|
||||
}
|
||||
let chip = args.next().unwrap_or_else(|| "BM1366".to_owned());
|
||||
let first_header = match chip.as_str() {
|
||||
"BM1366" => CAPTURED_HEADER,
|
||||
"BM1362" if expected_accepts == 1 => bm1362_header()?,
|
||||
"BM1362" => return Err(failure("BM1362 has one captured header").into()),
|
||||
_ => return Err(failure("unsupported captured chip").into()),
|
||||
};
|
||||
if args.next().is_some() {
|
||||
return Err(failure("unexpected argument").into());
|
||||
}
|
||||
if !address.ip().is_loopback() {
|
||||
return Err(failure("probe pool must be on loopback").into());
|
||||
}
|
||||
|
||||
let mut backend = HalClient::connect(socket)?;
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000)
|
||||
.ok_or_else(|| failure("invalid simulator configuration"))?;
|
||||
let mut pool = PoolConnection::connect(
|
||||
address,
|
||||
"alkane-simulator-probe",
|
||||
"alkane.test",
|
||||
"x",
|
||||
Duration::from_secs(2),
|
||||
Duration::from_secs(5),
|
||||
)
|
||||
.map_err(|error| failure(format!("pool handshake: {error:?}")))?;
|
||||
miner.start()?;
|
||||
let deadline = Instant::now() + Duration::from_secs(10 + 2 * expected_accepts as u64);
|
||||
let mut issued = 0;
|
||||
let mut submitted = 0;
|
||||
let mut accepted = 0;
|
||||
while Instant::now() < deadline {
|
||||
let (event, reports) = pool
|
||||
.cycle(&mut miner)
|
||||
.map_err(|error| failure(format!("mining cycle: {error:?}")))?;
|
||||
submitted += reports
|
||||
.iter()
|
||||
.filter(|report| matches!(report, ReportEvent::Submitted(_)))
|
||||
.count();
|
||||
match event {
|
||||
PoolEvent::Work(work) => {
|
||||
if issued == 0 && work.header != first_header {
|
||||
return Err(failure("pool job differs from real-silicon capture").into());
|
||||
}
|
||||
issued += 1;
|
||||
}
|
||||
PoolEvent::ShareAccepted(_) => {
|
||||
if accepted >= submitted || accepted >= issued {
|
||||
return Err(failure("unattributed pool acceptance").into());
|
||||
}
|
||||
accepted += 1;
|
||||
if accepted == expected_accepts {
|
||||
miner.stop()?;
|
||||
println!(
|
||||
"{chip} real captured nonce accepted by strict loopback pool ({accepted} shares)"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
PoolEvent::ShareRejected(_) => return Err(failure("pool rejected share").into()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Err(failure("captured share was not accepted before deadline").into())
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Exercise the SDK's host-only BM13xx HAL simulator with a captured job.
|
||||
|
||||
use alkane_os::hal_wire::HalClient;
|
||||
use alkane_os::miner::{Bm1366Miner, ChipModel};
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
|
||||
const HEADER: [u8; 80] = [
|
||||
0x00, 0x00, 0x00, 0x20, 0xfc, 0x3a, 0xb8, 0xd4, 0x30, 0x21, 0xf4, 0x5e, 0xb6, 0x62, 0x38, 0x9d,
|
||||
0x0f, 0x79, 0x1f, 0x65, 0x7d, 0xbf, 0x75, 0xa5, 0xcb, 0x93, 0x67, 0x0b, 0x57, 0xb6, 0xc2, 0x25,
|
||||
0x03, 0xb4, 0x21, 0x3a, 0x43, 0xae, 0x10, 0x52, 0xeb, 0xd4, 0x44, 0x43, 0x06, 0x8f, 0xe1, 0x78,
|
||||
0xe2, 0xc6, 0x1c, 0xa4, 0x99, 0x48, 0x46, 0xfd, 0x13, 0x3a, 0xdd, 0xb0, 0x4a, 0xab, 0x2b, 0x38,
|
||||
0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01, 0x2e, 0x6a, 0x8f, 0x06, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
|
||||
const BM1362_HEADER_HEX: &str = concat!(
|
||||
"00000020fc3ab8d43021f45eb662389d0f791f657dbf75a5cb93670b57b6c22503",
|
||||
"b4213a0503969b7384e1215aace8cfc65dae22d8493ad48fec0b7ac3b0ad7b4e7",
|
||||
"fb0c5c0012e6a8f06021700000000"
|
||||
);
|
||||
|
||||
fn bm1362_header() -> Result<[u8; 80], io::Error> {
|
||||
let mut header = [0_u8; 80];
|
||||
for (index, pair) in BM1362_HEADER_HEX.as_bytes().chunks_exact(2).enumerate() {
|
||||
let digits = std::str::from_utf8(pair).map_err(io::Error::other)?;
|
||||
header[index] = u8::from_str_radix(digits, 16).map_err(io::Error::other)?;
|
||||
}
|
||||
Ok(header)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut args = std::env::args().skip(1);
|
||||
let path = args
|
||||
.next()
|
||||
.ok_or("usage: simulator_replay_probe SOCKET_PATH [BM1366|BM1362]")?;
|
||||
let chip = args.next().unwrap_or_else(|| "BM1366".to_owned());
|
||||
if args.next().is_some() {
|
||||
return Err("unexpected argument".into());
|
||||
}
|
||||
let (header, nonce, model) = match chip.as_str() {
|
||||
"BM1366" => (HEADER, 0xedad_ff38, ChipModel::Bm1366),
|
||||
"BM1362" => (bm1362_header()?, 0x630b_2533, ChipModel::Bm1362),
|
||||
_ => return Err("unsupported captured chip".into()),
|
||||
};
|
||||
let mut backend = HalClient::connect(path)?;
|
||||
// Chip initialization is outside this host-only check.
|
||||
let mut miner = Bm1366Miner::new_for_chip(&mut backend, model, 0, 0, 0, 100, 80_000)
|
||||
.ok_or("invalid simulator probe configuration")?;
|
||||
miner.start()?;
|
||||
let mut target = [0_u8; 32];
|
||||
target[26..28].fill(0xff);
|
||||
miner.submit(header, target, 0x18)?;
|
||||
let share = miner.poll()?.ok_or("missing captured nonce report")?;
|
||||
miner.stop()?;
|
||||
assert_eq!(share.nonce, nonce);
|
||||
println!("{chip} captured report verified at difficulty 1");
|
||||
Ok(())
|
||||
}
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! BM1362 frame encoding from captured command and job traffic.
|
||||
//!
|
||||
//! These pure encoders do not initialize a chain. PLL fields, voltage, and
|
||||
//! model-specific operating points still require silicon-backed validation.
|
||||
|
||||
use crate::bm13xx::{crc16, crc5, reverse_words};
|
||||
|
||||
pub const JOB_BYTES: usize = 88;
|
||||
pub const CHIP_ADDRESS_STEP: u8 = 2;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct ModelGeometry {
|
||||
chips_per_chain: u8,
|
||||
ticket_mask: u8,
|
||||
}
|
||||
|
||||
pub const S19J_PRO: ModelGeometry = ModelGeometry {
|
||||
chips_per_chain: 126,
|
||||
ticket_mask: 0x7f,
|
||||
};
|
||||
pub const S19J: ModelGeometry = ModelGeometry {
|
||||
chips_per_chain: 108,
|
||||
ticket_mask: 0x7f,
|
||||
};
|
||||
pub const S19_88: ModelGeometry = ModelGeometry {
|
||||
chips_per_chain: 88,
|
||||
ticket_mask: 0x3f,
|
||||
};
|
||||
|
||||
impl ModelGeometry {
|
||||
pub const fn new(chips_per_chain: u8, ticket_mask: u8) -> Option<Self> {
|
||||
if chips_per_chain == 0 || chips_per_chain > 128 {
|
||||
return None;
|
||||
}
|
||||
Some(Self {
|
||||
chips_per_chain,
|
||||
ticket_mask,
|
||||
})
|
||||
}
|
||||
|
||||
pub const fn chips_per_chain(self) -> u8 {
|
||||
self.chips_per_chain
|
||||
}
|
||||
|
||||
pub const fn ticket_mask(self) -> u8 {
|
||||
self.ticket_mask
|
||||
}
|
||||
|
||||
/// Addresses for an observed two-address-step enumeration walk.
|
||||
pub fn chip_addresses(self) -> impl Iterator<Item = u8> {
|
||||
(0..self.chips_per_chain).map(|index| index * CHIP_ADDRESS_STEP)
|
||||
}
|
||||
}
|
||||
|
||||
fn short_command(code: u8, address: u8, register: u8) -> [u8; 7] {
|
||||
let mut frame = [0x55, 0xaa, code, 0x05, address, register, 0];
|
||||
frame[6] = crc5(&frame[2..6]);
|
||||
frame
|
||||
}
|
||||
|
||||
fn long_command(code: u8, address: u8, register: u8, value: u32) -> [u8; 11] {
|
||||
let mut frame = [0_u8; 11];
|
||||
frame[..6].copy_from_slice(&[0x55, 0xaa, code, 0x09, address, register]);
|
||||
frame[6..10].copy_from_slice(&value.to_be_bytes());
|
||||
frame[10] = crc5(&frame[2..10]);
|
||||
frame
|
||||
}
|
||||
|
||||
pub fn chain_inactive() -> [u8; 7] {
|
||||
short_command(0x53, 0, 0)
|
||||
}
|
||||
|
||||
pub fn set_chip_address(address: u8) -> [u8; 7] {
|
||||
short_command(0x40, address, 0)
|
||||
}
|
||||
|
||||
pub fn read_register(address: u8, register: u8) -> [u8; 7] {
|
||||
short_command(0x42, address, register)
|
||||
}
|
||||
|
||||
pub fn read_register_all(register: u8) -> [u8; 7] {
|
||||
short_command(0x52, 0, register)
|
||||
}
|
||||
|
||||
pub fn write_register(address: u8, register: u8, value: u32) -> [u8; 11] {
|
||||
long_command(0x41, address, register, value)
|
||||
}
|
||||
|
||||
pub fn write_register_all(register: u8, value: u32) -> [u8; 11] {
|
||||
long_command(0x51, 0, register, value)
|
||||
}
|
||||
|
||||
/// Raw header fields in BM1362 wire order. The caller owns work-ID allocation
|
||||
/// and must supply roots and hashes in the order established by its capture.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct Job {
|
||||
pub work_id: u8,
|
||||
pub num_midstates: u8,
|
||||
pub start_nonce: u32,
|
||||
pub nbits: u32,
|
||||
pub ntime: u32,
|
||||
pub merkle_root: [u8; 32],
|
||||
pub previous_hash: [u8; 32],
|
||||
pub version: u32,
|
||||
}
|
||||
|
||||
/// Encode the 88-byte SEND_JOB form with the true-length tag observed on wire.
|
||||
pub fn encode_job(job: &Job) -> [u8; JOB_BYTES] {
|
||||
let mut frame = [0_u8; JOB_BYTES];
|
||||
frame[..6].copy_from_slice(&[0x55, 0xaa, 0x21, 0x56, job.work_id, job.num_midstates]);
|
||||
frame[6..10].copy_from_slice(&job.start_nonce.to_le_bytes());
|
||||
frame[10..14].copy_from_slice(&job.nbits.to_le_bytes());
|
||||
frame[14..18].copy_from_slice(&job.ntime.to_le_bytes());
|
||||
frame[18..50].copy_from_slice(&job.merkle_root);
|
||||
frame[50..82].copy_from_slice(&job.previous_hash);
|
||||
frame[82..86].copy_from_slice(&job.version.to_le_bytes());
|
||||
let checksum = crc16(&frame[2..86]);
|
||||
frame[86..88].copy_from_slice(&checksum.to_be_bytes());
|
||||
frame
|
||||
}
|
||||
|
||||
/// Encode one nonce-zero pool header in the observed raw-header job layout.
|
||||
/// This covers work delivery; chain initialization still needs board evidence.
|
||||
pub fn encode_work(header: &[u8; 80], work_id: u8) -> Option<[u8; JOB_BYTES]> {
|
||||
if header[76..80] != [0; 4] || work_id & 7 != 0 {
|
||||
return None;
|
||||
}
|
||||
Some(encode_job(&Job {
|
||||
work_id,
|
||||
num_midstates: 1,
|
||||
start_nonce: 0,
|
||||
nbits: u32::from_le_bytes(header[72..76].try_into().expect("nbits")),
|
||||
ntime: u32::from_le_bytes(header[68..72].try_into().expect("ntime")),
|
||||
merkle_root: reverse_words(&header[36..68]),
|
||||
previous_hash: reverse_words(&header[4..36]),
|
||||
version: u32::from_le_bytes(header[..4].try_into().expect("version")),
|
||||
}))
|
||||
}
|
||||
|
||||
/// BT8D for a 25 MHz input clock with BCLK_SEL=0. A model must still choose
|
||||
/// the actual target baud and confirm switching on silicon.
|
||||
pub fn bt8d_for_baud(baud: u32) -> Option<u8> {
|
||||
if baud == 0 || baud > 3_125_000 {
|
||||
return None;
|
||||
}
|
||||
let divider = 25_000_000_u64 / (8 * u64::from(baud));
|
||||
u8::try_from(divider - 1).ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn captured_command_frames() {
|
||||
assert_eq!(chain_inactive(), [0x55, 0xaa, 0x53, 5, 0, 0, 3]);
|
||||
assert_eq!(set_chip_address(0), [0x55, 0xaa, 0x40, 5, 0, 0, 0x1c]);
|
||||
assert_eq!(set_chip_address(8), [0x55, 0xaa, 0x40, 5, 8, 0, 7]);
|
||||
assert_eq!(read_register_all(0), [0x55, 0xaa, 0x52, 5, 0, 0, 0x0a]);
|
||||
assert_eq!(
|
||||
read_register(0x40, 0x1c),
|
||||
[0x55, 0xaa, 0x42, 5, 0x40, 0x1c, 0x0b]
|
||||
);
|
||||
assert_eq!(
|
||||
write_register_all(0x80, 0),
|
||||
[0x55, 0xaa, 0x51, 9, 0, 0x80, 0, 0, 0, 0, 0x1c]
|
||||
);
|
||||
assert_eq!(
|
||||
write_register(0x40, 0x18, 0x7a31),
|
||||
[0x55, 0xaa, 0x41, 9, 0x40, 0x18, 0, 0, 0x7a, 0x31, 0x11]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_address_limits() {
|
||||
assert_eq!(ModelGeometry::new(0, 0), None);
|
||||
assert_eq!(ModelGeometry::new(129, 0), None);
|
||||
assert_eq!(
|
||||
ModelGeometry::new(128, 0).unwrap().chip_addresses().last(),
|
||||
Some(0xfe)
|
||||
);
|
||||
assert_eq!(S19J_PRO.chip_addresses().last(), Some(0xfa));
|
||||
assert_eq!(S19J.chip_addresses().last(), Some(0xd6));
|
||||
assert_eq!(S19_88.chip_addresses().last(), Some(0xae));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn job_frame_layout() {
|
||||
let mut merkle_root = [0_u8; 32];
|
||||
let mut previous_hash = [0_u8; 32];
|
||||
for index in 0..32 {
|
||||
merkle_root[index] = 0xa0 + index as u8;
|
||||
previous_hash[index] = 0x10 + index as u8;
|
||||
}
|
||||
let frame = encode_job(&Job {
|
||||
work_id: 8,
|
||||
num_midstates: 1,
|
||||
start_nonce: 0,
|
||||
nbits: 0x1a1f_2c34,
|
||||
ntime: 0x5e9b_3cd4,
|
||||
merkle_root,
|
||||
previous_hash,
|
||||
version: 0x2000_0000,
|
||||
});
|
||||
assert_eq!(
|
||||
&frame[..18],
|
||||
&[
|
||||
0x55, 0xaa, 0x21, 0x56, 8, 1, 0, 0, 0, 0, 0x34, 0x2c, 0x1f, 0x1a, 0xd4, 0x3c, 0x9b,
|
||||
0x5e
|
||||
]
|
||||
);
|
||||
assert_eq!(&frame[18..50], &merkle_root);
|
||||
assert_eq!(&frame[50..82], &previous_hash);
|
||||
assert_eq!(&frame[82..86], &[0, 0, 0, 0x20]);
|
||||
assert_eq!(&frame[86..88], &[0xc2, 0xbd]);
|
||||
assert_eq!(
|
||||
u16::from_be_bytes([frame[86], frame[87]]),
|
||||
crc16(&frame[2..86])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn baud_dividers() {
|
||||
assert_eq!(bt8d_for_baud(0), None);
|
||||
assert_eq!(bt8d_for_baud(3_000_000), Some(0));
|
||||
assert_eq!(bt8d_for_baud(115_200), Some(26));
|
||||
assert_eq!(bt8d_for_baud(1), None);
|
||||
assert_eq!(bt8d_for_baud(3_125_001), None);
|
||||
assert_eq!(bt8d_for_baud(u32::MAX), None);
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! BM1366 work and response framing. Nonces only come from a device or replay.
|
||||
|
||||
pub const WORK_BYTES: usize = 88;
|
||||
pub const REPORT_BYTES: usize = 11;
|
||||
|
||||
pub use crate::bm13xx::crc16;
|
||||
use crate::bm13xx::{crc5, reverse_words};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct NonceReport {
|
||||
pub nonce: u32,
|
||||
pub work_id: u8,
|
||||
pub version_field: u16,
|
||||
pub chip_address: u8,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ReportError {
|
||||
Length,
|
||||
Preamble,
|
||||
RegisterResponse,
|
||||
Checksum,
|
||||
}
|
||||
|
||||
/// Encode a nonce-zero Bitcoin header for the observed 88-byte SEND_JOB form.
|
||||
/// The low three work-ID bits are reserved by the BM1366 response format.
|
||||
pub fn encode_work(header: &[u8; 80], work_id: u8) -> Option<[u8; WORK_BYTES]> {
|
||||
if header[76..80] != [0; 4] || work_id & 7 != 0 {
|
||||
return None;
|
||||
}
|
||||
let mut frame = [0_u8; WORK_BYTES];
|
||||
frame[..4].copy_from_slice(&[0x55, 0xaa, 0x21, 0x54]);
|
||||
frame[4] = work_id;
|
||||
// The captured 88-byte on-die-midstate job uses one midstate.
|
||||
frame[5] = 1;
|
||||
frame[10..14].copy_from_slice(&header[72..76]);
|
||||
frame[14..18].copy_from_slice(&header[68..72]);
|
||||
frame[18..50].copy_from_slice(&reverse_words(&header[36..68]));
|
||||
frame[50..82].copy_from_slice(&reverse_words(&header[4..36]));
|
||||
frame[82..86].copy_from_slice(&header[..4]);
|
||||
let checksum = crc16(&frame[2..86]);
|
||||
frame[86..88].copy_from_slice(&checksum.to_be_bytes());
|
||||
Some(frame)
|
||||
}
|
||||
|
||||
/// Parse an observed nonce report; register replies must use a separate path.
|
||||
pub fn decode_nonce_report(frame: &[u8]) -> Result<NonceReport, ReportError> {
|
||||
if frame.len() != REPORT_BYTES {
|
||||
return Err(ReportError::Length);
|
||||
}
|
||||
if frame[..2] != [0xaa, 0x55] {
|
||||
return Err(ReportError::Preamble);
|
||||
}
|
||||
if frame[10] & 0x80 == 0 {
|
||||
return Err(ReportError::RegisterResponse);
|
||||
}
|
||||
if crc5(&frame[2..]) != 0 {
|
||||
return Err(ReportError::Checksum);
|
||||
}
|
||||
Ok(NonceReport {
|
||||
nonce: u32::from_le_bytes(frame[2..6].try_into().expect("four-byte nonce")),
|
||||
chip_address: frame[6],
|
||||
work_id: frame[7] & 0xf8,
|
||||
version_field: u16::from_be_bytes(frame[8..10].try_into().expect("version field")),
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn crc16_reference_vector() {
|
||||
assert_eq!(crc16(b"123456789"), 0x29b1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_work_and_response() {
|
||||
let header: [u8; 80] = hex_header();
|
||||
let frame = encode_work(&header, 0x18).unwrap();
|
||||
assert_eq!(&frame[..5], &[0x55, 0xaa, 0x21, 0x54, 0x18]);
|
||||
assert_eq!(frame[5], 1);
|
||||
assert_eq!(
|
||||
crc16(&frame[2..86]),
|
||||
u16::from_be_bytes([frame[86], frame[87]])
|
||||
);
|
||||
assert_eq!(&frame[10..14], &header[72..76]);
|
||||
assert_eq!(&frame[82..86], &header[..4]);
|
||||
assert!(encode_work(&header, 0x19).is_none());
|
||||
let mut with_nonce = header;
|
||||
with_nonce[76] = 1;
|
||||
assert!(encode_work(&with_nonce, 0x18).is_none());
|
||||
|
||||
let report = [
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
];
|
||||
let parsed = decode_nonce_report(&report).unwrap();
|
||||
assert_eq!(parsed.nonce, 0xedad_ff38);
|
||||
assert_eq!(parsed.work_id, 0x18);
|
||||
assert_eq!(parsed.version_field, 0x04b3);
|
||||
let mut corrupt = report;
|
||||
corrupt[4] ^= 1;
|
||||
assert_eq!(decode_nonce_report(&corrupt), Err(ReportError::Checksum));
|
||||
}
|
||||
|
||||
fn hex_header() -> [u8; 80] {
|
||||
let input = concat!(
|
||||
"00000020fc3ab8d43021f45eb662389d0f791f657dbf75a5cb93670b57b6c22503",
|
||||
"b4213a43ae1052ebd44443068fe178e2c61ca4994846fd133addb04aab2b382c5",
|
||||
"b4c74c1012e6a8f06021700000000"
|
||||
);
|
||||
let mut header = [0_u8; 80];
|
||||
for (index, bytes) in input.as_bytes().chunks_exact(2).enumerate() {
|
||||
let pair = std::str::from_utf8(bytes).unwrap();
|
||||
header[index] = u8::from_str_radix(pair, 16).unwrap();
|
||||
}
|
||||
header
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Shared BM13xx wire checksums. This module never generates mining nonces.
|
||||
|
||||
/// Reverse the eight 32-bit header words for the observed SEND_JOB layout.
|
||||
pub(crate) fn reverse_words(input: &[u8]) -> [u8; 32] {
|
||||
let mut output = [0_u8; 32];
|
||||
for index in 0..8 {
|
||||
output[index * 4..index * 4 + 4].copy_from_slice(&input[(7 - index) * 4..(8 - index) * 4]);
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// CRC5 used by command and nonce-report frames, initial value 0x1f.
|
||||
pub fn crc5(bytes: &[u8]) -> u8 {
|
||||
let mut state = 0x1f_u8;
|
||||
for &byte in bytes {
|
||||
for bit in (0..8).rev() {
|
||||
let feedback = ((state >> 4) ^ (byte >> bit)) & 1;
|
||||
state = ((state << 1) | feedback) & 0x1f;
|
||||
state ^= feedback << 2;
|
||||
}
|
||||
}
|
||||
state
|
||||
}
|
||||
|
||||
/// CRC16-CCITT-FALSE used by 88-byte SEND_JOB frames.
|
||||
pub fn crc16(bytes: &[u8]) -> u16 {
|
||||
let mut state = 0xffff_u16;
|
||||
for &byte in bytes {
|
||||
state ^= u16::from(byte) << 8;
|
||||
for _ in 0..8 {
|
||||
state = if state & 0x8000 == 0 {
|
||||
state << 1
|
||||
} else {
|
||||
(state << 1) ^ 0x1021
|
||||
};
|
||||
}
|
||||
}
|
||||
state
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn captured_command_checksums() {
|
||||
let vectors: &[(&[u8], u8)] = &[
|
||||
(&[0x53, 0x05, 0, 0], 0x03),
|
||||
(&[0x40, 0x05, 0, 0], 0x1c),
|
||||
(&[0x52, 0x05, 0, 0], 0x0a),
|
||||
(&[0x40, 0x05, 8, 0], 0x07),
|
||||
(&[0x42, 0x05, 0x40, 0x1c], 0x0b),
|
||||
(&[0x51, 0x09, 0, 0x80, 0, 0, 0, 0], 0x1c),
|
||||
(&[0x41, 0x09, 0x40, 0x18, 0, 0, 0x7a, 0x31], 0x11),
|
||||
];
|
||||
for &(body, expected) in vectors {
|
||||
assert_eq!(crc5(body), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn standard_crc16_check() {
|
||||
assert_eq!(crc16(b"123456789"), 0x29b1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Exact Bitcoin Stratum share target conversion; no floating point or nonce search.
|
||||
|
||||
use serde_json::Number;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum DifficultyError {
|
||||
Invalid,
|
||||
OutOfRange,
|
||||
}
|
||||
|
||||
/// A positive decimal difficulty represented exactly as numerator/denominator.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct PoolDifficulty {
|
||||
numerator: u64,
|
||||
denominator: u64,
|
||||
}
|
||||
|
||||
impl PoolDifficulty {
|
||||
pub fn from_json_number(number: &Number) -> Result<Self, DifficultyError> {
|
||||
let text = number.as_str();
|
||||
if text.len() > 128 || text.starts_with('-') {
|
||||
return Err(DifficultyError::OutOfRange);
|
||||
}
|
||||
let (mantissa, exponent) = match text.split_once(['e', 'E']) {
|
||||
Some((base, exponent)) => (
|
||||
base,
|
||||
exponent
|
||||
.parse::<i32>()
|
||||
.map_err(|_| DifficultyError::OutOfRange)?,
|
||||
),
|
||||
None => (text, 0),
|
||||
};
|
||||
let mut digits = Vec::with_capacity(mantissa.len());
|
||||
let mut fractional = 0_i32;
|
||||
let mut after_dot = false;
|
||||
for byte in mantissa.bytes() {
|
||||
match byte {
|
||||
b'.' if !after_dot => after_dot = true,
|
||||
b'0'..=b'9' => {
|
||||
digits.push(byte - b'0');
|
||||
if after_dot {
|
||||
fractional += 1;
|
||||
}
|
||||
}
|
||||
_ => return Err(DifficultyError::Invalid),
|
||||
}
|
||||
}
|
||||
let first = digits
|
||||
.iter()
|
||||
.position(|digit| *digit != 0)
|
||||
.ok_or(DifficultyError::Invalid)?;
|
||||
let last = digits
|
||||
.iter()
|
||||
.rposition(|digit| *digit != 0)
|
||||
.expect("nonzero digit");
|
||||
let trailing = (digits.len() - last - 1) as i32;
|
||||
let mut numerator = 0_u64;
|
||||
for digit in &digits[first..=last] {
|
||||
numerator = numerator
|
||||
.checked_mul(10)
|
||||
.and_then(|value| value.checked_add(u64::from(*digit)))
|
||||
.ok_or(DifficultyError::OutOfRange)?;
|
||||
}
|
||||
let shift = exponent
|
||||
.checked_sub(fractional)
|
||||
.and_then(|value| value.checked_add(trailing))
|
||||
.ok_or(DifficultyError::OutOfRange)?;
|
||||
if !(-19..=19).contains(&shift) {
|
||||
return Err(DifficultyError::OutOfRange);
|
||||
}
|
||||
let mut denominator = 1_u64;
|
||||
if shift >= 0 {
|
||||
for _ in 0..shift {
|
||||
numerator = numerator
|
||||
.checked_mul(10)
|
||||
.ok_or(DifficultyError::OutOfRange)?;
|
||||
}
|
||||
} else {
|
||||
for _ in 0..-shift {
|
||||
denominator = denominator
|
||||
.checked_mul(10)
|
||||
.ok_or(DifficultyError::OutOfRange)?;
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
numerator,
|
||||
denominator,
|
||||
})
|
||||
}
|
||||
|
||||
/// floor((0xFFFF << 208) / difficulty), capped at the 256-bit maximum.
|
||||
/// The 320-bit intermediate permits all supported decimal denominators.
|
||||
pub fn target_le(self) -> [u8; 32] {
|
||||
let mut product = [0_u8; 40];
|
||||
let mut carry = 0_u128;
|
||||
for (index, byte) in product.iter_mut().enumerate() {
|
||||
let diff_one_byte = if index == 26 || index == 27 { 0xff } else { 0 };
|
||||
let value = (diff_one_byte as u128) * u128::from(self.denominator) + carry;
|
||||
*byte = value as u8;
|
||||
carry = value >> 8;
|
||||
}
|
||||
debug_assert_eq!(carry, 0);
|
||||
|
||||
let mut quotient = [0_u8; 40];
|
||||
let mut remainder = 0_u128;
|
||||
for index in (0..320).rev() {
|
||||
let bit = (product[index / 8] >> (index % 8)) & 1;
|
||||
remainder = (remainder << 1) | u128::from(bit);
|
||||
if remainder >= u128::from(self.numerator) {
|
||||
remainder -= u128::from(self.numerator);
|
||||
quotient[index / 8] |= 1 << (index % 8);
|
||||
}
|
||||
}
|
||||
if quotient[32..].iter().any(|byte| *byte != 0) {
|
||||
return [0xff; 32];
|
||||
}
|
||||
quotient[..32].try_into().expect("256-bit quotient")
|
||||
}
|
||||
}
|
||||
+405
@@ -0,0 +1,405 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Versioned local wire contract between Alkane OS and an SDK hardware backend.
|
||||
|
||||
use crate::HardwareBackend;
|
||||
use std::fmt;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::net::Shutdown;
|
||||
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::path::Path;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const MAGIC: [u8; 4] = *b"ALKH";
|
||||
const VERSION: u8 = 1;
|
||||
const RESPONSE: u16 = 1;
|
||||
const HEADER_LEN: usize = 16;
|
||||
const MAX_PAYLOAD: usize = 4096;
|
||||
const IO_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
|
||||
const SEND_FRAME: u8 = 1;
|
||||
const RECEIVE_FRAME: u8 = 2;
|
||||
const SET_CHAIN_RESET: u8 = 3;
|
||||
const SET_FAN_TARGET: u8 = 4;
|
||||
const READ_TEMPERATURE: u8 = 5;
|
||||
const SET_PSU_ENABLED: u8 = 6;
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
compile_error!("Alkane HAL sockets require Linux");
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod sys {
|
||||
pub use std::ffi::{c_char as CChar, c_int as CInt, c_void as CVoid};
|
||||
pub type SaFamily = u16;
|
||||
pub type SocklenT = u32;
|
||||
|
||||
pub const AF_UNIX: CInt = 1;
|
||||
pub const SOCK_STREAM: CInt = 1;
|
||||
pub const SOCK_NONBLOCK: CInt = 0x800;
|
||||
pub const SOCK_CLOEXEC: CInt = 0x80000;
|
||||
pub const SOL_SOCKET: CInt = 1;
|
||||
pub const SO_ERROR: CInt = 4;
|
||||
pub const POLLOUT: i16 = 4;
|
||||
pub const EAGAIN: CInt = 11;
|
||||
pub const EISCONN: CInt = 106;
|
||||
pub const EINPROGRESS: CInt = 115;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct SockaddrUn {
|
||||
pub sun_family: SaFamily,
|
||||
pub sun_path: [CChar; 108],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct PollFd {
|
||||
pub fd: CInt,
|
||||
pub events: i16,
|
||||
pub revents: i16,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
pub fn socket(domain: CInt, socket_type: CInt, protocol: CInt) -> CInt;
|
||||
pub fn connect(fd: CInt, address: *const CVoid, length: SocklenT) -> CInt;
|
||||
pub fn poll(fds: *mut PollFd, count: usize, timeout_ms: CInt) -> CInt;
|
||||
pub fn getsockopt(
|
||||
fd: CInt,
|
||||
level: CInt,
|
||||
name: CInt,
|
||||
value: *mut CVoid,
|
||||
length: *mut SocklenT,
|
||||
) -> CInt;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum HalError {
|
||||
Io(io::Error),
|
||||
Protocol(&'static str),
|
||||
Device(u8),
|
||||
FrameTooLong,
|
||||
InvalidFanPercent,
|
||||
}
|
||||
|
||||
impl fmt::Display for HalError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Io(error) => write!(f, "HAL I/O: {error}"),
|
||||
Self::Protocol(message) => write!(f, "HAL protocol: {message}"),
|
||||
Self::Device(code) => write!(f, "HAL device error: {code}"),
|
||||
Self::FrameTooLong => f.write_str("HAL frame exceeds the wire limit"),
|
||||
Self::InvalidFanPercent => f.write_str("HAL fan target exceeds 100 percent"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for HalError {}
|
||||
|
||||
impl From<io::Error> for HalError {
|
||||
fn from(error: io::Error) -> Self {
|
||||
Self::Io(error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Local client for the privileged board backend supplied by the SDK image.
|
||||
pub struct HalClient {
|
||||
stream: UnixStream,
|
||||
sequence: u32,
|
||||
poisoned: bool,
|
||||
}
|
||||
|
||||
impl HalClient {
|
||||
fn poison(&mut self) {
|
||||
self.poisoned = true;
|
||||
let _ = self.stream.shutdown(Shutdown::Both);
|
||||
}
|
||||
|
||||
pub fn connect(path: impl AsRef<Path>) -> Result<Self, HalError> {
|
||||
let stream = connect_until(path.as_ref(), Instant::now() + IO_TIMEOUT)?;
|
||||
Self::from_stream(stream)
|
||||
}
|
||||
|
||||
pub fn from_stream(stream: UnixStream) -> Result<Self, HalError> {
|
||||
stream.set_read_timeout(Some(IO_TIMEOUT))?;
|
||||
stream.set_write_timeout(Some(IO_TIMEOUT))?;
|
||||
Ok(Self {
|
||||
stream,
|
||||
sequence: 0,
|
||||
poisoned: false,
|
||||
})
|
||||
}
|
||||
|
||||
fn exchange(&mut self, opcode: u8, payload: &[u8]) -> Result<Vec<u8>, HalError> {
|
||||
if self.poisoned {
|
||||
return Err(HalError::Protocol("connection is unusable"));
|
||||
}
|
||||
if payload.len() > MAX_PAYLOAD {
|
||||
return Err(HalError::FrameTooLong);
|
||||
}
|
||||
let result = self.exchange_inner(opcode, payload);
|
||||
if let Err(ref error) = result {
|
||||
if !matches!(error, HalError::Device(_)) {
|
||||
self.poison();
|
||||
// A backend power lease must expire on disconnect. Reusing a
|
||||
// desynchronized stream could silently discard an off request.
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn exchange_inner(&mut self, opcode: u8, payload: &[u8]) -> Result<Vec<u8>, HalError> {
|
||||
let deadline = Instant::now() + IO_TIMEOUT;
|
||||
self.sequence = self.sequence.wrapping_add(1);
|
||||
let sequence = self.sequence;
|
||||
let mut header = [0_u8; HEADER_LEN];
|
||||
header[..4].copy_from_slice(&MAGIC);
|
||||
header[4] = VERSION;
|
||||
header[5] = opcode;
|
||||
header[8..12].copy_from_slice(&sequence.to_le_bytes());
|
||||
header[12..16].copy_from_slice(&(payload.len() as u32).to_le_bytes());
|
||||
write_all_until(&mut self.stream, &header, deadline)?;
|
||||
write_all_until(&mut self.stream, payload, deadline)?;
|
||||
|
||||
read_exact_until(&mut self.stream, &mut header, deadline)?;
|
||||
if header[..4] != MAGIC || header[4] != VERSION || header[5] != opcode {
|
||||
return Err(HalError::Protocol("response identity mismatch"));
|
||||
}
|
||||
if u16::from_le_bytes([header[6], header[7]]) != RESPONSE {
|
||||
return Err(HalError::Protocol("response flag missing"));
|
||||
}
|
||||
if u32::from_le_bytes(header[8..12].try_into().unwrap()) != sequence {
|
||||
return Err(HalError::Protocol("response sequence mismatch"));
|
||||
}
|
||||
let length = u32::from_le_bytes(header[12..16].try_into().unwrap()) as usize;
|
||||
if !(1..=MAX_PAYLOAD).contains(&length) {
|
||||
return Err(HalError::Protocol("invalid response length"));
|
||||
}
|
||||
let mut body = vec![0_u8; length];
|
||||
read_exact_until(&mut self.stream, &mut body, deadline)?;
|
||||
if body[0] != 0 {
|
||||
if !matches!(body[0], 1..=3) || body.len() != 1 {
|
||||
return Err(HalError::Protocol("invalid error response"));
|
||||
}
|
||||
return Err(HalError::Device(body[0]));
|
||||
}
|
||||
Ok(body[1..].to_vec())
|
||||
}
|
||||
|
||||
fn empty_reply(&mut self, opcode: u8, payload: &[u8]) -> Result<(), HalError> {
|
||||
if !self.exchange(opcode, payload)?.is_empty() {
|
||||
self.poison();
|
||||
return Err(HalError::Protocol("unexpected response data"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn timeout_error() -> io::Error {
|
||||
io::Error::new(io::ErrorKind::TimedOut, "HAL operation timed out")
|
||||
}
|
||||
|
||||
fn remaining(deadline: Instant) -> io::Result<Duration> {
|
||||
deadline
|
||||
.checked_duration_since(Instant::now())
|
||||
.filter(|duration| !duration.is_zero())
|
||||
.ok_or_else(timeout_error)
|
||||
}
|
||||
|
||||
fn write_all_until(stream: &mut UnixStream, mut data: &[u8], deadline: Instant) -> io::Result<()> {
|
||||
while !data.is_empty() {
|
||||
stream.set_write_timeout(Some(remaining(deadline)?))?;
|
||||
match stream.write(data) {
|
||||
Ok(0) => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::WriteZero,
|
||||
"HAL socket closed",
|
||||
))
|
||||
}
|
||||
Ok(count) => data = &data[count..],
|
||||
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_exact_until(
|
||||
stream: &mut UnixStream,
|
||||
mut data: &mut [u8],
|
||||
deadline: Instant,
|
||||
) -> io::Result<()> {
|
||||
while !data.is_empty() {
|
||||
stream.set_read_timeout(Some(remaining(deadline)?))?;
|
||||
match stream.read(data) {
|
||||
Ok(0) => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof,
|
||||
"HAL socket closed",
|
||||
))
|
||||
}
|
||||
Ok(count) => data = &mut data[count..],
|
||||
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn connect_until(path: &Path, deadline: Instant) -> io::Result<UnixStream> {
|
||||
let bytes = path.as_os_str().as_bytes();
|
||||
let max_path =
|
||||
std::mem::size_of::<sys::SockaddrUn>() - std::mem::offset_of!(sys::SockaddrUn, sun_path);
|
||||
if bytes.is_empty() || bytes.len() >= max_path || bytes.contains(&0) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"invalid HAL socket path",
|
||||
));
|
||||
}
|
||||
let raw = unsafe {
|
||||
sys::socket(
|
||||
sys::AF_UNIX,
|
||||
sys::SOCK_STREAM | sys::SOCK_NONBLOCK | sys::SOCK_CLOEXEC,
|
||||
0,
|
||||
)
|
||||
};
|
||||
if raw < 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
let fd = unsafe { OwnedFd::from_raw_fd(raw) };
|
||||
let mut address: sys::SockaddrUn = unsafe { std::mem::zeroed() };
|
||||
address.sun_family = sys::AF_UNIX as sys::SaFamily;
|
||||
for (destination, source) in address.sun_path.iter_mut().zip(bytes) {
|
||||
*destination = *source as sys::CChar;
|
||||
}
|
||||
let length =
|
||||
(std::mem::offset_of!(sys::SockaddrUn, sun_path) + bytes.len() + 1) as sys::SocklenT;
|
||||
loop {
|
||||
let result = unsafe {
|
||||
sys::connect(
|
||||
fd.as_raw_fd(),
|
||||
&address as *const _ as *const sys::CVoid,
|
||||
length,
|
||||
)
|
||||
};
|
||||
if result == 0 {
|
||||
break;
|
||||
}
|
||||
let error = io::Error::last_os_error();
|
||||
if error.raw_os_error() == Some(sys::EISCONN) {
|
||||
break;
|
||||
}
|
||||
if !matches!(
|
||||
error.raw_os_error(),
|
||||
Some(sys::EINPROGRESS) | Some(sys::EAGAIN)
|
||||
) {
|
||||
return Err(error);
|
||||
}
|
||||
let mut poll_fd = sys::PollFd {
|
||||
fd: fd.as_raw_fd(),
|
||||
events: sys::POLLOUT,
|
||||
revents: 0,
|
||||
};
|
||||
let wait = remaining(deadline)?.as_millis().min(i32::MAX as u128) as i32;
|
||||
let polled = unsafe { sys::poll(&mut poll_fd, 1, wait.max(1)) };
|
||||
if polled < 0 {
|
||||
let error = io::Error::last_os_error();
|
||||
if error.kind() == io::ErrorKind::Interrupted {
|
||||
continue;
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
if polled == 0 {
|
||||
return Err(timeout_error());
|
||||
}
|
||||
let mut socket_error: sys::CInt = 0;
|
||||
let mut socket_error_len = std::mem::size_of_val(&socket_error) as sys::SocklenT;
|
||||
if unsafe {
|
||||
sys::getsockopt(
|
||||
fd.as_raw_fd(),
|
||||
sys::SOL_SOCKET,
|
||||
sys::SO_ERROR,
|
||||
&mut socket_error as *mut _ as *mut sys::CVoid,
|
||||
&mut socket_error_len,
|
||||
)
|
||||
} < 0
|
||||
{
|
||||
return Err(io::Error::last_os_error());
|
||||
}
|
||||
if socket_error != 0 {
|
||||
return Err(io::Error::from_raw_os_error(socket_error));
|
||||
}
|
||||
if error.raw_os_error() == Some(sys::EINPROGRESS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let stream = UnixStream::from(fd);
|
||||
stream.set_nonblocking(false)?;
|
||||
Ok(stream)
|
||||
}
|
||||
|
||||
impl HardwareBackend for HalClient {
|
||||
type Error = HalError;
|
||||
|
||||
fn send_frame(&mut self, chain: u8, frame: &[u8]) -> Result<(), Self::Error> {
|
||||
if frame.len() > MAX_PAYLOAD - 3 {
|
||||
return Err(HalError::FrameTooLong);
|
||||
}
|
||||
let mut body = Vec::with_capacity(frame.len() + 3);
|
||||
body.push(chain);
|
||||
body.extend_from_slice(&(frame.len() as u16).to_le_bytes());
|
||||
body.extend_from_slice(frame);
|
||||
self.empty_reply(SEND_FRAME, &body)
|
||||
}
|
||||
|
||||
fn receive_frame(&mut self, chain: u8, frame: &mut [u8]) -> Result<usize, Self::Error> {
|
||||
if frame.len() > MAX_PAYLOAD - 3 {
|
||||
return Err(HalError::FrameTooLong);
|
||||
}
|
||||
let mut request = [0_u8; 3];
|
||||
request[0] = chain;
|
||||
request[1..].copy_from_slice(&(frame.len() as u16).to_le_bytes());
|
||||
let body = self.exchange(RECEIVE_FRAME, &request)?;
|
||||
if body.len() < 2 {
|
||||
self.poison();
|
||||
return Err(HalError::Protocol("missing receive length"));
|
||||
}
|
||||
let count = u16::from_le_bytes([body[0], body[1]]) as usize;
|
||||
if count > frame.len() || body.len() != count + 2 {
|
||||
self.poison();
|
||||
return Err(HalError::Protocol("invalid received frame size"));
|
||||
}
|
||||
frame[..count].copy_from_slice(&body[2..]);
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
fn set_chain_reset(&mut self, chain: u8, asserted: bool) -> Result<(), Self::Error> {
|
||||
self.empty_reply(SET_CHAIN_RESET, &[chain, u8::from(asserted)])
|
||||
}
|
||||
|
||||
fn set_fan_target(&mut self, fan: u8, percent: u8) -> Result<(), Self::Error> {
|
||||
if percent > 100 {
|
||||
return Err(HalError::InvalidFanPercent);
|
||||
}
|
||||
self.empty_reply(SET_FAN_TARGET, &[fan, percent])
|
||||
}
|
||||
|
||||
fn read_temperature_mc(&mut self, sensor: u8) -> Result<i32, Self::Error> {
|
||||
let body = self.exchange(READ_TEMPERATURE, &[sensor])?;
|
||||
let bytes: [u8; 4] = match body.try_into() {
|
||||
Ok(bytes) => bytes,
|
||||
Err(_) => {
|
||||
self.poison();
|
||||
return Err(HalError::Protocol("invalid temperature response"));
|
||||
}
|
||||
};
|
||||
Ok(i32::from_le_bytes(bytes))
|
||||
}
|
||||
|
||||
fn set_psu_enabled(&mut self, enabled: bool) -> Result<(), Self::Error> {
|
||||
let result = self.empty_reply(SET_PSU_ENABLED, &[u8::from(enabled)]);
|
||||
if !enabled && result.is_err() {
|
||||
self.poison();
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
+16
@@ -3,6 +3,22 @@
|
||||
//! An entry in the target matrix is a development obligation, not a claim that
|
||||
//! the pairing already mines. Hardware backends are supplied by SDK images.
|
||||
|
||||
pub mod bm1362;
|
||||
pub mod bm1366;
|
||||
pub mod bm13xx;
|
||||
pub mod difficulty;
|
||||
pub mod hal_wire;
|
||||
pub mod management;
|
||||
pub mod miner;
|
||||
pub mod pow;
|
||||
pub mod scrypt_pow;
|
||||
pub mod stratum_client;
|
||||
pub mod stratum_session;
|
||||
pub mod stratum_submit;
|
||||
pub mod stratum_v1;
|
||||
pub mod stratum_wire;
|
||||
pub mod stratum_work;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Vendor {
|
||||
Bitmain,
|
||||
|
||||
+89
-2
@@ -1,7 +1,88 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::hal_wire::HalClient;
|
||||
use alkane_os::management::{bind_local, serve, MiningStatus};
|
||||
use alkane_os::miner::{Bm1366Miner, ChipModel};
|
||||
use alkane_os::stratum_client::{PoolConnection, PoolEvent};
|
||||
use alkane_os::target_matrix;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
fn sim_mine(mut args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
|
||||
let socket = args.next().ok_or("missing HAL socket")?;
|
||||
let address: SocketAddr = args.next().ok_or("missing pool address")?.parse()?;
|
||||
let worker = args.next().ok_or("missing pool worker")?;
|
||||
let chip = match args.next().as_deref() {
|
||||
Some("BM1362") => ChipModel::Bm1362,
|
||||
Some("BM1366") => ChipModel::Bm1366,
|
||||
_ => return Err("select BM1362 or BM1366".into()),
|
||||
};
|
||||
let max_accepts: Option<u64> = args.next().map(|value| value.parse()).transpose()?;
|
||||
if args.next().is_some() || worker.is_empty() || max_accepts == Some(0) {
|
||||
return Err("invalid simulator mining arguments".into());
|
||||
}
|
||||
let password = std::env::var("ALKANE_POOL_PASSWORD")
|
||||
.map_err(|_| "set ALKANE_POOL_PASSWORD in the environment")?;
|
||||
let status = Arc::new(MiningStatus::new());
|
||||
if let Ok(bind) = std::env::var("ALKANE_API_LISTEN") {
|
||||
let listener = bind_local(bind.parse()?)?;
|
||||
let snapshot = Arc::clone(&status);
|
||||
std::thread::spawn(move || {
|
||||
if let Err(error) = serve(listener, snapshot) {
|
||||
eprintln!("management API stopped: {error}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Connect to the pool before enabling power. The SDK HAL server owns the
|
||||
// power lease and cuts power if this process stalls or disconnects.
|
||||
let mut pool = PoolConnection::connect(
|
||||
address,
|
||||
concat!("alkane-os/", env!("CARGO_PKG_VERSION")),
|
||||
&worker,
|
||||
&password,
|
||||
Duration::from_secs(2),
|
||||
Duration::from_secs(5),
|
||||
)
|
||||
.map_err(|error| io::Error::other(format!("pool handshake: {error:?}")))?;
|
||||
let mut backend = HalClient::connect(socket)?;
|
||||
let mut miner = Bm1366Miner::new_for_chip(&mut backend, chip, 0, 0, 0, 100, 80_000)
|
||||
.ok_or("invalid simulator safety configuration")?;
|
||||
miner.start()?;
|
||||
status.set_running(true);
|
||||
let mut accepted = 0_u64;
|
||||
loop {
|
||||
let (event, reports) = pool
|
||||
.cycle(&mut miner)
|
||||
.map_err(|error| io::Error::other(format!("mining cycle: {error:?}")))?;
|
||||
match event {
|
||||
PoolEvent::ShareAccepted(_) => {
|
||||
accepted += 1;
|
||||
status.share_accepted();
|
||||
if max_accepts == Some(accepted) {
|
||||
miner.stop()?;
|
||||
status.set_running(false);
|
||||
println!("simulator shares accepted: {accepted}");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
PoolEvent::ShareRejected(id) => {
|
||||
status.share_rejected();
|
||||
eprintln!("pool rejected share request {id}");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if matches!(event, PoolEvent::Idle) && reports.is_empty() {
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match std::env::args().nth(1).as_deref() {
|
||||
let mut args = std::env::args().skip(1);
|
||||
match args.next().as_deref() {
|
||||
Some("--list-targets") => {
|
||||
println!("control_board,hash_chip,algorithm");
|
||||
for target in target_matrix() {
|
||||
@@ -12,8 +93,14 @@ fn main() {
|
||||
}
|
||||
}
|
||||
Some("--version") => println!("alkane-os {}", env!("CARGO_PKG_VERSION")),
|
||||
Some("--mine-simulator") => {
|
||||
if let Err(error) = sim_mine(args) {
|
||||
eprintln!("simulator mining failed: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
eprintln!("usage: alkane-os --list-targets | --version");
|
||||
eprintln!("usage: alkane-os --list-targets | --version | --mine-simulator HAL_SOCKET POOL_ADDRESS WORKER BM1362|BM1366 [MAX_ACCEPTS]");
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Bounded, read-only cgminer-style status for a local management client.
|
||||
|
||||
use serde_json::{json, Value};
|
||||
use std::io::{self, Read, Write};
|
||||
use std::net::{SocketAddr, TcpListener, TcpStream};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
const MAX_REQUEST_BYTES: usize = 2048;
|
||||
const CLIENT_TIMEOUT: Duration = Duration::from_millis(500);
|
||||
const REQUEST_DEADLINE: Duration = Duration::from_secs(2);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MiningStatus {
|
||||
started: Instant,
|
||||
running: AtomicBool,
|
||||
accepted: AtomicU64,
|
||||
rejected: AtomicU64,
|
||||
}
|
||||
|
||||
impl Default for MiningStatus {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl MiningStatus {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
started: Instant::now(),
|
||||
running: AtomicBool::new(false),
|
||||
accepted: AtomicU64::new(0),
|
||||
rejected: AtomicU64::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_running(&self, running: bool) {
|
||||
self.running.store(running, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn share_accepted(&self) {
|
||||
self.accepted.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn share_rejected(&self) {
|
||||
self.rejected.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
fn response(&self, command: &str) -> Value {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
let (code, description, payload) = match command {
|
||||
"summary" => (
|
||||
"S",
|
||||
"Summary",
|
||||
json!({"SUMMARY": [{
|
||||
"Elapsed": self.started.elapsed().as_secs(),
|
||||
"Accepted": self.accepted.load(Ordering::Relaxed),
|
||||
"Rejected": self.rejected.load(Ordering::Relaxed),
|
||||
"Status": if self.running.load(Ordering::Relaxed) { "Mining" } else { "Stopped" }
|
||||
}]}),
|
||||
),
|
||||
"version" => (
|
||||
"S",
|
||||
"Version",
|
||||
json!({"VERSION": [{
|
||||
"Miner": concat!("alkane-os/", env!("CARGO_PKG_VERSION")),
|
||||
"API": "1.0"
|
||||
}]}),
|
||||
),
|
||||
_ => ("E", "Unknown command", json!({})),
|
||||
};
|
||||
let mut response = json!({"STATUS": [{
|
||||
"STATUS": code,
|
||||
"When": now,
|
||||
"Code": if code == "S" { 0 } else { 14 },
|
||||
"Msg": description,
|
||||
"Description": "alkane-os"
|
||||
}]});
|
||||
response
|
||||
.as_object_mut()
|
||||
.unwrap()
|
||||
.extend(payload.as_object().unwrap().clone());
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
fn request_command(bytes: &[u8]) -> Option<&str> {
|
||||
let request = std::str::from_utf8(bytes).ok()?.trim();
|
||||
if request.starts_with('{') {
|
||||
let value: Value = serde_json::from_str(request).ok()?;
|
||||
let command = value.get("command")?.as_str()?;
|
||||
match command {
|
||||
"summary" => Some("summary"),
|
||||
"version" => Some("version"),
|
||||
_ => Some("unknown"),
|
||||
}
|
||||
} else {
|
||||
match request {
|
||||
"summary" => Some("summary"),
|
||||
"version" => Some("version"),
|
||||
_ => Some("unknown"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle(mut stream: TcpStream, status: &MiningStatus) -> io::Result<()> {
|
||||
stream.set_write_timeout(Some(CLIENT_TIMEOUT))?;
|
||||
let mut request = Vec::new();
|
||||
let mut chunk = [0_u8; 256];
|
||||
let deadline = Instant::now() + REQUEST_DEADLINE;
|
||||
loop {
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::TimedOut,
|
||||
"management request deadline",
|
||||
));
|
||||
}
|
||||
stream.set_read_timeout(Some(CLIENT_TIMEOUT.min(remaining)))?;
|
||||
let count = match stream.read(&mut chunk) {
|
||||
Ok(count) => count,
|
||||
Err(error) if error.kind() == io::ErrorKind::TimedOut && !request.is_empty() => break,
|
||||
Err(error) if error.kind() == io::ErrorKind::WouldBlock && !request.is_empty() => break,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
if request.len() + count > MAX_REQUEST_BYTES {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"management request too large",
|
||||
));
|
||||
}
|
||||
request.extend_from_slice(&chunk[..count]);
|
||||
if request.contains(&b'\n') || request.contains(&0) {
|
||||
break;
|
||||
}
|
||||
if matches!(request_command(&request), Some("summary" | "version"))
|
||||
|| serde_json::from_slice::<Value>(&request).is_ok()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
let end = request
|
||||
.iter()
|
||||
.position(|byte| *byte == 0 || *byte == b'\n')
|
||||
.unwrap_or(request.len());
|
||||
let command = request_command(&request[..end]).unwrap_or("unknown");
|
||||
let mut reply = serde_json::to_vec(&status.response(command)).map_err(io::Error::other)?;
|
||||
reply.push(0);
|
||||
stream.write_all(&reply)
|
||||
}
|
||||
|
||||
/// Bind only to loopback. The board management plane can be reached through SSH.
|
||||
pub fn bind_local(address: SocketAddr) -> io::Result<TcpListener> {
|
||||
if !address.ip().is_loopback() {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"management API requires loopback address",
|
||||
));
|
||||
}
|
||||
TcpListener::bind(address)
|
||||
}
|
||||
|
||||
pub fn serve(listener: TcpListener, status: Arc<MiningStatus>) -> io::Result<()> {
|
||||
for stream in listener.incoming() {
|
||||
let stream = stream?;
|
||||
if let Err(error) = handle(stream, &status) {
|
||||
eprintln!("management request failed: {error}");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::net::TcpStream;
|
||||
|
||||
#[test]
|
||||
fn refuses_nonlocal_bind() {
|
||||
let address = "0.0.0.0:0".parse().unwrap();
|
||||
assert_eq!(
|
||||
bind_local(address).unwrap_err().kind(),
|
||||
io::ErrorKind::InvalidInput
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_only_observed_share_counts() {
|
||||
let status = MiningStatus::new();
|
||||
status.set_running(true);
|
||||
status.share_accepted();
|
||||
status.share_rejected();
|
||||
let reply = status.response("summary");
|
||||
assert_eq!(reply["SUMMARY"][0]["Accepted"], 1);
|
||||
assert_eq!(reply["SUMMARY"][0]["Rejected"], 1);
|
||||
assert_eq!(reply["SUMMARY"][0]["Status"], "Mining");
|
||||
assert!(reply["SUMMARY"][0].get("MHS av").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_socket_uses_nul_terminated_cgminer_envelope() {
|
||||
let listener = bind_local("127.0.0.1:0".parse().unwrap()).unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let status = Arc::new(MiningStatus::new());
|
||||
let worker = std::thread::spawn(move || {
|
||||
let (stream, _) = listener.accept().unwrap();
|
||||
handle(stream, &status).unwrap();
|
||||
});
|
||||
let mut client = TcpStream::connect(address).unwrap();
|
||||
client.write_all(b"{\"command\":\"summary\"}\n").unwrap();
|
||||
let mut reply = Vec::new();
|
||||
client.read_to_end(&mut reply).unwrap();
|
||||
assert_eq!(reply.pop(), Some(0));
|
||||
let parsed: Value = serde_json::from_slice(&reply).unwrap();
|
||||
assert_eq!(parsed["STATUS"][0]["STATUS"], "S");
|
||||
assert_eq!(parsed["SUMMARY"][0]["Status"], "Stopped");
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fragmented_plain_command_is_not_answered_early() {
|
||||
let listener = bind_local("127.0.0.1:0".parse().unwrap()).unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let status = Arc::new(MiningStatus::new());
|
||||
let worker = std::thread::spawn(move || {
|
||||
let (stream, _) = listener.accept().unwrap();
|
||||
handle(stream, &status).unwrap();
|
||||
});
|
||||
let mut client = TcpStream::connect(address).unwrap();
|
||||
client.write_all(b"sum").unwrap();
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
client.write_all(b"mary").unwrap();
|
||||
let mut reply = Vec::new();
|
||||
client.read_to_end(&mut reply).unwrap();
|
||||
assert_eq!(reply.pop(), Some(0));
|
||||
let parsed: Value = serde_json::from_slice(&reply).unwrap();
|
||||
assert_eq!(parsed["STATUS"][0]["STATUS"], "S");
|
||||
assert!(parsed.get("SUMMARY").is_some());
|
||||
worker.join().unwrap();
|
||||
}
|
||||
}
|
||||
+327
@@ -0,0 +1,327 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! One-chain BM13xx mining control built on the SDK hardware backend.
|
||||
//! This module never searches for nonces or chooses board safety thresholds.
|
||||
|
||||
use crate::bm1362;
|
||||
use crate::bm1366::{decode_nonce_report, encode_work, ReportError, REPORT_BYTES};
|
||||
use crate::pow::verify_sha256_report;
|
||||
use crate::{power_decision, HardwareBackend, PowerDecision};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ChipModel {
|
||||
Bm1362,
|
||||
Bm1366,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum MinerError<E> {
|
||||
Backend(E),
|
||||
AlreadyRunning,
|
||||
NotRunning,
|
||||
Faulted,
|
||||
WorkIdInUse(u8),
|
||||
UnsafeTemperature(i32),
|
||||
InvalidWork,
|
||||
InvalidReport(ReportError),
|
||||
StaleReport,
|
||||
BelowTarget,
|
||||
}
|
||||
|
||||
impl<E: fmt::Display> fmt::Display for MinerError<E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Backend(error) => write!(f, "hardware backend: {error}"),
|
||||
Self::AlreadyRunning => f.write_str("miner is already running"),
|
||||
Self::NotRunning => f.write_str("miner is not running"),
|
||||
Self::Faulted => f.write_str("miner shutdown is unconfirmed"),
|
||||
Self::WorkIdInUse(id) => write!(f, "BM13xx work ID {id} is still in use"),
|
||||
Self::UnsafeTemperature(value) => write!(f, "unsafe temperature: {value} mC"),
|
||||
Self::InvalidWork => f.write_str("invalid BM13xx work"),
|
||||
Self::InvalidReport(error) => write!(f, "invalid BM13xx report: {error:?}"),
|
||||
Self::StaleReport => f.write_str("stale BM13xx work ID"),
|
||||
Self::BelowTarget => f.write_str("nonce is below the pool target"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: std::error::Error + 'static> std::error::Error for MinerError<E> {}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct ValidShare {
|
||||
pub nonce: u32,
|
||||
pub work_id: u8,
|
||||
pub rolled_version: u32,
|
||||
pub hash_le: [u8; 32],
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Work {
|
||||
header: [u8; 80],
|
||||
target_le: [u8; 32],
|
||||
id: u8,
|
||||
}
|
||||
|
||||
pub struct Bm1366Miner<'a, B: HardwareBackend> {
|
||||
backend: &'a mut B,
|
||||
chip: ChipModel,
|
||||
chain: u8,
|
||||
sensor: u8,
|
||||
fan: u8,
|
||||
fan_percent: u8,
|
||||
max_temperature_mc: i32,
|
||||
running: bool,
|
||||
faulted: bool,
|
||||
work: [Option<Work>; 32],
|
||||
used_work_ids: [bool; 32],
|
||||
}
|
||||
|
||||
impl<'a, B: HardwareBackend> Bm1366Miner<'a, B> {
|
||||
/// The caller supplies a threshold validated for this board revision.
|
||||
pub fn new(
|
||||
backend: &'a mut B,
|
||||
chain: u8,
|
||||
sensor: u8,
|
||||
fan: u8,
|
||||
fan_percent: u8,
|
||||
max_temperature_mc: i32,
|
||||
) -> Option<Self> {
|
||||
Self::new_for_chip(
|
||||
backend,
|
||||
ChipModel::Bm1366,
|
||||
chain,
|
||||
sensor,
|
||||
fan,
|
||||
fan_percent,
|
||||
max_temperature_mc,
|
||||
)
|
||||
}
|
||||
|
||||
/// Select the captured BM1362 or BM1366 work format explicitly.
|
||||
pub fn new_for_chip(
|
||||
backend: &'a mut B,
|
||||
chip: ChipModel,
|
||||
chain: u8,
|
||||
sensor: u8,
|
||||
fan: u8,
|
||||
fan_percent: u8,
|
||||
max_temperature_mc: i32,
|
||||
) -> Option<Self> {
|
||||
if fan_percent == 0 || fan_percent > 100 || max_temperature_mc <= 0 {
|
||||
return None;
|
||||
}
|
||||
Some(Self {
|
||||
backend,
|
||||
chip,
|
||||
chain,
|
||||
sensor,
|
||||
fan,
|
||||
fan_percent,
|
||||
max_temperature_mc,
|
||||
running: false,
|
||||
faulted: false,
|
||||
work: [None; 32],
|
||||
used_work_ids: [false; 32],
|
||||
})
|
||||
}
|
||||
|
||||
fn power_off(&mut self) -> Result<(), B::Error> {
|
||||
self.work = [None; 32];
|
||||
match self.backend.set_psu_enabled(false) {
|
||||
Ok(()) => {
|
||||
self.running = false;
|
||||
self.faulted = false;
|
||||
self.used_work_ids = [false; 32];
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => {
|
||||
// The backend may have left power on. Drop retries shutdown.
|
||||
self.running = true;
|
||||
self.faulted = true;
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_temperature(&mut self) -> Result<(), MinerError<B::Error>> {
|
||||
let value = match self.backend.read_temperature_mc(self.sensor) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
};
|
||||
if power_decision(Some(value), self.max_temperature_mc) == PowerDecision::Stop {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::UnsafeTemperature(value));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn start(&mut self) -> Result<(), MinerError<B::Error>> {
|
||||
if self.running {
|
||||
return Err(MinerError::AlreadyRunning);
|
||||
}
|
||||
self.power_off().map_err(MinerError::Backend)?;
|
||||
self.check_temperature()?;
|
||||
self.backend
|
||||
.set_fan_target(self.fan, self.fan_percent)
|
||||
.map_err(MinerError::Backend)?;
|
||||
if let Err(error) = self.backend.set_psu_enabled(true) {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
self.running = true;
|
||||
if let Err(error) = self.backend.set_chain_reset(self.chain, true) {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
if let Err(error) = self.backend.set_chain_reset(self.chain, false) {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn stop(&mut self) -> Result<(), B::Error> {
|
||||
self.power_off()
|
||||
}
|
||||
|
||||
/// Flush the ASIC's in-flight work before reusing any work IDs. A reset
|
||||
/// failure shuts the PSU down because old reports may still arrive.
|
||||
pub fn reset_chain(&mut self) -> Result<(), MinerError<B::Error>> {
|
||||
if !self.running {
|
||||
return Err(MinerError::NotRunning);
|
||||
}
|
||||
if self.faulted {
|
||||
return Err(MinerError::Faulted);
|
||||
}
|
||||
self.work = [None; 32];
|
||||
self.check_temperature()?;
|
||||
if let Err(error) = self.backend.set_chain_reset(self.chain, true) {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
if let Err(error) = self.backend.set_chain_reset(self.chain, false) {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
self.used_work_ids = [false; 32];
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn submit(
|
||||
&mut self,
|
||||
header: [u8; 80],
|
||||
target_le: [u8; 32],
|
||||
work_id: u8,
|
||||
) -> Result<(), MinerError<B::Error>> {
|
||||
if !self.running {
|
||||
return Err(MinerError::NotRunning);
|
||||
}
|
||||
if self.faulted {
|
||||
return Err(MinerError::Faulted);
|
||||
}
|
||||
self.check_temperature()?;
|
||||
let frame = match self.chip {
|
||||
ChipModel::Bm1362 => bm1362::encode_work(&header, work_id),
|
||||
ChipModel::Bm1366 => encode_work(&header, work_id),
|
||||
}
|
||||
.ok_or(MinerError::InvalidWork)?;
|
||||
let slot = usize::from(work_id >> 3);
|
||||
if self.used_work_ids[slot] {
|
||||
return Err(MinerError::WorkIdInUse(work_id));
|
||||
}
|
||||
// A failed send may still have reached the ASIC, so reserve first.
|
||||
self.used_work_ids[slot] = true;
|
||||
if let Err(error) = self.backend.send_frame(self.chain, &frame) {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
self.work[slot] = Some(Work {
|
||||
header,
|
||||
target_le,
|
||||
id: work_id,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Forget prior pool jobs after `clean_jobs`, while leaving the ASIC powered.
|
||||
/// Reports already in flight are then classified as stale. Their work IDs
|
||||
/// remain reserved until a confirmed chain reset or PSU shutdown.
|
||||
pub fn clear_work(&mut self) {
|
||||
self.work = [None; 32];
|
||||
}
|
||||
|
||||
pub fn poll(&mut self) -> Result<Option<ValidShare>, MinerError<B::Error>> {
|
||||
if !self.running {
|
||||
return Err(MinerError::NotRunning);
|
||||
}
|
||||
if self.faulted {
|
||||
return Err(MinerError::Faulted);
|
||||
}
|
||||
self.check_temperature()?;
|
||||
let mut frame = [0_u8; REPORT_BYTES];
|
||||
let length = match self.backend.receive_frame(self.chain, &mut frame) {
|
||||
Ok(length) => length,
|
||||
Err(error) => {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::Backend(error));
|
||||
}
|
||||
};
|
||||
if length == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let report = match decode_nonce_report(&frame[..length.min(REPORT_BYTES)]) {
|
||||
Ok(report) if length == REPORT_BYTES => report,
|
||||
Ok(_) => {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::InvalidReport(ReportError::Length));
|
||||
}
|
||||
Err(error) => {
|
||||
let _ = self.power_off();
|
||||
return Err(MinerError::InvalidReport(error));
|
||||
}
|
||||
};
|
||||
let Some(work) = self.work[usize::from(report.work_id >> 3)] else {
|
||||
return Err(MinerError::StaleReport);
|
||||
};
|
||||
if report.work_id != work.id {
|
||||
return Err(MinerError::StaleReport);
|
||||
}
|
||||
let check = verify_sha256_report(
|
||||
&work.header,
|
||||
report.nonce,
|
||||
report.version_field,
|
||||
&work.target_le,
|
||||
);
|
||||
if !check.meets_target {
|
||||
return Err(MinerError::BelowTarget);
|
||||
}
|
||||
Ok(Some(ValidShare {
|
||||
nonce: report.nonce,
|
||||
work_id: report.work_id,
|
||||
rolled_version: check.rolled_version,
|
||||
hash_le: check.hash_le,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Keep thermal supervision active while pool share replies are pending.
|
||||
pub fn supervise(&mut self) -> Result<(), MinerError<B::Error>> {
|
||||
if !self.running {
|
||||
return Err(MinerError::NotRunning);
|
||||
}
|
||||
if self.faulted {
|
||||
return Err(MinerError::Faulted);
|
||||
}
|
||||
self.check_temperature()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: HardwareBackend> Drop for Bm1366Miner<'_, B> {
|
||||
fn drop(&mut self) {
|
||||
if self.running {
|
||||
let _ = self.power_off();
|
||||
}
|
||||
}
|
||||
}
|
||||
+238
@@ -0,0 +1,238 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Verify reported SHA-256d shares. This module does not search for nonces.
|
||||
|
||||
const BM1366_VERSION_BITS: u32 = 0x1FFF_E000;
|
||||
|
||||
const ROUND: [u32; 64] = [
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
||||
];
|
||||
|
||||
const INITIAL: [u32; 8] = [
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
||||
];
|
||||
|
||||
fn compress(state: &mut [u32; 8], block: &[u8]) {
|
||||
let mut words = [0_u32; 64];
|
||||
for (index, part) in block.chunks_exact(4).enumerate() {
|
||||
words[index] = u32::from_be_bytes(part.try_into().expect("four-byte word"));
|
||||
}
|
||||
for index in 16..64 {
|
||||
let a = words[index - 15];
|
||||
let b = words[index - 2];
|
||||
let small_zero = a.rotate_right(7) ^ a.rotate_right(18) ^ (a >> 3);
|
||||
let small_one = b.rotate_right(17) ^ b.rotate_right(19) ^ (b >> 10);
|
||||
words[index] = words[index - 16]
|
||||
.wrapping_add(small_zero)
|
||||
.wrapping_add(words[index - 7])
|
||||
.wrapping_add(small_one);
|
||||
}
|
||||
|
||||
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
|
||||
for index in 0..64 {
|
||||
let choose = (e & f) ^ (!e & g);
|
||||
let majority = (a & b) ^ (a & c) ^ (b & c);
|
||||
let big_one = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25);
|
||||
let big_zero = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22);
|
||||
let first = h
|
||||
.wrapping_add(big_one)
|
||||
.wrapping_add(choose)
|
||||
.wrapping_add(ROUND[index])
|
||||
.wrapping_add(words[index]);
|
||||
let second = big_zero.wrapping_add(majority);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d.wrapping_add(first);
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = first.wrapping_add(second);
|
||||
}
|
||||
for (current, result) in state.iter_mut().zip([a, b, c, d, e, f, g, h]) {
|
||||
*current = current.wrapping_add(result);
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute SHA-256 for a message whose length fits the standard 64-bit field.
|
||||
pub fn sha256(message: &[u8]) -> [u8; 32] {
|
||||
let mut state = INITIAL;
|
||||
let chunks = message.chunks_exact(64);
|
||||
let remainder = chunks.remainder();
|
||||
for chunk in chunks {
|
||||
compress(&mut state, chunk);
|
||||
}
|
||||
let final_size = if remainder.len() >= 56 { 128 } else { 64 };
|
||||
let mut final_blocks = [0_u8; 128];
|
||||
final_blocks[..remainder.len()].copy_from_slice(remainder);
|
||||
final_blocks[remainder.len()] = 0x80;
|
||||
final_blocks[final_size - 8..final_size]
|
||||
.copy_from_slice(&((message.len() as u64).wrapping_mul(8)).to_be_bytes());
|
||||
for chunk in final_blocks[..final_size].chunks_exact(64) {
|
||||
compress(&mut state, chunk);
|
||||
}
|
||||
let mut digest = [0_u8; 32];
|
||||
for (word, bytes) in state.into_iter().zip(digest.chunks_exact_mut(4)) {
|
||||
bytes.copy_from_slice(&word.to_be_bytes());
|
||||
}
|
||||
digest
|
||||
}
|
||||
|
||||
/// Compute one double hash for verification, never for nonce search.
|
||||
pub fn sha256d(message: &[u8]) -> [u8; 32] {
|
||||
sha256(&sha256(message))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct ShareCheck {
|
||||
pub rolled_version: u32,
|
||||
pub hash_le: [u8; 32],
|
||||
pub meets_target: bool,
|
||||
}
|
||||
|
||||
/// Decode the raw BM1366 version extension against the version sent to the ASIC.
|
||||
/// The chip reports additional set bits; pool submission uses a separate mask.
|
||||
pub fn decode_bm1366_version(job_version: u32, version_field: u16) -> u32 {
|
||||
job_version | ((u32::from(version_field) << 13) & BM1366_VERSION_BITS)
|
||||
}
|
||||
|
||||
/// The BIP310 mask negotiated with a pool, limited to BM1366 rolling bits.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct VersionMask(u32);
|
||||
|
||||
impl VersionMask {
|
||||
pub fn new(mask: u32) -> Option<Self> {
|
||||
(mask & !BM1366_VERSION_BITS == 0).then_some(Self(mask))
|
||||
}
|
||||
|
||||
pub fn apply_submission_bits(self, base_version: u32, version_bits: u32) -> Option<u32> {
|
||||
(version_bits & !self.0 == 0).then_some((base_version & !self.0) | version_bits)
|
||||
}
|
||||
|
||||
pub fn bits(self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Check one ASIC-reported nonce against a little-endian 256-bit pool target.
|
||||
pub fn verify_sha256_report(
|
||||
nonce_zero_header: &[u8; 80],
|
||||
nonce: u32,
|
||||
version_field: u16,
|
||||
target_le: &[u8; 32],
|
||||
) -> ShareCheck {
|
||||
let mut header = *nonce_zero_header;
|
||||
let base_version = u32::from_le_bytes(header[..4].try_into().expect("version field"));
|
||||
let rolled_version = decode_bm1366_version(base_version, version_field);
|
||||
header[..4].copy_from_slice(&rolled_version.to_le_bytes());
|
||||
header[76..80].copy_from_slice(&nonce.to_le_bytes());
|
||||
let hash_le = sha256d(&header);
|
||||
let meets_target = hash_le.iter().rev().cmp(target_le.iter().rev()).is_le();
|
||||
ShareCheck {
|
||||
rolled_version,
|
||||
hash_le,
|
||||
meets_target,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|byte| format!("{byte:02x}")).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sha256_matches_standard_padding_vectors() {
|
||||
let vectors: [(&[u8], &str); 6] = [
|
||||
(
|
||||
b"",
|
||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
||||
),
|
||||
(
|
||||
b"abc",
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
||||
),
|
||||
(
|
||||
&(0_u8..55).collect::<Vec<_>>(),
|
||||
"463eb28e72f82e0a96c0a4cc53690c571281131f672aa229e0d45ae59b598b59",
|
||||
),
|
||||
(
|
||||
&(0_u8..56).collect::<Vec<_>>(),
|
||||
"da2ae4d6b36748f2a318f23e7ab1dfdf45acdc9d049bd80e59de82a60895f562",
|
||||
),
|
||||
(
|
||||
&(0_u8..64).collect::<Vec<_>>(),
|
||||
"fdeab9acf3710362bd2658cdc9a29e8f9c757fcf9811603a8c447cd1d9151108",
|
||||
),
|
||||
(
|
||||
&(0_u8..100).collect::<Vec<_>>(),
|
||||
"bce0aff19cf5aa6a7469a30d61d04e4376e4bbf6381052ee9e7f33925c954d52",
|
||||
),
|
||||
];
|
||||
for (message, expected) in vectors {
|
||||
assert_eq!(hex(&sha256(message)), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_bm1366_report_meets_diff_one_only_with_rolled_version() {
|
||||
let header = [
|
||||
0x00, 0x00, 0x00, 0x20, 0xfc, 0x3a, 0xb8, 0xd4, 0x30, 0x21, 0xf4, 0x5e, 0xb6, 0x62,
|
||||
0x38, 0x9d, 0x0f, 0x79, 0x1f, 0x65, 0x7d, 0xbf, 0x75, 0xa5, 0xcb, 0x93, 0x67, 0x0b,
|
||||
0x57, 0xb6, 0xc2, 0x25, 0x03, 0xb4, 0x21, 0x3a, 0x43, 0xae, 0x10, 0x52, 0xeb, 0xd4,
|
||||
0x44, 0x43, 0x06, 0x8f, 0xe1, 0x78, 0xe2, 0xc6, 0x1c, 0xa4, 0x99, 0x48, 0x46, 0xfd,
|
||||
0x13, 0x3a, 0xdd, 0xb0, 0x4a, 0xab, 0x2b, 0x38, 0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01,
|
||||
0x2e, 0x6a, 0x8f, 0x06, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let mut diff_one = [0_u8; 32];
|
||||
diff_one[26..28].fill(0xff);
|
||||
let result = verify_sha256_report(&header, 0xEDAD_FF38, 0x04B3, &diff_one);
|
||||
assert_eq!(result.rolled_version, 0x2096_6000);
|
||||
assert_eq!(
|
||||
hex(&result.hash_le),
|
||||
"4b5e00850cf9de9809f822f8858eda39e49996d9d40f634f1043290000000000"
|
||||
);
|
||||
assert!(result.meets_target);
|
||||
assert!(!verify_sha256_report(&header, 0xEDAD_FF38, 0, &diff_one).meets_target);
|
||||
assert!(!verify_sha256_report(&header, 0xEDAD_FF39, 0x04B3, &diff_one).meets_target);
|
||||
assert!(verify_sha256_report(&header, 0xEDAD_FF38, 0x04B3, &[0xff; 32]).meets_target);
|
||||
assert!(!verify_sha256_report(&header, 0xEDAD_FF38, 0x04B3, &[0; 32]).meets_target);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_asic_version_preserves_bits_sent_to_chip() {
|
||||
assert_eq!(decode_bm1366_version(0x2100_0004, 1), 0x2100_2004);
|
||||
assert_eq!(decode_bm1366_version(0x2000_0000, 0), 0x2000_0000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pool_version_bits_replace_only_negotiated_mask() {
|
||||
let mask = VersionMask::new(0x0060_0000).expect("valid restricted mask");
|
||||
assert_eq!(
|
||||
mask.apply_submission_bits(0x2160_0004, 0x0020_0000),
|
||||
Some(0x2120_0004)
|
||||
);
|
||||
assert_eq!(mask.apply_submission_bits(0x2160_0004, 0x0080_0000), None);
|
||||
assert_eq!(
|
||||
VersionMask::new(0)
|
||||
.unwrap()
|
||||
.apply_submission_bits(0x2100_0004, 0),
|
||||
Some(0x2100_0004)
|
||||
);
|
||||
assert_eq!(
|
||||
VersionMask::new(BM1366_VERSION_BITS)
|
||||
.unwrap()
|
||||
.apply_submission_bits(0x2160_0004, 0),
|
||||
Some(0x2000_0004)
|
||||
);
|
||||
assert!(VersionMask::new(0x8000_0000).is_none());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Litecoin's fixed scrypt proof-of-work hash for validating reported shares.
|
||||
//! This module hashes one supplied header and never searches for a nonce.
|
||||
|
||||
use crate::pow::sha256;
|
||||
|
||||
const N: usize = 1024;
|
||||
|
||||
fn hmac_sha256(key: &[u8], message: &[u8]) -> [u8; 32] {
|
||||
let mut block = [0_u8; 64];
|
||||
if key.len() > block.len() {
|
||||
block[..32].copy_from_slice(&sha256(key));
|
||||
} else {
|
||||
block[..key.len()].copy_from_slice(key);
|
||||
}
|
||||
let mut inner = Vec::with_capacity(64 + message.len());
|
||||
inner.extend(block.iter().map(|byte| byte ^ 0x36));
|
||||
inner.extend_from_slice(message);
|
||||
let inner_hash = sha256(&inner);
|
||||
let mut outer = [0_u8; 96];
|
||||
for (destination, byte) in outer[..64].iter_mut().zip(block) {
|
||||
*destination = byte ^ 0x5c;
|
||||
}
|
||||
outer[64..].copy_from_slice(&inner_hash);
|
||||
sha256(&outer)
|
||||
}
|
||||
|
||||
// PBKDF2-HMAC-SHA256 with the fixed single iteration specified for scrypt.
|
||||
fn pbkdf2_one(password: &[u8], salt: &[u8], output: &mut [u8]) {
|
||||
let mut message = Vec::with_capacity(salt.len() + 4);
|
||||
message.extend_from_slice(salt);
|
||||
message.extend_from_slice(&[0_u8; 4]);
|
||||
for (index, chunk) in output.chunks_mut(32).enumerate() {
|
||||
let block = u32::try_from(index + 1).expect("fixed scrypt output fits u32");
|
||||
message[salt.len()..].copy_from_slice(&block.to_be_bytes());
|
||||
chunk.copy_from_slice(&hmac_sha256(password, &message)[..chunk.len()]);
|
||||
}
|
||||
}
|
||||
|
||||
fn salsa20_8(input: &mut [u32; 16]) {
|
||||
let original = *input;
|
||||
let mut x = original;
|
||||
for _ in 0..4 {
|
||||
x[4] ^= x[0].wrapping_add(x[12]).rotate_left(7);
|
||||
x[8] ^= x[4].wrapping_add(x[0]).rotate_left(9);
|
||||
x[12] ^= x[8].wrapping_add(x[4]).rotate_left(13);
|
||||
x[0] ^= x[12].wrapping_add(x[8]).rotate_left(18);
|
||||
x[9] ^= x[5].wrapping_add(x[1]).rotate_left(7);
|
||||
x[13] ^= x[9].wrapping_add(x[5]).rotate_left(9);
|
||||
x[1] ^= x[13].wrapping_add(x[9]).rotate_left(13);
|
||||
x[5] ^= x[1].wrapping_add(x[13]).rotate_left(18);
|
||||
x[14] ^= x[10].wrapping_add(x[6]).rotate_left(7);
|
||||
x[2] ^= x[14].wrapping_add(x[10]).rotate_left(9);
|
||||
x[6] ^= x[2].wrapping_add(x[14]).rotate_left(13);
|
||||
x[10] ^= x[6].wrapping_add(x[2]).rotate_left(18);
|
||||
x[3] ^= x[15].wrapping_add(x[11]).rotate_left(7);
|
||||
x[7] ^= x[3].wrapping_add(x[15]).rotate_left(9);
|
||||
x[11] ^= x[7].wrapping_add(x[3]).rotate_left(13);
|
||||
x[15] ^= x[11].wrapping_add(x[7]).rotate_left(18);
|
||||
|
||||
x[1] ^= x[0].wrapping_add(x[3]).rotate_left(7);
|
||||
x[2] ^= x[1].wrapping_add(x[0]).rotate_left(9);
|
||||
x[3] ^= x[2].wrapping_add(x[1]).rotate_left(13);
|
||||
x[0] ^= x[3].wrapping_add(x[2]).rotate_left(18);
|
||||
x[6] ^= x[5].wrapping_add(x[4]).rotate_left(7);
|
||||
x[7] ^= x[6].wrapping_add(x[5]).rotate_left(9);
|
||||
x[4] ^= x[7].wrapping_add(x[6]).rotate_left(13);
|
||||
x[5] ^= x[4].wrapping_add(x[7]).rotate_left(18);
|
||||
x[11] ^= x[10].wrapping_add(x[9]).rotate_left(7);
|
||||
x[8] ^= x[11].wrapping_add(x[10]).rotate_left(9);
|
||||
x[9] ^= x[8].wrapping_add(x[11]).rotate_left(13);
|
||||
x[10] ^= x[9].wrapping_add(x[8]).rotate_left(18);
|
||||
x[12] ^= x[15].wrapping_add(x[14]).rotate_left(7);
|
||||
x[13] ^= x[12].wrapping_add(x[15]).rotate_left(9);
|
||||
x[14] ^= x[13].wrapping_add(x[12]).rotate_left(13);
|
||||
x[15] ^= x[14].wrapping_add(x[13]).rotate_left(18);
|
||||
}
|
||||
for ((word, initial), mixed) in input.iter_mut().zip(original).zip(x) {
|
||||
*word = initial.wrapping_add(mixed);
|
||||
}
|
||||
}
|
||||
|
||||
fn block_mix(input: &[u32; 32]) -> [u32; 32] {
|
||||
let mut x: [u32; 16] = input[16..].try_into().expect("fixed block size");
|
||||
let mut output = [0_u32; 32];
|
||||
for (index, chunk) in input.chunks_exact(16).enumerate() {
|
||||
for (word, source) in x.iter_mut().zip(chunk) {
|
||||
*word ^= source;
|
||||
}
|
||||
salsa20_8(&mut x);
|
||||
output[index * 16..(index + 1) * 16].copy_from_slice(&x);
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// Hash one 80-byte Litecoin header with scrypt(N=1024, r=1, p=1, dkLen=32).
|
||||
/// The return value is the raw little-endian PoW digest.
|
||||
pub fn scrypt_1024_1_1_256(header: &[u8; 80]) -> [u8; 32] {
|
||||
let mut initial = [0_u8; 128];
|
||||
pbkdf2_one(header, header, &mut initial);
|
||||
let mut x = [0_u32; 32];
|
||||
for (word, bytes) in x.iter_mut().zip(initial.chunks_exact(4)) {
|
||||
*word = u32::from_le_bytes(bytes.try_into().expect("four-byte word"));
|
||||
}
|
||||
let mut memory = vec![[0_u32; 32]; N];
|
||||
for saved in &mut memory {
|
||||
*saved = x;
|
||||
x = block_mix(&x);
|
||||
}
|
||||
for _ in 0..N {
|
||||
let index = x[16] as usize & (N - 1);
|
||||
for (word, saved) in x.iter_mut().zip(memory[index]) {
|
||||
*word ^= saved;
|
||||
}
|
||||
x = block_mix(&x);
|
||||
}
|
||||
for (word, bytes) in x.iter().zip(initial.chunks_exact_mut(4)) {
|
||||
bytes.copy_from_slice(&word.to_le_bytes());
|
||||
}
|
||||
let mut digest = [0_u8; 32];
|
||||
pbkdf2_one(header, &initial, &mut digest);
|
||||
digest
|
||||
}
|
||||
|
||||
/// Validate one supplied header against an exact little-endian pool target.
|
||||
pub fn verify_scrypt_header(header: &[u8; 80], target_le: &[u8; 32]) -> ([u8; 32], bool) {
|
||||
let digest = scrypt_1024_1_1_256(header);
|
||||
let meets_target = digest.iter().rev().cmp(target_le.iter().rev()).is_le();
|
||||
(digest, meets_target)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn litecoin_block_29255_matches_published_scrypt_pow() {
|
||||
let header_hex = concat!(
|
||||
"01000000f615f7ce3b4fc6b8f61e8f89aedb1d0852507650533a9e3b10b9bbcc30639f27",
|
||||
"9fcaa86746e1ef52d3edb3c4ad8259920d509bd073605c9bf1d59983752a6b06b817bb4",
|
||||
"ea78e011d012d59d4"
|
||||
);
|
||||
let mut header = [0_u8; 80];
|
||||
for (byte, pair) in header.iter_mut().zip(header_hex.as_bytes().chunks_exact(2)) {
|
||||
*byte = u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap();
|
||||
}
|
||||
let expected_big = "0000000110c8357966576df46f3b802ca897deb7ad18b12f1c24ecff6386ebd9";
|
||||
let mut expected = [0_u8; 32];
|
||||
for (byte, pair) in expected
|
||||
.iter_mut()
|
||||
.rev()
|
||||
.zip(expected_big.as_bytes().chunks_exact(2))
|
||||
{
|
||||
*byte = u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap();
|
||||
}
|
||||
let (digest, accepted) = verify_scrypt_header(&header, &expected);
|
||||
assert_eq!(digest, expected);
|
||||
assert!(accepted);
|
||||
assert!(!verify_scrypt_header(&header, &[0_u8; 32]).1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixed_header_matches_independent_openssl_scrypt() {
|
||||
let header = core::array::from_fn(|index| index as u8);
|
||||
let expected = "bc540a1a801df96e493005c71e010e2d387607fbf0fec416fd3c2645aa1ba9d2";
|
||||
let actual = scrypt_1024_1_1_256(&header);
|
||||
let actual_hex: String = actual.iter().map(|byte| format!("{byte:02x}")).collect();
|
||||
assert_eq!(actual_hex, expected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! One bounded pool connection with ordered jobs and verified share replies.
|
||||
|
||||
use crate::miner::{Bm1366Miner, MinerError, ValidShare};
|
||||
use crate::stratum_session::{DispatchError, IssuedWork, PoolSession, SessionError};
|
||||
use crate::stratum_v1::{parse_boolean_response, PoolMessageError};
|
||||
use crate::stratum_wire::{HandshakeError, PoolHandshake, StratumWire, WireError};
|
||||
use crate::HardwareBackend;
|
||||
use serde_json::Value;
|
||||
use std::collections::VecDeque;
|
||||
use std::net::SocketAddr;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const MAX_PENDING_SHARES: usize = 32;
|
||||
const SHARE_REPLY_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
const MAX_REPORTS_PER_CYCLE: usize = 256;
|
||||
const POOL_WRITE_DEADLINE: Duration = Duration::from_millis(100);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ClientError<E> {
|
||||
Wire(WireError),
|
||||
Pool(PoolMessageError),
|
||||
Session(SessionError),
|
||||
Dispatch(DispatchError<E>),
|
||||
UnexpectedResponse,
|
||||
TooManyPending,
|
||||
RequestIdsExhausted,
|
||||
ShareReplyTimeout,
|
||||
EmergencyShutdown(E),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PoolEvent {
|
||||
Idle,
|
||||
Control,
|
||||
Work(IssuedWork),
|
||||
ShareAccepted(u64),
|
||||
ShareRejected(u64),
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum ReportEvent {
|
||||
Stale,
|
||||
BelowTarget,
|
||||
Submitted(u64),
|
||||
Backpressured,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CycleError<E> {
|
||||
Pool(ClientError<E>),
|
||||
Miner(MinerError<E>),
|
||||
Submit(ClientError<()>),
|
||||
EmergencyShutdown(E),
|
||||
}
|
||||
|
||||
pub struct PoolConnection {
|
||||
wire: StratumWire,
|
||||
session: PoolSession,
|
||||
worker: String,
|
||||
early: VecDeque<Vec<u8>>,
|
||||
next_request_id: u64,
|
||||
pending: Vec<PendingShare>,
|
||||
accepted: u64,
|
||||
rejected: u64,
|
||||
}
|
||||
|
||||
struct PendingShare {
|
||||
id: u64,
|
||||
sent_at: Instant,
|
||||
}
|
||||
|
||||
impl PoolConnection {
|
||||
pub fn connect(
|
||||
address: SocketAddr,
|
||||
agent: &str,
|
||||
worker: &str,
|
||||
password: &str,
|
||||
io_timeout: Duration,
|
||||
handshake_timeout: Duration,
|
||||
) -> Result<Self, HandshakeError> {
|
||||
let mut wire = StratumWire::connect(address, io_timeout).map_err(HandshakeError::Wire)?;
|
||||
let handshake = wire.handshake(agent, worker, password, handshake_timeout)?;
|
||||
Self::from_handshake(wire, handshake, worker)
|
||||
.map_err(|_| HandshakeError::Pool(PoolMessageError::InvalidField))
|
||||
}
|
||||
|
||||
fn from_handshake(
|
||||
mut wire: StratumWire,
|
||||
handshake: PoolHandshake,
|
||||
worker: &str,
|
||||
) -> Result<Self, SessionError> {
|
||||
let session = PoolSession::new(
|
||||
handshake.subscription,
|
||||
handshake.configure.version_mask,
|
||||
handshake.configure.subscribe_extranonce,
|
||||
)?;
|
||||
wire.cap_write_timeout(POOL_WRITE_DEADLINE);
|
||||
Ok(Self {
|
||||
wire,
|
||||
session,
|
||||
worker: worker.to_owned(),
|
||||
early: handshake.early_notifications.into(),
|
||||
next_request_id: 4,
|
||||
pending: Vec::new(),
|
||||
accepted: 0,
|
||||
rejected: 0,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn step<B: HardwareBackend>(
|
||||
&mut self,
|
||||
miner: &mut Bm1366Miner<'_, B>,
|
||||
) -> Result<PoolEvent, ClientError<B::Error>> {
|
||||
let result = self.step_inner(miner);
|
||||
if result.is_err() {
|
||||
self.wire.close();
|
||||
if let Err(error) = miner.stop() {
|
||||
return Err(ClientError::EmergencyShutdown(error));
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn step_inner<B: HardwareBackend>(
|
||||
&mut self,
|
||||
miner: &mut Bm1366Miner<'_, B>,
|
||||
) -> Result<PoolEvent, ClientError<B::Error>> {
|
||||
if self
|
||||
.pending
|
||||
.first()
|
||||
.is_some_and(|share| share.sent_at.elapsed() >= SHARE_REPLY_TIMEOUT)
|
||||
{
|
||||
return Err(ClientError::ShareReplyTimeout);
|
||||
}
|
||||
let line = if let Some(line) = self.early.pop_front() {
|
||||
line
|
||||
} else {
|
||||
match self.wire.try_read_line() {
|
||||
Ok(Some(line)) => line,
|
||||
Ok(None) => return Ok(PoolEvent::Idle),
|
||||
Err(error) => return Err(ClientError::Wire(error)),
|
||||
}
|
||||
};
|
||||
let message: Value =
|
||||
serde_json::from_slice(&line).map_err(|_| ClientError::Pool(PoolMessageError::Json))?;
|
||||
if let Some(id) = message.get("id").and_then(Value::as_u64) {
|
||||
if !self.pending.iter().any(|share| share.id == id) {
|
||||
return Err(ClientError::UnexpectedResponse);
|
||||
}
|
||||
let accepted = parse_boolean_response(&line, id).map_err(ClientError::Pool)?;
|
||||
self.pending.retain(|pending| pending.id != id);
|
||||
if accepted {
|
||||
self.accepted = self.accepted.saturating_add(1);
|
||||
return Ok(PoolEvent::ShareAccepted(id));
|
||||
}
|
||||
self.rejected = self.rejected.saturating_add(1);
|
||||
return Ok(PoolEvent::ShareRejected(id));
|
||||
}
|
||||
if !message.get("id").is_none_or(Value::is_null) {
|
||||
return Err(ClientError::Pool(PoolMessageError::WrongMessage));
|
||||
}
|
||||
match message.get("method").and_then(Value::as_str) {
|
||||
Some("mining.notify") => {
|
||||
let issued = match self.session.issue_to_miner(miner, &line) {
|
||||
Err(DispatchError::Session(SessionError::WorkIdsExhausted)) => {
|
||||
miner
|
||||
.reset_chain()
|
||||
.map_err(|error| ClientError::Dispatch(DispatchError::Miner(error)))?;
|
||||
self.after_hardware_reset(miner);
|
||||
self.session.issue_to_miner(miner, &line)
|
||||
}
|
||||
result => result,
|
||||
};
|
||||
issued.map(PoolEvent::Work).map_err(ClientError::Dispatch)
|
||||
}
|
||||
Some("mining.set_difficulty" | "mining.set_version_mask" | "mining.set_extranonce") => {
|
||||
self.session
|
||||
.control_with_miner(miner, &line)
|
||||
.map_err(ClientError::Session)?;
|
||||
Ok(PoolEvent::Control)
|
||||
}
|
||||
_ => Err(ClientError::Pool(PoolMessageError::WrongMessage)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Send only a nonce already verified by the miner and session. A sent
|
||||
/// request remains pending until its matching boolean pool reply arrives.
|
||||
pub fn submit_share(&mut self, share: ValidShare) -> Result<u64, ClientError<()>> {
|
||||
if self.pending.len() >= MAX_PENDING_SHARES {
|
||||
return Err(ClientError::TooManyPending);
|
||||
}
|
||||
let id = self.next_request_id;
|
||||
let next = id.checked_add(1).ok_or(ClientError::RequestIdsExhausted)?;
|
||||
let line = self
|
||||
.session
|
||||
.submission(share, &self.worker, id)
|
||||
.map_err(ClientError::Session)?;
|
||||
self.wire
|
||||
.send_line(line.as_bytes())
|
||||
.map_err(ClientError::Wire)?;
|
||||
self.next_request_id = next;
|
||||
self.pending.push(PendingShare {
|
||||
id,
|
||||
sent_at: Instant::now(),
|
||||
});
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn share_counts(&self) -> (u64, u64, usize) {
|
||||
(self.accepted, self.rejected, self.pending.len())
|
||||
}
|
||||
|
||||
/// Process one pool message, then drain a bounded burst of ASIC reports.
|
||||
/// A reported nonce is verified before sending. The caller must cycle
|
||||
/// continuously; hardware still requires an independent power watchdog.
|
||||
pub fn cycle<B: HardwareBackend>(
|
||||
&mut self,
|
||||
miner: &mut Bm1366Miner<'_, B>,
|
||||
) -> Result<(PoolEvent, Vec<ReportEvent>), CycleError<B::Error>> {
|
||||
let pool = self.step(miner).map_err(CycleError::Pool)?;
|
||||
let mut reports = Vec::new();
|
||||
for _ in 0..MAX_REPORTS_PER_CYCLE {
|
||||
if self.pending.len() >= MAX_PENDING_SHARES {
|
||||
miner.supervise().map_err(CycleError::Miner)?;
|
||||
reports.push(ReportEvent::Backpressured);
|
||||
break;
|
||||
}
|
||||
let event = match miner.poll() {
|
||||
Ok(None) => break,
|
||||
Ok(Some(share)) => {
|
||||
let id = match self.submit_share(share) {
|
||||
Ok(id) => id,
|
||||
Err(error) => {
|
||||
self.wire.close();
|
||||
miner.stop().map_err(CycleError::EmergencyShutdown)?;
|
||||
return Err(CycleError::Submit(error));
|
||||
}
|
||||
};
|
||||
ReportEvent::Submitted(id)
|
||||
}
|
||||
Err(MinerError::StaleReport) => ReportEvent::Stale,
|
||||
Err(MinerError::BelowTarget) => ReportEvent::BelowTarget,
|
||||
Err(error) => return Err(CycleError::Miner(error)),
|
||||
};
|
||||
reports.push(event);
|
||||
}
|
||||
Ok((pool, reports))
|
||||
}
|
||||
|
||||
/// Call only after an actual chain reset flushes old hardware reports.
|
||||
pub fn after_hardware_reset<B: HardwareBackend>(&mut self, miner: &mut Bm1366Miner<'_, B>) {
|
||||
miner.clear_work();
|
||||
self.session.after_hardware_reset();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::stratum_v1::Subscription;
|
||||
use std::cell::Cell;
|
||||
use std::io::Write;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::rc::Rc;
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Backend {
|
||||
sent: Vec<Vec<u8>>,
|
||||
powered: Rc<Cell<bool>>,
|
||||
reports_remaining: usize,
|
||||
reset_assertions: usize,
|
||||
}
|
||||
|
||||
impl HardwareBackend for Backend {
|
||||
type Error = ();
|
||||
|
||||
fn send_frame(&mut self, _chain: u8, frame: &[u8]) -> Result<(), Self::Error> {
|
||||
self.sent.push(frame.to_vec());
|
||||
Ok(())
|
||||
}
|
||||
fn receive_frame(&mut self, _chain: u8, frame: &mut [u8]) -> Result<usize, Self::Error> {
|
||||
if self.reports_remaining == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
self.reports_remaining -= 1;
|
||||
frame[..11].copy_from_slice(&[
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
]);
|
||||
Ok(11)
|
||||
}
|
||||
fn set_chain_reset(&mut self, _chain: u8, asserted: bool) -> Result<(), Self::Error> {
|
||||
if asserted {
|
||||
self.reset_assertions += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn set_fan_target(&mut self, _fan: u8, _percent: u8) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
fn read_temperature_mc(&mut self, _sensor: u8) -> Result<i32, Self::Error> {
|
||||
Ok(50_000)
|
||||
}
|
||||
fn set_psu_enabled(&mut self, enabled: bool) -> Result<(), Self::Error> {
|
||||
self.powered.set(enabled);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn test_link(input: &'static [u8]) -> (PoolConnection, thread::JoinHandle<()>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let (ready, received) = mpsc::channel();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
stream.write_all(input).unwrap();
|
||||
ready.send(()).unwrap();
|
||||
});
|
||||
let stream = TcpStream::connect(address).unwrap();
|
||||
let wire = StratumWire::from_stream(stream, Duration::from_secs(2)).unwrap();
|
||||
received.recv_timeout(Duration::from_secs(2)).unwrap();
|
||||
let handshake = PoolHandshake {
|
||||
subscription: Subscription {
|
||||
extranonce1: vec![1, 2, 3, 4],
|
||||
extranonce2_size: 4,
|
||||
},
|
||||
configure: crate::stratum_v1::ConfigureResult {
|
||||
version_mask: None,
|
||||
subscribe_extranonce: false,
|
||||
},
|
||||
early_notifications: Vec::new(),
|
||||
};
|
||||
(
|
||||
PoolConnection::from_handshake(wire, handshake, "miner.worker").unwrap(),
|
||||
server,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pool_controls_then_dispatches_one_synthetic_job() {
|
||||
let (mut link, server) = test_link(
|
||||
concat!(
|
||||
"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[1]}\n",
|
||||
"{\"id\":null,\"method\":\"mining.notify\",\"params\":[\"job-1\",",
|
||||
"\"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f\",",
|
||||
"\"616263\",\"78797a\",[],\"20000000\",\"1d00ffff\",\"66554433\",true]}\n"
|
||||
)
|
||||
.as_bytes(),
|
||||
);
|
||||
let mut backend = Backend::default();
|
||||
{
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
assert!(matches!(
|
||||
link.cycle(&mut miner).unwrap(),
|
||||
(PoolEvent::Control, reports) if reports.is_empty()
|
||||
));
|
||||
match link.cycle(&mut miner).unwrap() {
|
||||
(PoolEvent::Work(work), reports) if reports.is_empty() => {
|
||||
assert_eq!(work.work_id, 0)
|
||||
}
|
||||
_ => panic!("expected job"),
|
||||
}
|
||||
}
|
||||
assert_eq!(backend.sent.len(), 1);
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thirty_third_job_flushes_asic_before_reusing_work_id() {
|
||||
let (mut link, server) = test_link(b"");
|
||||
link.early.push_back(
|
||||
b"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[1]}\n".to_vec(),
|
||||
);
|
||||
let notify = concat!(
|
||||
"{\"id\":null,\"method\":\"mining.notify\",\"params\":[\"job\",",
|
||||
"\"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f\",",
|
||||
"\"616263\",\"78797a\",[],\"20000000\",\"1d00ffff\",\"66554433\",true]}\n"
|
||||
);
|
||||
for _ in 0..33 {
|
||||
link.early.push_back(notify.as_bytes().to_vec());
|
||||
}
|
||||
let mut backend = Backend::default();
|
||||
let powered = Rc::clone(&backend.powered);
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
assert!(matches!(link.step(&mut miner), Ok(PoolEvent::Control)));
|
||||
let mut first_header = None;
|
||||
for index in 0..33 {
|
||||
let PoolEvent::Work(work) = link.step(&mut miner).unwrap() else {
|
||||
panic!("expected work");
|
||||
};
|
||||
if index == 0 {
|
||||
first_header = Some(work.header);
|
||||
}
|
||||
if index == 32 {
|
||||
assert_eq!(work.work_id, 0);
|
||||
assert_ne!(Some(work.header), first_header);
|
||||
}
|
||||
}
|
||||
assert!(powered.get());
|
||||
miner.stop().unwrap();
|
||||
drop(miner);
|
||||
assert_eq!(backend.reset_assertions, 2);
|
||||
assert_eq!(backend.sent.len(), 33);
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn share_replies_match_pending_ids_and_count_accepts_separately() {
|
||||
let (mut link, server) = test_link(
|
||||
concat!(
|
||||
"{\"id\":4,\"result\":true,\"error\":null}\n",
|
||||
"{\"id\":5,\"result\":false,\"error\":[23,\"stale\"]}\n",
|
||||
"{\"id\":6,\"result\":true,\"error\":null}\n"
|
||||
)
|
||||
.as_bytes(),
|
||||
);
|
||||
link.pending.extend([4, 5].map(|id| PendingShare {
|
||||
id,
|
||||
sent_at: Instant::now(),
|
||||
}));
|
||||
let mut backend = Backend::default();
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
assert!(matches!(
|
||||
link.step(&mut miner).unwrap(),
|
||||
PoolEvent::ShareAccepted(4)
|
||||
));
|
||||
assert!(matches!(
|
||||
link.step(&mut miner).unwrap(),
|
||||
PoolEvent::ShareRejected(5)
|
||||
));
|
||||
assert_eq!(link.share_counts(), (1, 1, 0));
|
||||
assert!(matches!(
|
||||
link.step(&mut miner),
|
||||
Err(ClientError::UnexpectedResponse)
|
||||
));
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn untracked_report_cannot_be_sent_to_pool() {
|
||||
let (mut link, server) = test_link(b"");
|
||||
let report = ValidShare {
|
||||
nonce: 1,
|
||||
work_id: 0,
|
||||
rolled_version: 0x2000_0000,
|
||||
hash_le: [0; 32],
|
||||
};
|
||||
assert!(matches!(
|
||||
link.submit_share(report),
|
||||
Err(ClientError::Session(SessionError::StaleShare))
|
||||
));
|
||||
assert_eq!(link.share_counts(), (0, 0, 0));
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unanswered_share_closes_pool_connection() {
|
||||
let (mut link, server) = test_link(b"");
|
||||
link.pending.push(PendingShare {
|
||||
id: 4,
|
||||
sent_at: Instant::now() - SHARE_REPLY_TIMEOUT - Duration::from_secs(1),
|
||||
});
|
||||
let mut backend = Backend::default();
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
assert!(matches!(
|
||||
link.step(&mut miner),
|
||||
Err(ClientError::ShareReplyTimeout)
|
||||
));
|
||||
assert!(link.wire.is_closed());
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_pool_message_stops_miner_before_returning() {
|
||||
let (mut link, server) = test_link(b"{}\n");
|
||||
let mut backend = Backend::default();
|
||||
let powered = Rc::clone(&backend.powered);
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
assert!(powered.get());
|
||||
assert!(matches!(link.cycle(&mut miner), Err(CycleError::Pool(_))));
|
||||
assert!(!powered.get());
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_pool_does_not_limit_asic_report_burst_to_one() {
|
||||
let (mut link, server) =
|
||||
test_link(b"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[1]}\n");
|
||||
let mut backend = Backend {
|
||||
reports_remaining: MAX_REPORTS_PER_CYCLE + 3,
|
||||
..Backend::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
let (pool, reports) = link.cycle(&mut miner).unwrap();
|
||||
assert!(matches!(pool, PoolEvent::Control));
|
||||
assert_eq!(reports.len(), MAX_REPORTS_PER_CYCLE);
|
||||
assert!(reports
|
||||
.iter()
|
||||
.all(|event| matches!(event, ReportEvent::Stale)));
|
||||
drop(miner);
|
||||
assert_eq!(backend.reports_remaining, 3);
|
||||
server.join().unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Bounded pool-job bookkeeping for one BM1366 chain and one Stratum V1 session.
|
||||
|
||||
use crate::difficulty::PoolDifficulty;
|
||||
use crate::miner::{Bm1366Miner, MinerError, ValidShare};
|
||||
use crate::pow::{sha256d, VersionMask};
|
||||
use crate::stratum_submit::{format_submission, Submission, SubmitError};
|
||||
use crate::stratum_v1::{
|
||||
parse_control_notification, parse_notify, PoolMessageError, PoolNotification, Subscription,
|
||||
};
|
||||
use crate::stratum_work::JobError;
|
||||
use crate::HardwareBackend;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum SessionError {
|
||||
InvalidSubscription,
|
||||
Pool(PoolMessageError),
|
||||
Work(JobError),
|
||||
MissingDifficulty,
|
||||
ExtranonceExhausted,
|
||||
WorkIdsExhausted,
|
||||
StaleShare,
|
||||
UnverifiedShare,
|
||||
Submit(SubmitError),
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum DispatchError<E> {
|
||||
Session(SessionError),
|
||||
Miner(MinerError<E>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IssuedWork {
|
||||
pub work_id: u8,
|
||||
pub header: [u8; 80],
|
||||
pub target_le: [u8; 32],
|
||||
/// The hardware miner must forget prior work before submitting this job.
|
||||
pub clean_jobs: bool,
|
||||
}
|
||||
|
||||
struct TrackedWork {
|
||||
job_id: String,
|
||||
header: [u8; 80],
|
||||
target_le: [u8; 32],
|
||||
extranonce2: Vec<u8>,
|
||||
ntime: u32,
|
||||
base_version: u32,
|
||||
}
|
||||
|
||||
struct NonceSpace {
|
||||
template: [u8; 80],
|
||||
next: [u8; 32],
|
||||
exhausted: bool,
|
||||
}
|
||||
|
||||
const MAX_NONCE_SPACES: usize = 4096;
|
||||
|
||||
pub struct PoolSession {
|
||||
subscription: Subscription,
|
||||
version_mask: Option<VersionMask>,
|
||||
subscribe_extranonce: bool,
|
||||
pending_difficulty: Option<PoolDifficulty>,
|
||||
active_difficulty: Option<PoolDifficulty>,
|
||||
nonce_spaces: Vec<NonceSpace>,
|
||||
zero_extranonce_headers: Vec<[u8; 80]>,
|
||||
tracked: [Option<TrackedWork>; 32],
|
||||
next_slot: usize,
|
||||
}
|
||||
|
||||
impl PoolSession {
|
||||
pub fn new(
|
||||
subscription: Subscription,
|
||||
version_mask: Option<VersionMask>,
|
||||
subscribe_extranonce: bool,
|
||||
) -> Result<Self, SessionError> {
|
||||
if subscription.extranonce1.len() > 32 || subscription.extranonce2_size > 32 {
|
||||
return Err(SessionError::InvalidSubscription);
|
||||
}
|
||||
Ok(Self {
|
||||
subscription,
|
||||
version_mask,
|
||||
subscribe_extranonce,
|
||||
pending_difficulty: None,
|
||||
active_difficulty: None,
|
||||
nonce_spaces: Vec::new(),
|
||||
zero_extranonce_headers: Vec::new(),
|
||||
tracked: std::array::from_fn(|_| None),
|
||||
next_slot: 0,
|
||||
})
|
||||
}
|
||||
|
||||
/// Apply a control message in wire order. A true result means the caller
|
||||
/// must call the hardware miner's `clear_work` before accepting reports.
|
||||
pub fn control(&mut self, line: &[u8]) -> Result<bool, SessionError> {
|
||||
let control = parse_control_notification(
|
||||
line,
|
||||
self.version_mask.is_some(),
|
||||
self.subscribe_extranonce,
|
||||
)
|
||||
.map_err(SessionError::Pool)?;
|
||||
match control {
|
||||
PoolNotification::SetDifficulty(difficulty) => {
|
||||
self.pending_difficulty = Some(difficulty);
|
||||
Ok(false)
|
||||
}
|
||||
PoolNotification::SetVersionMask(mask) => {
|
||||
if self.version_mask == Some(mask) {
|
||||
return Ok(false);
|
||||
}
|
||||
self.version_mask = Some(mask);
|
||||
self.tracked = std::array::from_fn(|_| None);
|
||||
Ok(true)
|
||||
}
|
||||
PoolNotification::SetExtranonce(subscription) => {
|
||||
self.subscription = subscription;
|
||||
self.nonce_spaces.clear();
|
||||
self.zero_extranonce_headers.clear();
|
||||
self.tracked = std::array::from_fn(|_| None);
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply a pool control message and invalidate the hardware miner's work
|
||||
/// when an extranonce or version-mask change makes earlier jobs stale.
|
||||
pub fn control_with_miner<B: HardwareBackend>(
|
||||
&mut self,
|
||||
miner: &mut Bm1366Miner<'_, B>,
|
||||
line: &[u8],
|
||||
) -> Result<(), SessionError> {
|
||||
if self.control(line)? {
|
||||
miner.clear_work();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Allocate one unique extranonce and ASIC work ID for a pool job. The
|
||||
/// caller must pass this header, target, and ID to `Bm1366Miner::submit`.
|
||||
/// On `clean_jobs`, clear the hardware miner's work first. Work IDs are
|
||||
/// never reused on the same chain without an explicit hardware reset.
|
||||
pub fn issue_notify(&mut self, line: &[u8]) -> Result<IssuedWork, SessionError> {
|
||||
let job = parse_notify(line, &self.subscription).map_err(SessionError::Pool)?;
|
||||
let difficulty = self
|
||||
.pending_difficulty
|
||||
.or(self.active_difficulty)
|
||||
.ok_or(SessionError::MissingDifficulty)?;
|
||||
if self.next_slot == self.tracked.len() {
|
||||
return Err(SessionError::WorkIdsExhausted);
|
||||
}
|
||||
let size = self.subscription.extranonce2_size;
|
||||
let template = if size == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(
|
||||
job.work
|
||||
.nonce_zero_header(&vec![0; size], job.work.ntime)
|
||||
.map_err(SessionError::Work)?,
|
||||
)
|
||||
};
|
||||
let space_index = template.and_then(|header| {
|
||||
self.nonce_spaces
|
||||
.iter()
|
||||
.position(|space| space.template == header)
|
||||
});
|
||||
if size != 0 && space_index.is_none() && self.nonce_spaces.len() == MAX_NONCE_SPACES {
|
||||
return Err(SessionError::ExtranonceExhausted);
|
||||
}
|
||||
let extranonce2 = match space_index {
|
||||
Some(index) if self.nonce_spaces[index].exhausted => {
|
||||
return Err(SessionError::ExtranonceExhausted)
|
||||
}
|
||||
Some(index) => self.nonce_spaces[index].next[..size].to_vec(),
|
||||
None => vec![0; size],
|
||||
};
|
||||
let header = job
|
||||
.work
|
||||
.nonce_zero_header(&extranonce2, job.work.ntime)
|
||||
.map_err(SessionError::Work)?;
|
||||
if size == 0 && self.zero_extranonce_headers.contains(&header) {
|
||||
return Err(SessionError::ExtranonceExhausted);
|
||||
}
|
||||
let target_le = difficulty.target_le();
|
||||
if job.clean_jobs {
|
||||
self.tracked = std::array::from_fn(|_| None);
|
||||
}
|
||||
self.active_difficulty = Some(difficulty);
|
||||
self.pending_difficulty = None;
|
||||
let slot = self.next_slot;
|
||||
self.next_slot += 1;
|
||||
self.tracked[slot] = Some(TrackedWork {
|
||||
job_id: job.id,
|
||||
header,
|
||||
target_le,
|
||||
extranonce2,
|
||||
ntime: job.work.ntime,
|
||||
base_version: job.work.version,
|
||||
});
|
||||
if size == 0 {
|
||||
self.zero_extranonce_headers.push(header);
|
||||
} else {
|
||||
let space = match space_index {
|
||||
Some(index) => &mut self.nonce_spaces[index],
|
||||
None => {
|
||||
self.nonce_spaces.push(NonceSpace {
|
||||
template: template.expect("nonzero extranonce has a template"),
|
||||
next: [0; 32],
|
||||
exhausted: false,
|
||||
});
|
||||
self.nonce_spaces.last_mut().expect("just pushed")
|
||||
}
|
||||
};
|
||||
let mut carry = true;
|
||||
for byte in space.next[..size].iter_mut().rev() {
|
||||
let (new, overflow) = byte.overflowing_add(u8::from(carry));
|
||||
*byte = new;
|
||||
carry = overflow;
|
||||
if !carry {
|
||||
break;
|
||||
}
|
||||
}
|
||||
space.exhausted = carry;
|
||||
}
|
||||
Ok(IssuedWork {
|
||||
work_id: (slot as u8) << 3,
|
||||
header,
|
||||
target_le,
|
||||
clean_jobs: job.clean_jobs,
|
||||
})
|
||||
}
|
||||
|
||||
/// Preserve pool-message order while dispatching one job to the hardware
|
||||
/// miner. A failed dispatch consumes its ASIC work ID to avoid ambiguity
|
||||
/// with any frame that may have reached the device before the error.
|
||||
pub fn issue_to_miner<B: HardwareBackend>(
|
||||
&mut self,
|
||||
miner: &mut Bm1366Miner<'_, B>,
|
||||
line: &[u8],
|
||||
) -> Result<IssuedWork, DispatchError<B::Error>> {
|
||||
let issued = self.issue_notify(line).map_err(DispatchError::Session)?;
|
||||
if issued.clean_jobs {
|
||||
miner.clear_work();
|
||||
}
|
||||
if let Err(error) = miner.submit(issued.header, issued.target_le, issued.work_id) {
|
||||
self.tracked[usize::from(issued.work_id >> 3)] = None;
|
||||
return Err(DispatchError::Miner(error));
|
||||
}
|
||||
Ok(issued)
|
||||
}
|
||||
|
||||
/// Recheck the hardware-reported share and format its pool submission.
|
||||
/// This method verifies one reported nonce; it never searches for one.
|
||||
pub fn submission(
|
||||
&self,
|
||||
share: ValidShare,
|
||||
worker: &str,
|
||||
request_id: u64,
|
||||
) -> Result<String, SessionError> {
|
||||
let tracked = self
|
||||
.tracked
|
||||
.get(usize::from(share.work_id >> 3))
|
||||
.and_then(Option::as_ref)
|
||||
.ok_or(SessionError::StaleShare)?;
|
||||
if share.work_id & 7 != 0 {
|
||||
return Err(SessionError::StaleShare);
|
||||
}
|
||||
let mut header = tracked.header;
|
||||
header[..4].copy_from_slice(&share.rolled_version.to_le_bytes());
|
||||
header[76..80].copy_from_slice(&share.nonce.to_le_bytes());
|
||||
let hash_le = sha256d(&header);
|
||||
if hash_le != share.hash_le
|
||||
|| hash_le
|
||||
.iter()
|
||||
.rev()
|
||||
.cmp(tracked.target_le.iter().rev())
|
||||
.is_gt()
|
||||
{
|
||||
return Err(SessionError::UnverifiedShare);
|
||||
}
|
||||
format_submission(&Submission {
|
||||
request_id,
|
||||
worker,
|
||||
job_id: &tracked.job_id,
|
||||
extranonce2: &tracked.extranonce2,
|
||||
ntime: tracked.ntime,
|
||||
nonce: share.nonce,
|
||||
base_version: tracked.base_version,
|
||||
rolled_version: share.rolled_version,
|
||||
version_mask: self.version_mask,
|
||||
})
|
||||
.map_err(SessionError::Submit)
|
||||
}
|
||||
|
||||
/// Call only after the chain has actually reset and prior reports cannot
|
||||
/// arrive. A pool `clean_jobs` notification alone is insufficient.
|
||||
pub fn after_hardware_reset(&mut self) {
|
||||
self.tracked = std::array::from_fn(|_| None);
|
||||
self.next_slot = 0;
|
||||
self.zero_extranonce_headers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn bytes(text: &str) -> Vec<u8> {
|
||||
text.as_bytes()
|
||||
.chunks_exact(2)
|
||||
.map(|part| u8::from_str_radix(std::str::from_utf8(part).unwrap(), 16).unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_report_is_rechecked_and_formatted_for_pool() {
|
||||
let header: [u8; 80] = bytes(concat!(
|
||||
"00000020fc3ab8d43021f45eb662389d0f791f657dbf75a5cb93670b57b6c22503",
|
||||
"b4213a43ae1052ebd44443068fe178e2c61ca4994846fd133addb04aab2b382c5",
|
||||
"b4c74c1012e6a8f06021700000000"
|
||||
))
|
||||
.try_into()
|
||||
.unwrap();
|
||||
let hash_le: [u8; 32] =
|
||||
bytes("4b5e00850cf9de9809f822f8858eda39e49996d9d40f634f1043290000000000")
|
||||
.try_into()
|
||||
.unwrap();
|
||||
let mut target_le = [0_u8; 32];
|
||||
target_le[26..28].fill(0xff);
|
||||
let mut session = PoolSession::new(
|
||||
Subscription {
|
||||
extranonce1: vec![],
|
||||
extranonce2_size: 4,
|
||||
},
|
||||
VersionMask::new(0x1fff_e000),
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
session.tracked[3] = Some(TrackedWork {
|
||||
job_id: "captured-job".into(),
|
||||
header,
|
||||
target_le,
|
||||
extranonce2: vec![0, 0, 0, 1],
|
||||
ntime: u32::from_le_bytes(header[68..72].try_into().unwrap()),
|
||||
base_version: 0x2000_0000,
|
||||
});
|
||||
let share = ValidShare {
|
||||
nonce: 0xedad_ff38,
|
||||
work_id: 0x18,
|
||||
rolled_version: 0x2096_6000,
|
||||
hash_le,
|
||||
};
|
||||
let submit = session.submission(share, "miner.worker", 7).unwrap();
|
||||
assert!(submit.contains("\"mining.submit\""));
|
||||
assert!(submit.contains("\"captured-job\""));
|
||||
assert!(submit.contains("\"edadff38\",\"00966000\""));
|
||||
assert_eq!(
|
||||
session.submission(
|
||||
ValidShare {
|
||||
hash_le: [0; 32],
|
||||
..share
|
||||
},
|
||||
"miner.worker",
|
||||
8
|
||||
),
|
||||
Err(SessionError::UnverifiedShare)
|
||||
);
|
||||
assert_eq!(
|
||||
session.control(
|
||||
b"{\"id\":null,\"method\":\"mining.set_version_mask\",\"params\":[\"00002000\"]}"
|
||||
),
|
||||
Ok(true)
|
||||
);
|
||||
assert_eq!(
|
||||
session.submission(share, "miner.worker", 8),
|
||||
Err(SessionError::StaleShare)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Format one verified share for Stratum V1 and negotiated BIP310 submission.
|
||||
//! Pool I/O and request tracking are owned by the caller.
|
||||
|
||||
use crate::pow::VersionMask;
|
||||
use std::fmt::Write;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum SubmitError {
|
||||
EmptyWorker,
|
||||
EmptyJobId,
|
||||
FieldTooLong,
|
||||
ExtranonceTooLong,
|
||||
UnrepresentableVersion,
|
||||
}
|
||||
|
||||
pub struct Submission<'a> {
|
||||
pub request_id: u64,
|
||||
pub worker: &'a str,
|
||||
pub job_id: &'a str,
|
||||
pub extranonce2: &'a [u8],
|
||||
pub ntime: u32,
|
||||
pub nonce: u32,
|
||||
pub base_version: u32,
|
||||
pub rolled_version: u32,
|
||||
/// The last pool mask, only after version rolling was accepted.
|
||||
pub version_mask: Option<VersionMask>,
|
||||
}
|
||||
|
||||
fn json_string(out: &mut String, value: &str) {
|
||||
out.push('"');
|
||||
for ch in value.chars() {
|
||||
match ch {
|
||||
'"' => out.push_str("\\\""),
|
||||
'\\' => out.push_str("\\\\"),
|
||||
'\n' => out.push_str("\\n"),
|
||||
'\r' => out.push_str("\\r"),
|
||||
'\t' => out.push_str("\\t"),
|
||||
c if c <= '\u{001f}' => {
|
||||
write!(out, "\\u{:04x}", c as u32).expect("write to String");
|
||||
}
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out.push('"');
|
||||
}
|
||||
|
||||
/// Emit one newline-terminated JSON request. The caller must have checked the
|
||||
/// ASIC report against the pool target and retained the original job metadata.
|
||||
pub fn format_submission(share: &Submission<'_>) -> Result<String, SubmitError> {
|
||||
if share.worker.is_empty() {
|
||||
return Err(SubmitError::EmptyWorker);
|
||||
}
|
||||
if share.job_id.is_empty() {
|
||||
return Err(SubmitError::EmptyJobId);
|
||||
}
|
||||
if share.worker.len() > 128 || share.job_id.len() > 128 {
|
||||
return Err(SubmitError::FieldTooLong);
|
||||
}
|
||||
if share.extranonce2.len() > 32 {
|
||||
return Err(SubmitError::ExtranonceTooLong);
|
||||
}
|
||||
let version_bits = match share.version_mask {
|
||||
Some(mask) => {
|
||||
let bits = share.rolled_version & mask.bits();
|
||||
if mask.apply_submission_bits(share.base_version, bits) != Some(share.rolled_version) {
|
||||
return Err(SubmitError::UnrepresentableVersion);
|
||||
}
|
||||
Some(bits)
|
||||
}
|
||||
None if share.rolled_version == share.base_version => None,
|
||||
None => return Err(SubmitError::UnrepresentableVersion),
|
||||
};
|
||||
|
||||
let mut out = format!(
|
||||
"{{\"id\":{},\"method\":\"mining.submit\",\"params\":[",
|
||||
share.request_id
|
||||
);
|
||||
json_string(&mut out, share.worker);
|
||||
out.push(',');
|
||||
json_string(&mut out, share.job_id);
|
||||
out.push_str(",\"");
|
||||
for byte in share.extranonce2 {
|
||||
write!(out, "{byte:02x}").expect("write to String");
|
||||
}
|
||||
write!(out, "\",\"{:08x}\",\"{:08x}\"", share.ntime, share.nonce).expect("write to String");
|
||||
if let Some(bits) = version_bits {
|
||||
write!(out, ",\"{bits:08x}\"").expect("write to String");
|
||||
}
|
||||
out.push_str("]}\n");
|
||||
Ok(out)
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Bounded decoding of Bitcoin Stratum V1 subscription and job messages.
|
||||
//! The network client must enforce a line limit before passing data here.
|
||||
|
||||
use crate::difficulty::PoolDifficulty;
|
||||
use crate::pow::VersionMask;
|
||||
use crate::stratum_work::{JobError, Sha256Job};
|
||||
use serde_json::Value;
|
||||
|
||||
pub const MAX_POOL_LINE_BYTES: usize = 131_072;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum PoolMessageError {
|
||||
TooLarge,
|
||||
Json,
|
||||
WrongMessage,
|
||||
Unnegotiated,
|
||||
InvalidField,
|
||||
InvalidHex,
|
||||
InvalidWork(JobError),
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Subscription {
|
||||
pub extranonce1: Vec<u8>,
|
||||
pub extranonce2_size: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct ConfigureResult {
|
||||
pub version_mask: Option<VersionMask>,
|
||||
pub subscribe_extranonce: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum PoolNotification {
|
||||
SetDifficulty(PoolDifficulty),
|
||||
SetExtranonce(Subscription),
|
||||
SetVersionMask(VersionMask),
|
||||
}
|
||||
|
||||
pub struct PoolJob {
|
||||
pub id: String,
|
||||
pub clean_jobs: bool,
|
||||
pub work: Sha256Job,
|
||||
}
|
||||
|
||||
fn parse_json(line: &[u8]) -> Result<Value, PoolMessageError> {
|
||||
if line.len() > MAX_POOL_LINE_BYTES {
|
||||
return Err(PoolMessageError::TooLarge);
|
||||
}
|
||||
serde_json::from_slice(line).map_err(|_| PoolMessageError::Json)
|
||||
}
|
||||
|
||||
fn hex_bytes(text: &str) -> Result<Vec<u8>, PoolMessageError> {
|
||||
let bytes = text.as_bytes();
|
||||
if bytes.len() & 1 != 0 {
|
||||
return Err(PoolMessageError::InvalidHex);
|
||||
}
|
||||
let mut result = Vec::with_capacity(bytes.len() / 2);
|
||||
for pair in bytes.chunks_exact(2) {
|
||||
let high = (pair[0] as char)
|
||||
.to_digit(16)
|
||||
.ok_or(PoolMessageError::InvalidHex)?;
|
||||
let low = (pair[1] as char)
|
||||
.to_digit(16)
|
||||
.ok_or(PoolMessageError::InvalidHex)?;
|
||||
result.push(((high << 4) | low) as u8);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn hex_fixed<const N: usize>(field: &Value) -> Result<[u8; N], PoolMessageError> {
|
||||
let text = field.as_str().ok_or(PoolMessageError::InvalidField)?;
|
||||
if text.len() != N * 2 {
|
||||
return Err(PoolMessageError::InvalidField);
|
||||
}
|
||||
hex_bytes(text)?
|
||||
.try_into()
|
||||
.map_err(|_| PoolMessageError::InvalidField)
|
||||
}
|
||||
|
||||
fn hex_u32(field: &Value) -> Result<u32, PoolMessageError> {
|
||||
let text = field.as_str().ok_or(PoolMessageError::InvalidField)?;
|
||||
if text.len() != 8 {
|
||||
return Err(PoolMessageError::InvalidField);
|
||||
}
|
||||
u32::from_str_radix(text, 16).map_err(|_| PoolMessageError::InvalidHex)
|
||||
}
|
||||
|
||||
fn valid_subscription(
|
||||
extranonce1: Vec<u8>,
|
||||
extranonce2_size: u64,
|
||||
) -> Result<Subscription, PoolMessageError> {
|
||||
if extranonce1.len() > 32 || extranonce2_size > 32 {
|
||||
return Err(PoolMessageError::InvalidField);
|
||||
}
|
||||
Ok(Subscription {
|
||||
extranonce1,
|
||||
extranonce2_size: extranonce2_size as usize,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse the response to a matching `mining.subscribe` request.
|
||||
pub fn parse_subscription(line: &[u8], request_id: u64) -> Result<Subscription, PoolMessageError> {
|
||||
let message = parse_json(line)?;
|
||||
if message.get("id").and_then(Value::as_u64) != Some(request_id)
|
||||
|| !message.get("error").is_none_or(Value::is_null)
|
||||
{
|
||||
return Err(PoolMessageError::WrongMessage);
|
||||
}
|
||||
let result = message
|
||||
.get("result")
|
||||
.and_then(Value::as_array)
|
||||
.ok_or(PoolMessageError::InvalidField)?;
|
||||
if result.len() != 3 || !result[0].is_array() {
|
||||
return Err(PoolMessageError::InvalidField);
|
||||
}
|
||||
let extranonce1 = hex_bytes(result[1].as_str().ok_or(PoolMessageError::InvalidField)?)?;
|
||||
let extranonce2_size = result[2].as_u64().ok_or(PoolMessageError::InvalidField)?;
|
||||
valid_subscription(extranonce1, extranonce2_size)
|
||||
}
|
||||
|
||||
/// Decode the response to a BIP310 configure request that offered version
|
||||
/// rolling. A pool mask outside this ASIC's changeable bits is unusable: the
|
||||
/// pool would reconstruct a different block version for submitted shares.
|
||||
pub fn parse_configure_response(
|
||||
line: &[u8],
|
||||
request_id: u64,
|
||||
) -> Result<Option<VersionMask>, PoolMessageError> {
|
||||
let message = parse_json(line)?;
|
||||
if message.get("id").and_then(Value::as_u64) != Some(request_id)
|
||||
|| !message.get("error").is_none_or(Value::is_null)
|
||||
{
|
||||
return Err(PoolMessageError::WrongMessage);
|
||||
}
|
||||
let result = message
|
||||
.get("result")
|
||||
.and_then(Value::as_object)
|
||||
.ok_or(PoolMessageError::InvalidField)?;
|
||||
match result.get("version-rolling") {
|
||||
Some(Value::Bool(false) | Value::String(_)) => Ok(None),
|
||||
Some(Value::Bool(true)) => {
|
||||
let mask = hex_u32(
|
||||
result
|
||||
.get("version-rolling.mask")
|
||||
.ok_or(PoolMessageError::InvalidField)?,
|
||||
)?;
|
||||
VersionMask::new(mask)
|
||||
.map(Some)
|
||||
.ok_or(PoolMessageError::InvalidField)
|
||||
}
|
||||
_ => Err(PoolMessageError::InvalidField),
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode both offered BIP310 extensions. An omitted extranonce extension is
|
||||
/// unsupported; its notifications cannot be used without an explicit grant.
|
||||
pub fn parse_configure_result(
|
||||
line: &[u8],
|
||||
request_id: u64,
|
||||
) -> Result<ConfigureResult, PoolMessageError> {
|
||||
let version_mask = parse_configure_response(line, request_id)?;
|
||||
let message = parse_json(line)?;
|
||||
let result = message
|
||||
.get("result")
|
||||
.and_then(Value::as_object)
|
||||
.ok_or(PoolMessageError::InvalidField)?;
|
||||
let subscribe_extranonce = match result.get("subscribe-extranonce") {
|
||||
Some(Value::Bool(value)) => *value,
|
||||
Some(Value::String(_)) | None => false,
|
||||
_ => return Err(PoolMessageError::InvalidField),
|
||||
};
|
||||
Ok(ConfigureResult {
|
||||
version_mask,
|
||||
subscribe_extranonce,
|
||||
})
|
||||
}
|
||||
|
||||
/// Decode a matching authorization or share-submission result. A false
|
||||
/// result is an ordinary pool rejection and must not count as an accept.
|
||||
pub fn parse_boolean_response(line: &[u8], request_id: u64) -> Result<bool, PoolMessageError> {
|
||||
let message = parse_json(line)?;
|
||||
if message.get("id").and_then(Value::as_u64) != Some(request_id) {
|
||||
return Err(PoolMessageError::WrongMessage);
|
||||
}
|
||||
match message.get("result") {
|
||||
Some(Value::Bool(true)) if message.get("error").is_none_or(Value::is_null) => Ok(true),
|
||||
Some(Value::Bool(false)) => Ok(false),
|
||||
Some(Value::Null) if !message.get("error").is_none_or(Value::is_null) => Ok(false),
|
||||
_ => Err(PoolMessageError::InvalidField),
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode pool control notifications. The caller applies them in stream order:
|
||||
/// difficulty to the next job, extranonce to subsequent work, and the version
|
||||
/// mask immediately to all pending submissions.
|
||||
pub fn parse_control_notification(
|
||||
line: &[u8],
|
||||
version_rolling: bool,
|
||||
subscribe_extranonce: bool,
|
||||
) -> Result<PoolNotification, PoolMessageError> {
|
||||
let message = parse_json(line)?;
|
||||
if !message.get("id").is_none_or(Value::is_null) {
|
||||
return Err(PoolMessageError::WrongMessage);
|
||||
}
|
||||
let method = message
|
||||
.get("method")
|
||||
.and_then(Value::as_str)
|
||||
.ok_or(PoolMessageError::WrongMessage)?;
|
||||
let params = message
|
||||
.get("params")
|
||||
.and_then(Value::as_array)
|
||||
.ok_or(PoolMessageError::InvalidField)?;
|
||||
match method {
|
||||
"mining.set_difficulty" if params.len() == 1 => {
|
||||
let number = params[0]
|
||||
.as_number()
|
||||
.ok_or(PoolMessageError::InvalidField)?;
|
||||
let difficulty = PoolDifficulty::from_json_number(number)
|
||||
.map_err(|_| PoolMessageError::InvalidField)?;
|
||||
Ok(PoolNotification::SetDifficulty(difficulty))
|
||||
}
|
||||
"mining.set_version_mask" if params.len() == 1 => {
|
||||
if !version_rolling {
|
||||
return Err(PoolMessageError::Unnegotiated);
|
||||
}
|
||||
let mask = hex_u32(¶ms[0])?;
|
||||
let mask = VersionMask::new(mask).ok_or(PoolMessageError::InvalidField)?;
|
||||
Ok(PoolNotification::SetVersionMask(mask))
|
||||
}
|
||||
"mining.set_extranonce" if params.len() == 2 => {
|
||||
if !subscribe_extranonce {
|
||||
return Err(PoolMessageError::Unnegotiated);
|
||||
}
|
||||
let extranonce1 = hex_bytes(params[0].as_str().ok_or(PoolMessageError::InvalidField)?)?;
|
||||
let size = params[1].as_u64().ok_or(PoolMessageError::InvalidField)?;
|
||||
Ok(PoolNotification::SetExtranonce(valid_subscription(
|
||||
extranonce1,
|
||||
size,
|
||||
)?))
|
||||
}
|
||||
"mining.set_difficulty" | "mining.set_version_mask" | "mining.set_extranonce" => {
|
||||
Err(PoolMessageError::InvalidField)
|
||||
}
|
||||
_ => Err(PoolMessageError::WrongMessage),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse exactly the nine Bitcoin `mining.notify` parameters into work.
|
||||
pub fn parse_notify(line: &[u8], subscription: &Subscription) -> Result<PoolJob, PoolMessageError> {
|
||||
let message = parse_json(line)?;
|
||||
if message.get("method").and_then(Value::as_str) != Some("mining.notify")
|
||||
|| !message.get("id").is_none_or(Value::is_null)
|
||||
{
|
||||
return Err(PoolMessageError::WrongMessage);
|
||||
}
|
||||
let params = message
|
||||
.get("params")
|
||||
.and_then(Value::as_array)
|
||||
.ok_or(PoolMessageError::InvalidField)?;
|
||||
if params.len() != 9 {
|
||||
return Err(PoolMessageError::InvalidField);
|
||||
}
|
||||
let id = params[0].as_str().ok_or(PoolMessageError::InvalidField)?;
|
||||
if id.is_empty() || id.len() > 128 {
|
||||
return Err(PoolMessageError::InvalidField);
|
||||
}
|
||||
let branches = params[4].as_array().ok_or(PoolMessageError::InvalidField)?;
|
||||
if branches.len() > 64 {
|
||||
return Err(PoolMessageError::InvalidField);
|
||||
}
|
||||
let mut merkle_branches = Vec::with_capacity(branches.len());
|
||||
for branch in branches {
|
||||
merkle_branches.push(hex_fixed::<32>(branch)?);
|
||||
}
|
||||
let work = Sha256Job {
|
||||
prevhash_stratum: hex_fixed::<32>(¶ms[1])?,
|
||||
coinb1: hex_bytes(params[2].as_str().ok_or(PoolMessageError::InvalidField)?)?,
|
||||
coinb2: hex_bytes(params[3].as_str().ok_or(PoolMessageError::InvalidField)?)?,
|
||||
merkle_branches,
|
||||
version: hex_u32(¶ms[5])?,
|
||||
nbits: hex_u32(¶ms[6])?,
|
||||
ntime: hex_u32(¶ms[7])?,
|
||||
extranonce1: subscription.extranonce1.clone(),
|
||||
extranonce2_size: subscription.extranonce2_size,
|
||||
};
|
||||
work.validate().map_err(PoolMessageError::InvalidWork)?;
|
||||
let clean_jobs = params[8].as_bool().ok_or(PoolMessageError::InvalidField)?;
|
||||
Ok(PoolJob {
|
||||
id: id.to_owned(),
|
||||
clean_jobs,
|
||||
work,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod configure_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn omitted_extranonce_extension_is_unsupported() {
|
||||
let line = br#"{"id":1,"result":{"version-rolling":true,"version-rolling.mask":"1fffe000"},"error":null}"#;
|
||||
let result = parse_configure_result(line, 1).unwrap();
|
||||
assert!(result.version_mask.is_some());
|
||||
assert!(!result.subscribe_extranonce);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Bounded newline-framed TCP transport for Bitcoin Stratum V1 pools.
|
||||
|
||||
use crate::stratum_v1::MAX_POOL_LINE_BYTES;
|
||||
use crate::stratum_v1::{
|
||||
parse_boolean_response, parse_configure_result, parse_subscription, ConfigureResult,
|
||||
PoolMessageError, Subscription,
|
||||
};
|
||||
use serde_json::json;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::net::{Shutdown, SocketAddr, TcpStream};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const MAX_EARLY_NOTIFICATIONS: usize = 32;
|
||||
const MAX_EARLY_NOTIFICATION_BYTES: usize = 262_144;
|
||||
const READ_SLICE: Duration = Duration::from_millis(1);
|
||||
const MAX_PARTIAL_AGE: Duration = Duration::from_secs(2);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WireError {
|
||||
InvalidRequest,
|
||||
LineTooLong,
|
||||
Timeout,
|
||||
Eof,
|
||||
Closed,
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum HandshakeError {
|
||||
Wire(WireError),
|
||||
Pool(PoolMessageError),
|
||||
Rejected,
|
||||
TooManyNotifications,
|
||||
}
|
||||
|
||||
pub struct PoolHandshake {
|
||||
pub subscription: Subscription,
|
||||
pub configure: ConfigureResult,
|
||||
/// Notifications received during negotiation, in original wire order.
|
||||
pub early_notifications: Vec<Vec<u8>>,
|
||||
}
|
||||
|
||||
pub struct StratumWire {
|
||||
stream: TcpStream,
|
||||
buffered: Vec<u8>,
|
||||
partial_started: Option<Instant>,
|
||||
timeout: Duration,
|
||||
write_timeout: Duration,
|
||||
closed: bool,
|
||||
}
|
||||
|
||||
impl StratumWire {
|
||||
pub fn connect(address: SocketAddr, timeout: Duration) -> Result<Self, WireError> {
|
||||
if timeout.is_zero() || Instant::now().checked_add(timeout).is_none() {
|
||||
return Err(WireError::InvalidRequest);
|
||||
}
|
||||
let stream = TcpStream::connect_timeout(&address, timeout).map_err(WireError::Io)?;
|
||||
stream.set_nodelay(true).map_err(WireError::Io)?;
|
||||
Ok(Self {
|
||||
stream,
|
||||
buffered: Vec::new(),
|
||||
partial_started: None,
|
||||
timeout,
|
||||
write_timeout: timeout,
|
||||
closed: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_stream(stream: TcpStream, timeout: Duration) -> Result<Self, WireError> {
|
||||
if timeout.is_zero() || Instant::now().checked_add(timeout).is_none() {
|
||||
return Err(WireError::InvalidRequest);
|
||||
}
|
||||
stream.set_nodelay(true).map_err(WireError::Io)?;
|
||||
Ok(Self {
|
||||
stream,
|
||||
buffered: Vec::new(),
|
||||
partial_started: None,
|
||||
timeout,
|
||||
write_timeout: timeout,
|
||||
closed: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn close(&mut self) {
|
||||
self.closed = true;
|
||||
let _ = self.stream.shutdown(Shutdown::Both);
|
||||
}
|
||||
|
||||
pub fn is_closed(&self) -> bool {
|
||||
self.closed
|
||||
}
|
||||
|
||||
pub(crate) fn cap_write_timeout(&mut self, limit: Duration) {
|
||||
self.write_timeout = self.write_timeout.min(limit);
|
||||
}
|
||||
|
||||
fn remaining(deadline: Instant) -> Result<Duration, WireError> {
|
||||
deadline
|
||||
.checked_duration_since(Instant::now())
|
||||
.filter(|remaining| !remaining.is_zero())
|
||||
.ok_or(WireError::Timeout)
|
||||
}
|
||||
|
||||
fn transport_error(error: io::Error) -> WireError {
|
||||
match error.kind() {
|
||||
io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock => WireError::Timeout,
|
||||
_ => WireError::Io(error),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_line_before(&mut self, deadline: Instant) -> Result<Vec<u8>, WireError> {
|
||||
if self.closed {
|
||||
return Err(WireError::Closed);
|
||||
}
|
||||
loop {
|
||||
if let Some(end) = self.buffered.iter().position(|byte| *byte == b'\n') {
|
||||
if end + 1 > MAX_POOL_LINE_BYTES {
|
||||
self.close();
|
||||
return Err(WireError::LineTooLong);
|
||||
}
|
||||
let line = self.buffered.drain(..=end).collect();
|
||||
self.partial_started = if self.buffered.is_empty() {
|
||||
None
|
||||
} else {
|
||||
self.partial_started.or_else(|| Some(Instant::now()))
|
||||
};
|
||||
return Ok(line);
|
||||
}
|
||||
if self.buffered.len() >= MAX_POOL_LINE_BYTES {
|
||||
self.close();
|
||||
return Err(WireError::LineTooLong);
|
||||
}
|
||||
let remaining = match Self::remaining(deadline) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
if !self.buffered.is_empty() {
|
||||
self.close();
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
if let Err(error) = self.stream.set_read_timeout(Some(remaining)) {
|
||||
self.close();
|
||||
return Err(WireError::Io(error));
|
||||
}
|
||||
let mut chunk = [0_u8; 4096];
|
||||
match self.stream.read(&mut chunk) {
|
||||
Ok(0) => {
|
||||
self.close();
|
||||
return Err(WireError::Eof);
|
||||
}
|
||||
Ok(length) => {
|
||||
if self.buffered.is_empty() {
|
||||
self.partial_started = Some(Instant::now());
|
||||
}
|
||||
self.buffered.extend_from_slice(&chunk[..length]);
|
||||
}
|
||||
Err(error) => {
|
||||
let error = Self::transport_error(error);
|
||||
if !matches!(error, WireError::Timeout) || !self.buffered.is_empty() {
|
||||
self.close();
|
||||
}
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_line(&mut self) -> Result<Vec<u8>, WireError> {
|
||||
self.read_line_before(Instant::now() + self.timeout)
|
||||
}
|
||||
|
||||
/// Check one socket slice without coupling ASIC polling to pool silence.
|
||||
/// An unfinished line has its own absolute deadline across calls.
|
||||
pub fn try_read_line(&mut self) -> Result<Option<Vec<u8>>, WireError> {
|
||||
if self.closed {
|
||||
return Err(WireError::Closed);
|
||||
}
|
||||
if let Some(line) = self.try_read_line_buffered()? {
|
||||
return Ok(Some(line));
|
||||
}
|
||||
if self
|
||||
.partial_started
|
||||
.is_some_and(|start| start.elapsed() >= MAX_PARTIAL_AGE)
|
||||
{
|
||||
self.close();
|
||||
return Err(WireError::Timeout);
|
||||
}
|
||||
self.stream
|
||||
.set_read_timeout(Some(READ_SLICE))
|
||||
.map_err(WireError::Io)?;
|
||||
let mut chunk = [0_u8; 4096];
|
||||
match self.stream.read(&mut chunk) {
|
||||
Ok(0) => {
|
||||
self.close();
|
||||
Err(WireError::Eof)
|
||||
}
|
||||
Ok(length) => {
|
||||
if self.buffered.is_empty() {
|
||||
self.partial_started = Some(Instant::now());
|
||||
}
|
||||
self.buffered.extend_from_slice(&chunk[..length]);
|
||||
self.try_read_line_buffered()
|
||||
}
|
||||
Err(error)
|
||||
if matches!(
|
||||
error.kind(),
|
||||
io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock
|
||||
) =>
|
||||
{
|
||||
Ok(None)
|
||||
}
|
||||
Err(error) => {
|
||||
self.close();
|
||||
Err(WireError::Io(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn try_read_line_buffered(&mut self) -> Result<Option<Vec<u8>>, WireError> {
|
||||
if let Some(end) = self.buffered.iter().position(|byte| *byte == b'\n') {
|
||||
if end + 1 > MAX_POOL_LINE_BYTES {
|
||||
self.close();
|
||||
return Err(WireError::LineTooLong);
|
||||
}
|
||||
let line = self.buffered.drain(..=end).collect();
|
||||
self.partial_started = if self.buffered.is_empty() {
|
||||
None
|
||||
} else {
|
||||
self.partial_started.or_else(|| Some(Instant::now()))
|
||||
};
|
||||
return Ok(Some(line));
|
||||
}
|
||||
if self.buffered.len() >= MAX_POOL_LINE_BYTES {
|
||||
self.close();
|
||||
return Err(WireError::LineTooLong);
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn send_line_before(&mut self, line: &[u8], deadline: Instant) -> Result<(), WireError> {
|
||||
if self.closed {
|
||||
return Err(WireError::Closed);
|
||||
}
|
||||
if line.is_empty()
|
||||
|| line.len() > MAX_POOL_LINE_BYTES
|
||||
|| line.last() != Some(&b'\n')
|
||||
|| line[..line.len() - 1].contains(&b'\n')
|
||||
{
|
||||
return Err(WireError::InvalidRequest);
|
||||
}
|
||||
let mut sent = 0;
|
||||
while sent < line.len() {
|
||||
let remaining = match Self::remaining(deadline) {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
self.close();
|
||||
return Err(error);
|
||||
}
|
||||
};
|
||||
if let Err(error) = self.stream.set_write_timeout(Some(remaining)) {
|
||||
self.close();
|
||||
return Err(WireError::Io(error));
|
||||
}
|
||||
match self.stream.write(&line[sent..]) {
|
||||
Ok(0) => {
|
||||
self.close();
|
||||
return Err(WireError::Eof);
|
||||
}
|
||||
Ok(length) => sent += length,
|
||||
Err(error) => {
|
||||
self.close();
|
||||
return Err(Self::transport_error(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn send_line(&mut self, line: &[u8]) -> Result<(), WireError> {
|
||||
self.send_line_before(line, Instant::now() + self.write_timeout)
|
||||
}
|
||||
|
||||
fn read_reply(
|
||||
&mut self,
|
||||
request_id: u64,
|
||||
deadline: Instant,
|
||||
early: &mut Vec<Vec<u8>>,
|
||||
early_bytes: &mut usize,
|
||||
) -> Result<Vec<u8>, HandshakeError> {
|
||||
loop {
|
||||
let line = self
|
||||
.read_line_before(deadline)
|
||||
.map_err(HandshakeError::Wire)?;
|
||||
let message: serde_json::Value = serde_json::from_slice(&line)
|
||||
.map_err(|_| HandshakeError::Pool(PoolMessageError::Json))?;
|
||||
if message.get("id").and_then(serde_json::Value::as_u64) == Some(request_id) {
|
||||
return Ok(line);
|
||||
}
|
||||
if !message.get("id").is_none_or(serde_json::Value::is_null)
|
||||
|| !matches!(
|
||||
message.get("method").and_then(serde_json::Value::as_str),
|
||||
Some(
|
||||
"mining.set_difficulty"
|
||||
| "mining.set_version_mask"
|
||||
| "mining.set_extranonce"
|
||||
| "mining.notify"
|
||||
)
|
||||
)
|
||||
{
|
||||
return Err(HandshakeError::Pool(PoolMessageError::WrongMessage));
|
||||
}
|
||||
*early_bytes += line.len();
|
||||
if early.len() >= MAX_EARLY_NOTIFICATIONS || *early_bytes > MAX_EARLY_NOTIFICATION_BYTES
|
||||
{
|
||||
return Err(HandshakeError::TooManyNotifications);
|
||||
}
|
||||
early.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
/// Negotiate BIP310, subscribe, and authorize in that order. The entire
|
||||
/// handshake shares one absolute deadline; early notifications are kept
|
||||
/// for the caller to apply before reading new pool lines.
|
||||
pub fn handshake(
|
||||
&mut self,
|
||||
agent: &str,
|
||||
worker: &str,
|
||||
password: &str,
|
||||
timeout: Duration,
|
||||
) -> Result<PoolHandshake, HandshakeError> {
|
||||
let result = self.handshake_inner(agent, worker, password, timeout);
|
||||
if result.is_err() {
|
||||
self.close();
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn handshake_inner(
|
||||
&mut self,
|
||||
agent: &str,
|
||||
worker: &str,
|
||||
password: &str,
|
||||
timeout: Duration,
|
||||
) -> Result<PoolHandshake, HandshakeError> {
|
||||
let deadline = Instant::now()
|
||||
.checked_add(timeout)
|
||||
.filter(|_| !timeout.is_zero())
|
||||
.ok_or(HandshakeError::Wire(WireError::InvalidRequest))?;
|
||||
let subscribe = subscribe_request(2, agent).map_err(HandshakeError::Wire)?;
|
||||
let authorize = authorize_request(3, worker, password).map_err(HandshakeError::Wire)?;
|
||||
let mut early_notifications = Vec::new();
|
||||
let mut early_bytes = 0;
|
||||
self.send_line_before(configure_request(1).as_bytes(), deadline)
|
||||
.map_err(HandshakeError::Wire)?;
|
||||
let configure = self.read_reply(1, deadline, &mut early_notifications, &mut early_bytes)?;
|
||||
let configure = parse_configure_result(&configure, 1).map_err(HandshakeError::Pool)?;
|
||||
self.send_line_before(subscribe.as_bytes(), deadline)
|
||||
.map_err(HandshakeError::Wire)?;
|
||||
let subscription =
|
||||
self.read_reply(2, deadline, &mut early_notifications, &mut early_bytes)?;
|
||||
let subscription = parse_subscription(&subscription, 2).map_err(HandshakeError::Pool)?;
|
||||
self.send_line_before(authorize.as_bytes(), deadline)
|
||||
.map_err(HandshakeError::Wire)?;
|
||||
let authorized =
|
||||
self.read_reply(3, deadline, &mut early_notifications, &mut early_bytes)?;
|
||||
if !parse_boolean_response(&authorized, 3).map_err(HandshakeError::Pool)? {
|
||||
return Err(HandshakeError::Rejected);
|
||||
}
|
||||
Ok(PoolHandshake {
|
||||
subscription,
|
||||
configure,
|
||||
early_notifications,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Offer BIP310 version rolling and extranonce updates before subscribing.
|
||||
pub fn configure_request(id: u64) -> String {
|
||||
json!({
|
||||
"id": id,
|
||||
"method": "mining.configure",
|
||||
"params": [
|
||||
["version-rolling", "subscribe-extranonce"],
|
||||
{"version-rolling.mask": "1fffe000", "version-rolling.min-bit-count": 0}
|
||||
]
|
||||
})
|
||||
.to_string()
|
||||
+ "\n"
|
||||
}
|
||||
|
||||
pub fn subscribe_request(id: u64, agent: &str) -> Result<String, WireError> {
|
||||
if agent.is_empty() || agent.len() > 128 {
|
||||
return Err(WireError::InvalidRequest);
|
||||
}
|
||||
Ok(json!({"id": id, "method": "mining.subscribe", "params": [agent]}).to_string() + "\n")
|
||||
}
|
||||
|
||||
pub fn authorize_request(id: u64, worker: &str, password: &str) -> Result<String, WireError> {
|
||||
if worker.is_empty() || worker.len() > 128 || password.len() > 256 {
|
||||
return Err(WireError::InvalidRequest);
|
||||
}
|
||||
Ok(
|
||||
json!({"id": id, "method": "mining.authorize", "params": [worker, password]}).to_string()
|
||||
+ "\n",
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Construct SHA-256 Stratum V1 work after a protocol client decodes notify.
|
||||
//! This module assembles headers; it never searches for nonces.
|
||||
|
||||
use crate::pow::sha256d;
|
||||
|
||||
const MAX_COINBASE_BYTES: usize = 65_536;
|
||||
const MAX_MERKLE_BRANCHES: usize = 64;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum JobError {
|
||||
InvalidExtranonceSize,
|
||||
CoinbaseTooLarge,
|
||||
TooManyMerkleBranches,
|
||||
ExtranonceLength,
|
||||
}
|
||||
|
||||
/// Binary fields from `mining.notify`, plus `mining.subscribe` extranonce data.
|
||||
/// `prevhash_stratum` is the eight word-swapped 32-bit words sent on the wire.
|
||||
pub struct Sha256Job {
|
||||
pub prevhash_stratum: [u8; 32],
|
||||
pub coinb1: Vec<u8>,
|
||||
pub coinb2: Vec<u8>,
|
||||
pub merkle_branches: Vec<[u8; 32]>,
|
||||
pub version: u32,
|
||||
pub nbits: u32,
|
||||
pub ntime: u32,
|
||||
pub extranonce1: Vec<u8>,
|
||||
pub extranonce2_size: usize,
|
||||
}
|
||||
|
||||
impl Sha256Job {
|
||||
pub fn validate(&self) -> Result<(), JobError> {
|
||||
if self.extranonce1.len() > 32 || self.extranonce2_size > 32 {
|
||||
return Err(JobError::InvalidExtranonceSize);
|
||||
}
|
||||
let total = self
|
||||
.coinb1
|
||||
.len()
|
||||
.checked_add(self.extranonce1.len())
|
||||
.and_then(|sum| sum.checked_add(self.extranonce2_size))
|
||||
.and_then(|sum| sum.checked_add(self.coinb2.len()));
|
||||
if total.is_none_or(|sum| sum > MAX_COINBASE_BYTES) {
|
||||
return Err(JobError::CoinbaseTooLarge);
|
||||
}
|
||||
if self.merkle_branches.len() > MAX_MERKLE_BRANCHES {
|
||||
return Err(JobError::TooManyMerkleBranches);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn nonce_zero_header(&self, extranonce2: &[u8], ntime: u32) -> Result<[u8; 80], JobError> {
|
||||
self.validate()?;
|
||||
if extranonce2.len() != self.extranonce2_size {
|
||||
return Err(JobError::ExtranonceLength);
|
||||
}
|
||||
let mut coinbase = Vec::with_capacity(
|
||||
self.coinb1.len() + self.extranonce1.len() + extranonce2.len() + self.coinb2.len(),
|
||||
);
|
||||
coinbase.extend_from_slice(&self.coinb1);
|
||||
coinbase.extend_from_slice(&self.extranonce1);
|
||||
coinbase.extend_from_slice(extranonce2);
|
||||
coinbase.extend_from_slice(&self.coinb2);
|
||||
let mut root = sha256d(&coinbase);
|
||||
for branch in &self.merkle_branches {
|
||||
let mut pair = [0_u8; 64];
|
||||
pair[..32].copy_from_slice(&root);
|
||||
pair[32..].copy_from_slice(branch);
|
||||
root = sha256d(&pair);
|
||||
}
|
||||
let mut header = [0_u8; 80];
|
||||
header[..4].copy_from_slice(&self.version.to_le_bytes());
|
||||
for (index, word) in self.prevhash_stratum.chunks_exact(4).enumerate() {
|
||||
for (offset, byte) in word.iter().rev().enumerate() {
|
||||
header[4 + index * 4 + offset] = *byte;
|
||||
}
|
||||
}
|
||||
header[36..68].copy_from_slice(&root);
|
||||
header[68..72].copy_from_slice(&ntime.to_le_bytes());
|
||||
header[72..76].copy_from_slice(&self.nbits.to_le_bytes());
|
||||
Ok(header)
|
||||
}
|
||||
}
|
||||
+441
-4
@@ -1,13 +1,55 @@
|
||||
use alkane_os::{power_decision, target_matrix};
|
||||
use alkane_os::bm1362::{
|
||||
bt8d_for_baud, chain_inactive, encode_job, encode_work as encode_bm1362_work, read_register,
|
||||
read_register_all, set_chip_address, write_register, write_register_all, Job, S19J_PRO,
|
||||
};
|
||||
use alkane_os::bm1366::{decode_nonce_report, encode_work};
|
||||
use alkane_os::bm13xx::{crc16, crc5};
|
||||
use alkane_os::difficulty::PoolDifficulty;
|
||||
use alkane_os::hal_wire::HalClient;
|
||||
use alkane_os::miner::Bm1366Miner;
|
||||
use alkane_os::pow::verify_sha256_report;
|
||||
use alkane_os::pow::VersionMask;
|
||||
use alkane_os::scrypt_pow::verify_scrypt_header;
|
||||
use alkane_os::stratum_session::PoolSession;
|
||||
use alkane_os::stratum_submit::{format_submission, Submission};
|
||||
use alkane_os::stratum_v1::{
|
||||
parse_boolean_response, parse_configure_result, parse_control_notification, parse_notify,
|
||||
parse_subscription, Subscription,
|
||||
};
|
||||
use alkane_os::stratum_wire::{
|
||||
authorize_request, configure_request, subscribe_request, StratumWire,
|
||||
};
|
||||
use alkane_os::stratum_work::Sha256Job;
|
||||
use alkane_os::{power_decision, target_matrix, HardwareBackend};
|
||||
use std::hint::black_box;
|
||||
use std::time::Instant;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn measure(name: &str, iterations: u64, mut f: impl FnMut()) {
|
||||
fn sample(iterations: u64, f: &mut impl FnMut()) -> Duration {
|
||||
let started = Instant::now();
|
||||
for _ in 0..iterations {
|
||||
f();
|
||||
}
|
||||
let ns = started.elapsed().as_nanos() as f64 / iterations as f64;
|
||||
started.elapsed()
|
||||
}
|
||||
|
||||
fn measure(name: &str, minimum_iterations: u64, mut f: impl FnMut()) {
|
||||
for _ in 0..minimum_iterations.min(1000) {
|
||||
f();
|
||||
}
|
||||
let mut iterations = minimum_iterations.max(1);
|
||||
while sample(iterations, &mut f) < Duration::from_millis(20) && iterations < 100_000_000 {
|
||||
iterations = (iterations * 2).min(100_000_000);
|
||||
}
|
||||
let mut measurements = [0_f64; 7];
|
||||
for value in &mut measurements {
|
||||
*value = sample(iterations, &mut f).as_nanos() as f64 / iterations as f64;
|
||||
}
|
||||
measurements.sort_by(f64::total_cmp);
|
||||
let ns = measurements[measurements.len() / 2];
|
||||
println!("BENCH {name} {ns:.3} ns/op");
|
||||
}
|
||||
|
||||
@@ -30,3 +72,398 @@ fn bench_power_policy() {
|
||||
black_box(power_decision(black_box(Some(64_000)), 80_000));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm13xx_crc5() {
|
||||
let command = [0x53, 0x05, 0, 0];
|
||||
measure("bm13xx_crc5", 100_000, || {
|
||||
black_box(crc5(black_box(&command)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm13xx_crc16() {
|
||||
let job = [0x21_u8; 84];
|
||||
measure("bm13xx_crc16", 20_000, || {
|
||||
black_box(crc16(black_box(&job)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_share_verification() {
|
||||
let header = [0_u8; 80];
|
||||
let target = [0xff_u8; 32];
|
||||
measure("share_verification", 20_000, || {
|
||||
black_box(verify_sha256_report(
|
||||
black_box(&header),
|
||||
black_box(0xEDAD_FF38),
|
||||
black_box(0x04B3),
|
||||
black_box(&target),
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_scrypt_share_verification() {
|
||||
let header = [0_u8; 80];
|
||||
let target = [0xff_u8; 32];
|
||||
measure("scrypt_share_verification", 4, || {
|
||||
black_box(verify_scrypt_header(black_box(&header), black_box(&target)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1366_work_encoding() {
|
||||
let header = [0_u8; 80];
|
||||
measure("bm1366_work_encoding", 20_000, || {
|
||||
black_box(encode_work(black_box(&header), black_box(0x18)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1362_commands() {
|
||||
measure("bm1362_commands", 20_000, || {
|
||||
black_box(chain_inactive());
|
||||
black_box(set_chip_address(black_box(8)));
|
||||
black_box(read_register(black_box(0x40), 0x1c));
|
||||
black_box(read_register_all(0));
|
||||
black_box(write_register(black_box(0x40), 0x18, 0x7a31));
|
||||
black_box(write_register_all(0x80, 0));
|
||||
black_box(bt8d_for_baud(black_box(115_200)));
|
||||
black_box(S19J_PRO.chip_addresses().last());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1362_job_encoding() {
|
||||
let job = Job {
|
||||
work_id: 8,
|
||||
num_midstates: 1,
|
||||
start_nonce: 0,
|
||||
nbits: 0x1a1f_2c34,
|
||||
ntime: 0x5e9b_3cd4,
|
||||
merkle_root: [0xa5; 32],
|
||||
previous_hash: [0x5a; 32],
|
||||
version: 0x2000_0000,
|
||||
};
|
||||
measure("bm1362_job_encoding", 20_000, || {
|
||||
black_box(encode_job(black_box(&job)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1362_work_encoding() {
|
||||
let header = [0_u8; 80];
|
||||
measure("bm1362_work_encoding", 20_000, || {
|
||||
black_box(encode_bm1362_work(black_box(&header), black_box(8)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1366_report_decode() {
|
||||
let report = [
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
];
|
||||
measure("bm1366_report_decode", 100_000, || {
|
||||
let _ = black_box(decode_nonce_report(black_box(&report)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_header() {
|
||||
let job = Sha256Job {
|
||||
prevhash_stratum: [0; 32],
|
||||
coinb1: vec![0; 48],
|
||||
coinb2: vec![0; 64],
|
||||
merkle_branches: vec![[0; 32]; 4],
|
||||
version: 0x2000_0000,
|
||||
nbits: 0x1d00_ffff,
|
||||
ntime: 0x6655_4433,
|
||||
extranonce1: vec![0; 4],
|
||||
extranonce2_size: 8,
|
||||
};
|
||||
measure("stratum_header", 5_000, || {
|
||||
black_box(
|
||||
job.nonce_zero_header(black_box(&[0; 8]), job.ntime)
|
||||
.unwrap(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_submission() {
|
||||
let share = Submission {
|
||||
request_id: 1,
|
||||
worker: "miner.worker",
|
||||
job_id: "job-123",
|
||||
extranonce2: &[0; 8],
|
||||
ntime: 0x680f_6a2e,
|
||||
nonce: 0xedad_ff38,
|
||||
base_version: 0x2000_0000,
|
||||
rolled_version: 0x2096_6000,
|
||||
version_mask: Some(VersionMask::new(0x1fff_e000).unwrap()),
|
||||
};
|
||||
measure("stratum_submission", 20_000, || {
|
||||
black_box(format_submission(black_box(&share)).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_authorize_format() {
|
||||
measure("stratum_authorize_format", 20_000, || {
|
||||
black_box(authorize_request(black_box(3), "miner.worker", "password").unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_configure_format() {
|
||||
measure("stratum_configure_format", 20_000, || {
|
||||
black_box(configure_request(black_box(1)));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_subscribe_format() {
|
||||
measure("stratum_subscribe_format", 20_000, || {
|
||||
black_box(subscribe_request(black_box(2), "alkane-os/0.1").unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_subscription_parse() {
|
||||
let line = br#"{"id":1,"result":[[["mining.notify","session"]],"01020304",4],"error":null}"#;
|
||||
measure("stratum_subscription_parse", 20_000, || {
|
||||
black_box(parse_subscription(black_box(line), black_box(1)).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_accept_parse() {
|
||||
let line = br#"{"id":5,"result":true,"error":null}"#;
|
||||
measure("stratum_accept_parse", 20_000, || {
|
||||
black_box(parse_boolean_response(black_box(line), black_box(5)).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_notify_parse() {
|
||||
let line = serde_json::json!({
|
||||
"id": null,
|
||||
"method": "mining.notify",
|
||||
"params": [
|
||||
"job-1",
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
"616263", "78797a", [], "20000000", "1d00ffff", "66554433", true
|
||||
]
|
||||
})
|
||||
.to_string();
|
||||
let subscription = Subscription {
|
||||
extranonce1: vec![1, 2, 3, 4],
|
||||
extranonce2_size: 4,
|
||||
};
|
||||
measure("stratum_notify_parse", 20_000, || {
|
||||
black_box(parse_notify(black_box(line.as_bytes()), &subscription).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_control_parse() {
|
||||
let line = br#"{"id":null,"method":"mining.set_difficulty","params":[2048.5]}"#;
|
||||
measure("stratum_control_parse", 20_000, || {
|
||||
black_box(parse_control_notification(black_box(line), false, false).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_configure_parse() {
|
||||
let line = br#"{"id":1,"result":{"version-rolling":true,"version-rolling.mask":"1fffe000","subscribe-extranonce":true},"error":null}"#;
|
||||
measure("stratum_configure_parse", 20_000, || {
|
||||
black_box(parse_configure_result(black_box(line), 1).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_difficulty_target() {
|
||||
let number = serde_json::from_str("2048.5").unwrap();
|
||||
let difficulty = PoolDifficulty::from_json_number(&number).unwrap();
|
||||
measure("difficulty_target", 20_000, || {
|
||||
black_box(black_box(difficulty).target_le());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_job_issue() {
|
||||
let line = serde_json::json!({
|
||||
"id": null,
|
||||
"method": "mining.notify",
|
||||
"params": [
|
||||
"job-1", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
"616263", "78797a", [], "20000000", "1d00ffff", "66554433", true
|
||||
]
|
||||
})
|
||||
.to_string();
|
||||
let mut session = PoolSession::new(
|
||||
Subscription {
|
||||
extranonce1: vec![1, 2, 3, 4],
|
||||
extranonce2_size: 4,
|
||||
},
|
||||
None,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
session
|
||||
.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
||||
.unwrap();
|
||||
measure("stratum_job_issue", 5_000, || {
|
||||
session.after_hardware_reset();
|
||||
black_box(session.issue_notify(black_box(line.as_bytes())).unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
struct BenchBackend {
|
||||
report: Option<[u8; 11]>,
|
||||
}
|
||||
|
||||
impl HardwareBackend for BenchBackend {
|
||||
type Error = ();
|
||||
|
||||
fn send_frame(&mut self, _chain: u8, frame: &[u8]) -> Result<(), Self::Error> {
|
||||
black_box(frame);
|
||||
Ok(())
|
||||
}
|
||||
fn receive_frame(&mut self, _chain: u8, frame: &mut [u8]) -> Result<usize, Self::Error> {
|
||||
if let Some(report) = self.report {
|
||||
frame[..report.len()].copy_from_slice(&report);
|
||||
Ok(report.len())
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
fn set_chain_reset(&mut self, _chain: u8, _asserted: bool) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
fn set_fan_target(&mut self, _fan: u8, _percent: u8) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
fn read_temperature_mc(&mut self, _sensor: u8) -> Result<i32, Self::Error> {
|
||||
Ok(50_000)
|
||||
}
|
||||
fn set_psu_enabled(&mut self, _enabled: bool) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1366_session_cycle() {
|
||||
let header = [0_u8; 80];
|
||||
let target = [0xff_u8; 32];
|
||||
measure("bm1366_session_cycle", 20_000, || {
|
||||
let mut backend = BenchBackend { report: None };
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner
|
||||
.submit(black_box(header), black_box(target), 0x18)
|
||||
.unwrap();
|
||||
black_box(miner.poll().unwrap());
|
||||
miner.stop().unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_bm1366_captured_report_poll() {
|
||||
let header = [
|
||||
0x00, 0x00, 0x00, 0x20, 0xfc, 0x3a, 0xb8, 0xd4, 0x30, 0x21, 0xf4, 0x5e, 0xb6, 0x62, 0x38,
|
||||
0x9d, 0x0f, 0x79, 0x1f, 0x65, 0x7d, 0xbf, 0x75, 0xa5, 0xcb, 0x93, 0x67, 0x0b, 0x57, 0xb6,
|
||||
0xc2, 0x25, 0x03, 0xb4, 0x21, 0x3a, 0x43, 0xae, 0x10, 0x52, 0xeb, 0xd4, 0x44, 0x43, 0x06,
|
||||
0x8f, 0xe1, 0x78, 0xe2, 0xc6, 0x1c, 0xa4, 0x99, 0x48, 0x46, 0xfd, 0x13, 0x3a, 0xdd, 0xb0,
|
||||
0x4a, 0xab, 0x2b, 0x38, 0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01, 0x2e, 0x6a, 0x8f, 0x06, 0x02,
|
||||
0x17, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
let report = [
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
];
|
||||
let mut target = [0_u8; 32];
|
||||
target[26..28].fill(0xff);
|
||||
let mut backend = BenchBackend {
|
||||
report: Some(report),
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(header, target, 0x18).unwrap();
|
||||
measure("bm1366_captured_report_poll", 20_000, || {
|
||||
let share = miner.poll().unwrap().unwrap();
|
||||
assert_eq!(black_box(share).nonce, 0xedad_ff38);
|
||||
});
|
||||
miner.stop().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_hal_socket_exchange() {
|
||||
let (client, mut server) = UnixStream::pair().unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let mut header = [0_u8; 16];
|
||||
let mut payload = [0_u8; 2];
|
||||
while server.read_exact(&mut header).is_ok() {
|
||||
assert_eq!(&header[..6], b"ALKH\x01\x04");
|
||||
assert_eq!(u32::from_le_bytes(header[12..16].try_into().unwrap()), 2);
|
||||
server.read_exact(&mut payload).unwrap();
|
||||
assert_eq!(payload, [0, 80]);
|
||||
header[6..8].copy_from_slice(&1_u16.to_le_bytes());
|
||||
header[12..16].copy_from_slice(&1_u32.to_le_bytes());
|
||||
server.write_all(&header).unwrap();
|
||||
server.write_all(&[0]).unwrap();
|
||||
}
|
||||
});
|
||||
let mut backend = HalClient::from_stream(client).unwrap();
|
||||
measure("hal_socket_exchange", 100, || {
|
||||
backend.set_fan_target(black_box(0), black_box(80)).unwrap();
|
||||
});
|
||||
drop(backend);
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "microbenchmark"]
|
||||
fn bench_stratum_socket_round_trip() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let (mut server, _) = listener.accept().unwrap();
|
||||
let mut request = [0_u8; 3];
|
||||
while server.read_exact(&mut request).is_ok() {
|
||||
assert_eq!(&request, b"{}\n");
|
||||
server.write_all(b"{}\n").unwrap();
|
||||
}
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_secs(2)).unwrap();
|
||||
measure("stratum_socket_round_trip", 100, || {
|
||||
wire.send_line(black_box(b"{}\n")).unwrap();
|
||||
black_box(wire.read_line().unwrap());
|
||||
});
|
||||
drop(wire);
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::difficulty::{DifficultyError, PoolDifficulty};
|
||||
|
||||
fn target(decimal: &str) -> String {
|
||||
let number = serde_json::from_str(decimal).unwrap();
|
||||
PoolDifficulty::from_json_number(&number)
|
||||
.unwrap()
|
||||
.target_le()
|
||||
.iter()
|
||||
.rev()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exact_share_targets_match_integer_reference() {
|
||||
for (difficulty, expected) in [
|
||||
(
|
||||
"1",
|
||||
"00000000ffff0000000000000000000000000000000000000000000000000000",
|
||||
),
|
||||
(
|
||||
"2",
|
||||
"000000007fff8000000000000000000000000000000000000000000000000000",
|
||||
),
|
||||
(
|
||||
"0.5",
|
||||
"00000001fffe0000000000000000000000000000000000000000000000000000",
|
||||
),
|
||||
(
|
||||
"1.25",
|
||||
"00000000cccc0000000000000000000000000000000000000000000000000000",
|
||||
),
|
||||
(
|
||||
"2048.5",
|
||||
"00000000001ffde021fde021fde021fde021fde021fde021fde021fde021fde0",
|
||||
),
|
||||
(
|
||||
"0.000001",
|
||||
"000f4230bdc00000000000000000000000000000000000000000000000000000",
|
||||
),
|
||||
(
|
||||
"1000000000000000000",
|
||||
"000000000000000000000012724b5f7471d95d3bbefee5e602a7b17261180d8e",
|
||||
),
|
||||
] {
|
||||
assert_eq!(target(difficulty), expected, "difficulty {difficulty}");
|
||||
}
|
||||
assert_eq!(target("1.2e3"), target("1200"));
|
||||
assert_eq!(target("1e-19"), "ff".repeat(32));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_or_unrepresentable_difficulty_is_rejected() {
|
||||
let zero = serde_json::from_str("0").unwrap();
|
||||
assert_eq!(
|
||||
PoolDifficulty::from_json_number(&zero),
|
||||
Err(DifficultyError::Invalid)
|
||||
);
|
||||
let negative = serde_json::from_str("-1").unwrap();
|
||||
assert_eq!(
|
||||
PoolDifficulty::from_json_number(&negative),
|
||||
Err(DifficultyError::OutOfRange)
|
||||
);
|
||||
for value in ["1e20", "1e-20", "1.00000000000000000001"] {
|
||||
let number = serde_json::from_str(value).unwrap();
|
||||
assert_eq!(
|
||||
PoolDifficulty::from_json_number(&number),
|
||||
Err(DifficultyError::OutOfRange),
|
||||
"difficulty {value}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::hal_wire::{HalClient, HalError};
|
||||
use alkane_os::HardwareBackend;
|
||||
use std::io::{Read, Write};
|
||||
use std::os::unix::net::{UnixListener, UnixStream};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn reply(socket: &mut UnixStream, request: &[u8; 16], body: &[u8]) {
|
||||
let mut header = *request;
|
||||
header[6..8].copy_from_slice(&1_u16.to_le_bytes());
|
||||
header[12..16].copy_from_slice(&(body.len() as u32).to_le_bytes());
|
||||
socket.write_all(&header).unwrap();
|
||||
socket.write_all(body).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sdk_backend_round_trip_and_safety_limits() {
|
||||
let (client, mut server) = UnixStream::pair().unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let expected = [
|
||||
(1, vec![2, 3, 0, 0x55, 0xaa, 0x21], vec![0]),
|
||||
(2, vec![2, 8, 0], vec![0, 3, 0, 0xaa, 0x55, 0x01]),
|
||||
(3, vec![2, 1], vec![0]),
|
||||
(4, vec![0, 90], vec![0]),
|
||||
(
|
||||
5,
|
||||
vec![1],
|
||||
[vec![0], 72_500_i32.to_le_bytes().to_vec()].concat(),
|
||||
),
|
||||
(6, vec![1], vec![0]),
|
||||
];
|
||||
for (index, (opcode, request_body, response_body)) in expected.iter().enumerate() {
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
assert_eq!(&header[..4], b"ALKH");
|
||||
assert_eq!(header[4], 1);
|
||||
assert_eq!(header[5], *opcode);
|
||||
assert_eq!(
|
||||
u32::from_le_bytes(header[8..12].try_into().unwrap()),
|
||||
index as u32 + 1
|
||||
);
|
||||
let length = u32::from_le_bytes(header[12..16].try_into().unwrap()) as usize;
|
||||
let mut body = vec![0_u8; length];
|
||||
server.read_exact(&mut body).unwrap();
|
||||
assert_eq!(&body, request_body);
|
||||
reply(&mut server, &header, response_body);
|
||||
}
|
||||
});
|
||||
let mut backend = HalClient::from_stream(client).unwrap();
|
||||
backend.send_frame(2, &[0x55, 0xaa, 0x21]).unwrap();
|
||||
let mut buffer = [0_u8; 8];
|
||||
assert_eq!(backend.receive_frame(2, &mut buffer).unwrap(), 3);
|
||||
assert_eq!(&buffer[..3], &[0xaa, 0x55, 0x01]);
|
||||
backend.set_chain_reset(2, true).unwrap();
|
||||
backend.set_fan_target(0, 90).unwrap();
|
||||
assert_eq!(backend.read_temperature_mc(1).unwrap(), 72_500);
|
||||
backend.set_psu_enabled(true).unwrap();
|
||||
assert!(matches!(
|
||||
backend.set_fan_target(0, 101),
|
||||
Err(HalError::InvalidFanPercent)
|
||||
));
|
||||
assert!(matches!(
|
||||
backend.send_frame(0, &[0_u8; 4094]),
|
||||
Err(HalError::FrameTooLong)
|
||||
));
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_wrong_response_sequence() {
|
||||
let (client, mut server) = UnixStream::pair().unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
let mut body = [0_u8; 1];
|
||||
server.read_exact(&mut body).unwrap();
|
||||
header[8..12].copy_from_slice(&2_u32.to_le_bytes());
|
||||
header[6..8].copy_from_slice(&1_u16.to_le_bytes());
|
||||
header[12..16].copy_from_slice(&1_u32.to_le_bytes());
|
||||
server.write_all(&header).unwrap();
|
||||
let mut one = [0_u8; 1];
|
||||
assert_eq!(server.read(&mut one).unwrap(), 0);
|
||||
});
|
||||
let mut backend = HalClient::from_stream(client).unwrap();
|
||||
assert!(matches!(
|
||||
backend.set_psu_enabled(true),
|
||||
Err(HalError::Protocol("response sequence mismatch"))
|
||||
));
|
||||
assert!(matches!(
|
||||
backend.set_psu_enabled(true),
|
||||
Err(HalError::Protocol("connection is unusable"))
|
||||
));
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_power_off_ends_backend_power_lease() {
|
||||
let (client, mut server) = UnixStream::pair().unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
let mut body = [0_u8; 1];
|
||||
server.read_exact(&mut body).unwrap();
|
||||
assert_eq!(header[5], 6);
|
||||
assert_eq!(body, [0]);
|
||||
reply(&mut server, &header, &[3]);
|
||||
assert_eq!(server.read(&mut body).unwrap(), 0);
|
||||
});
|
||||
let mut backend = HalClient::from_stream(client).unwrap();
|
||||
assert!(matches!(
|
||||
backend.set_psu_enabled(false),
|
||||
Err(HalError::Device(3))
|
||||
));
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_temperature_response_disconnects_power_lease() {
|
||||
let (client, mut server) = UnixStream::pair().unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
let mut request = [0_u8; 1];
|
||||
server.read_exact(&mut request).unwrap();
|
||||
assert_eq!(header[5], 5);
|
||||
reply(&mut server, &header, &[0, 1]);
|
||||
assert_eq!(server.read(&mut request).unwrap(), 0);
|
||||
});
|
||||
let mut backend = HalClient::from_stream(client).unwrap();
|
||||
assert!(matches!(
|
||||
backend.read_temperature_mc(1),
|
||||
Err(HalError::Protocol("invalid temperature response"))
|
||||
));
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_malformed_error_response_and_poisoned_connection() {
|
||||
for body in [&[4_u8][..], &[3_u8, 99][..]] {
|
||||
let (client, mut server) = UnixStream::pair().unwrap();
|
||||
let response = body.to_vec();
|
||||
let worker = thread::spawn(move || {
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
let mut request = [0_u8; 1];
|
||||
server.read_exact(&mut request).unwrap();
|
||||
reply(&mut server, &header, &response);
|
||||
});
|
||||
let mut backend = HalClient::from_stream(client).unwrap();
|
||||
assert!(matches!(
|
||||
backend.set_psu_enabled(true),
|
||||
Err(HalError::Protocol("invalid error response"))
|
||||
));
|
||||
assert!(matches!(
|
||||
backend.set_psu_enabled(true),
|
||||
Err(HalError::Protocol("connection is unusable"))
|
||||
));
|
||||
worker.join().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slow_partial_response_hits_absolute_exchange_deadline() {
|
||||
let (client, mut server) = UnixStream::pair().unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
let mut request = [0_u8; 1];
|
||||
server.read_exact(&mut request).unwrap();
|
||||
header[6..8].copy_from_slice(&1_u16.to_le_bytes());
|
||||
header[12..16].copy_from_slice(&1_u32.to_le_bytes());
|
||||
for byte in header.into_iter().chain([0]) {
|
||||
if server.write_all(&[byte]).is_err() {
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
}
|
||||
});
|
||||
let mut backend = HalClient::from_stream(client).unwrap();
|
||||
let start = Instant::now();
|
||||
assert!(matches!(
|
||||
backend.set_psu_enabled(true),
|
||||
Err(HalError::Io(_))
|
||||
));
|
||||
assert!(start.elapsed() < Duration::from_secs(4));
|
||||
drop(backend);
|
||||
worker.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connects_to_a_local_backend_socket() {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"alkane-hal-{}-{}.sock",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
));
|
||||
let listener = UnixListener::bind(&path).unwrap();
|
||||
let worker = thread::spawn(move || {
|
||||
let (mut server, _) = listener.accept().unwrap();
|
||||
let mut header = [0_u8; 16];
|
||||
server.read_exact(&mut header).unwrap();
|
||||
let mut request = [0_u8; 1];
|
||||
server.read_exact(&mut request).unwrap();
|
||||
reply(&mut server, &header, &[0]);
|
||||
});
|
||||
let mut backend = HalClient::connect(&path).unwrap();
|
||||
backend.set_psu_enabled(true).unwrap();
|
||||
worker.join().unwrap();
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::bm1366::REPORT_BYTES;
|
||||
use alkane_os::miner::{Bm1366Miner, ChipModel, MinerError};
|
||||
use alkane_os::HardwareBackend;
|
||||
|
||||
const HEADER: [u8; 80] = [
|
||||
0x00, 0x00, 0x00, 0x20, 0xfc, 0x3a, 0xb8, 0xd4, 0x30, 0x21, 0xf4, 0x5e, 0xb6, 0x62, 0x38, 0x9d,
|
||||
0x0f, 0x79, 0x1f, 0x65, 0x7d, 0xbf, 0x75, 0xa5, 0xcb, 0x93, 0x67, 0x0b, 0x57, 0xb6, 0xc2, 0x25,
|
||||
0x03, 0xb4, 0x21, 0x3a, 0x43, 0xae, 0x10, 0x52, 0xeb, 0xd4, 0x44, 0x43, 0x06, 0x8f, 0xe1, 0x78,
|
||||
0xe2, 0xc6, 0x1c, 0xa4, 0x99, 0x48, 0x46, 0xfd, 0x13, 0x3a, 0xdd, 0xb0, 0x4a, 0xab, 0x2b, 0x38,
|
||||
0x2c, 0x5b, 0x4c, 0x74, 0xc1, 0x01, 0x2e, 0x6a, 0x8f, 0x06, 0x02, 0x17, 0x00, 0x00, 0x00, 0x00,
|
||||
];
|
||||
const REPORT: [u8; REPORT_BYTES] = [
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
];
|
||||
|
||||
fn diff_one() -> [u8; 32] {
|
||||
let mut target = [0_u8; 32];
|
||||
target[26..28].fill(0xff);
|
||||
target
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct FakeBackend {
|
||||
events: Vec<&'static str>,
|
||||
temperatures: Vec<i32>,
|
||||
powered: bool,
|
||||
fail_off_call: usize,
|
||||
off_calls: usize,
|
||||
fail_reset_call: usize,
|
||||
reset_calls: usize,
|
||||
report: Option<[u8; REPORT_BYTES]>,
|
||||
additional_reports: Vec<[u8; REPORT_BYTES]>,
|
||||
sent: Vec<u8>,
|
||||
}
|
||||
|
||||
impl HardwareBackend for FakeBackend {
|
||||
type Error = &'static str;
|
||||
|
||||
fn send_frame(&mut self, _chain: u8, frame: &[u8]) -> Result<(), Self::Error> {
|
||||
assert!(self.powered);
|
||||
self.events.push("send");
|
||||
self.sent = frame.to_vec();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn receive_frame(&mut self, _chain: u8, frame: &mut [u8]) -> Result<usize, Self::Error> {
|
||||
self.events.push("receive");
|
||||
if let Some(report) = self.report.take().or_else(|| self.additional_reports.pop()) {
|
||||
frame[..REPORT_BYTES].copy_from_slice(&report);
|
||||
Ok(REPORT_BYTES)
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
fn set_chain_reset(&mut self, _chain: u8, asserted: bool) -> Result<(), Self::Error> {
|
||||
assert!(self.powered);
|
||||
self.reset_calls += 1;
|
||||
if self.reset_calls == self.fail_reset_call {
|
||||
return Err("reset failed");
|
||||
}
|
||||
self.events
|
||||
.push(if asserted { "reset-on" } else { "reset-off" });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_fan_target(&mut self, _fan: u8, percent: u8) -> Result<(), Self::Error> {
|
||||
assert!(percent > 0);
|
||||
self.events.push("fan");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_temperature_mc(&mut self, _sensor: u8) -> Result<i32, Self::Error> {
|
||||
self.events.push("temperature");
|
||||
if self.temperatures.is_empty() {
|
||||
return Err("sensor unavailable");
|
||||
}
|
||||
Ok(self.temperatures.remove(0))
|
||||
}
|
||||
|
||||
fn set_psu_enabled(&mut self, enabled: bool) -> Result<(), Self::Error> {
|
||||
self.events.push(if enabled { "psu-on" } else { "psu-off" });
|
||||
if !enabled {
|
||||
self.off_calls += 1;
|
||||
if self.off_calls == self.fail_off_call {
|
||||
return Err("power-off did not complete");
|
||||
}
|
||||
}
|
||||
self.powered = enabled;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_nonce_passes_a_started_miner() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000, 50_000, 50_000],
|
||||
report: Some(REPORT),
|
||||
..Default::default()
|
||||
};
|
||||
{
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(HEADER, diff_one(), 0x18).unwrap();
|
||||
let share = miner.poll().unwrap().unwrap();
|
||||
assert_eq!(share.nonce, 0xedad_ff38);
|
||||
assert_eq!(share.work_id, 0x18);
|
||||
assert_eq!(share.rolled_version, 0x2096_6000);
|
||||
miner.stop().unwrap();
|
||||
}
|
||||
assert!(!backend.powered);
|
||||
assert_eq!(&backend.sent[..5], &[0x55, 0xaa, 0x21, 0x54, 0x18]);
|
||||
assert_eq!(
|
||||
&backend.events[..6],
|
||||
&[
|
||||
"psu-off",
|
||||
"temperature",
|
||||
"fan",
|
||||
"psu-on",
|
||||
"reset-on",
|
||||
"reset-off"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_bm1362_capture_uses_its_job_format_and_shared_report() {
|
||||
// SDK capture bm1362-real-nonces.txt: one real-silicon pool-accepted share.
|
||||
// This checks host framing and proof only; board initialization is separate.
|
||||
let header_hex = concat!(
|
||||
"00000020fc3ab8d43021f45eb662389d0f791f657dbf75a5cb93670b57b6c22503b4213a",
|
||||
"0503969b7384e1215aace8cfc65dae22d8493ad48fec0b7ac3b0ad7b4e7fb0c5",
|
||||
"c0012e6a8f06021700000000"
|
||||
);
|
||||
let mut header = [0_u8; 80];
|
||||
for (byte, pair) in header.iter_mut().zip(header_hex.as_bytes().chunks_exact(2)) {
|
||||
*byte = u8::from_str_radix(std::str::from_utf8(pair).unwrap(), 16).unwrap();
|
||||
}
|
||||
let report = [
|
||||
0xaa, 0x55, 0x33, 0x25, 0x0b, 0x63, 0x00, 0x18, 0x67, 0x6b, 0x95,
|
||||
];
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000; 3],
|
||||
report: Some(report),
|
||||
..Default::default()
|
||||
};
|
||||
{
|
||||
let mut miner =
|
||||
Bm1366Miner::new_for_chip(&mut backend, ChipModel::Bm1362, 0, 0, 0, 100, 80_000)
|
||||
.unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(header, diff_one(), 0x18).unwrap();
|
||||
let share = miner.poll().unwrap().unwrap();
|
||||
assert_eq!(share.nonce, 0x630b_2533);
|
||||
assert_eq!(share.work_id, 0x18);
|
||||
assert_eq!(share.rolled_version, 0x2ced_6000);
|
||||
miner.stop().unwrap();
|
||||
}
|
||||
assert!(!backend.powered);
|
||||
assert_eq!(&backend.sent[..6], &[0x55, 0xaa, 0x21, 0x56, 0x18, 1]);
|
||||
assert_eq!(&backend.sent[10..14], &header[72..76]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hot_or_missing_sensor_stops_power() {
|
||||
let mut hot = FakeBackend {
|
||||
temperatures: vec![80_000],
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut hot, 0, 0, 0, 100, 80_000).unwrap();
|
||||
assert_eq!(miner.start(), Err(MinerError::UnsafeTemperature(80_000)));
|
||||
drop(miner);
|
||||
assert!(!hot.powered);
|
||||
assert!(!hot.events.contains(&"psu-on"));
|
||||
|
||||
let mut missing = FakeBackend::default();
|
||||
let mut miner = Bm1366Miner::new(&mut missing, 0, 0, 0, 100, 80_000).unwrap();
|
||||
assert_eq!(
|
||||
miner.start(),
|
||||
Err(MinerError::Backend("sensor unavailable"))
|
||||
);
|
||||
drop(miner);
|
||||
assert!(!missing.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn temperature_rise_stops_power_and_stale_reports_preserve_mining() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000, 50_000, 90_000],
|
||||
report: Some(REPORT),
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(HEADER, diff_one(), 0x18).unwrap();
|
||||
assert_eq!(miner.poll(), Err(MinerError::UnsafeTemperature(90_000)));
|
||||
assert_eq!(miner.poll(), Err(MinerError::NotRunning));
|
||||
drop(miner);
|
||||
assert!(!backend.powered);
|
||||
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000; 5],
|
||||
report: Some(REPORT),
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(HEADER, diff_one(), 0x20).unwrap();
|
||||
assert_eq!(miner.poll(), Err(MinerError::StaleReport));
|
||||
miner.submit(HEADER, diff_one(), 0x18).unwrap();
|
||||
assert_eq!(miner.poll(), Ok(None));
|
||||
drop(miner);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn earlier_in_flight_job_is_checked_against_its_own_target() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000; 5],
|
||||
report: Some(REPORT),
|
||||
additional_reports: vec![REPORT],
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(HEADER, diff_one(), 0x18).unwrap();
|
||||
miner.submit(HEADER, [0; 32], 0x20).unwrap();
|
||||
assert_eq!(miner.poll().unwrap().unwrap().work_id, 0x18);
|
||||
miner.clear_work();
|
||||
assert_eq!(miner.poll(), Err(MinerError::StaleReport));
|
||||
drop(miner);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn below_target_is_rejected_without_misreporting_a_share() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000, 50_000, 50_000],
|
||||
report: Some(REPORT),
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(HEADER, [0; 32], 0x18).unwrap();
|
||||
assert_eq!(miner.poll(), Err(MinerError::BelowTarget));
|
||||
drop(miner);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_power_off_is_retried_on_drop() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000],
|
||||
fail_off_call: 2,
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
assert_eq!(miner.stop(), Err("power-off did not complete"));
|
||||
drop(miner);
|
||||
assert_eq!(backend.off_calls, 3);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_chain_flush_powers_off_before_work_id_reuse() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000, 50_000],
|
||||
fail_reset_call: 3,
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
assert_eq!(
|
||||
miner.reset_chain(),
|
||||
Err(MinerError::Backend("reset failed"))
|
||||
);
|
||||
assert_eq!(
|
||||
miner.submit(HEADER, diff_one(), 0),
|
||||
Err(MinerError::NotRunning)
|
||||
);
|
||||
drop(miner);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn work_id_stays_reserved_until_confirmed_chain_reset() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000; 6],
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(HEADER, diff_one(), 0x18).unwrap();
|
||||
assert_eq!(
|
||||
miner.submit(HEADER, diff_one(), 0x18),
|
||||
Err(MinerError::WorkIdInUse(0x18))
|
||||
);
|
||||
miner.clear_work();
|
||||
assert_eq!(
|
||||
miner.submit(HEADER, diff_one(), 0x18),
|
||||
Err(MinerError::WorkIdInUse(0x18))
|
||||
);
|
||||
miner.reset_chain().unwrap();
|
||||
miner.submit(HEADER, diff_one(), 0x18).unwrap();
|
||||
miner.stop().unwrap();
|
||||
drop(miner);
|
||||
assert_eq!(
|
||||
backend
|
||||
.events
|
||||
.iter()
|
||||
.filter(|event| **event == "send")
|
||||
.count(),
|
||||
2
|
||||
);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_work_request_still_checks_temperature() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000, 50_000, 90_000],
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
miner.submit(HEADER, diff_one(), 0x18).unwrap();
|
||||
assert_eq!(
|
||||
miner.submit(HEADER, diff_one(), 0x18),
|
||||
Err(MinerError::UnsafeTemperature(90_000))
|
||||
);
|
||||
drop(miner);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unconfirmed_power_off_blocks_mining_until_shutdown_retries() {
|
||||
let mut backend = FakeBackend {
|
||||
temperatures: vec![50_000],
|
||||
fail_off_call: 2,
|
||||
..Default::default()
|
||||
};
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
assert_eq!(miner.stop(), Err("power-off did not complete"));
|
||||
assert_eq!(
|
||||
miner.submit(HEADER, diff_one(), 0),
|
||||
Err(MinerError::Faulted)
|
||||
);
|
||||
assert_eq!(miner.poll(), Err(MinerError::Faulted));
|
||||
assert_eq!(miner.reset_chain(), Err(MinerError::Faulted));
|
||||
miner.stop().unwrap();
|
||||
assert_eq!(
|
||||
miner.submit(HEADER, diff_one(), 0),
|
||||
Err(MinerError::NotRunning)
|
||||
);
|
||||
drop(miner);
|
||||
assert!(!backend.powered);
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::miner::{Bm1366Miner, MinerError, ValidShare};
|
||||
use alkane_os::pow::VersionMask;
|
||||
use alkane_os::stratum_session::{DispatchError, PoolSession, SessionError};
|
||||
use alkane_os::stratum_v1::Subscription;
|
||||
use alkane_os::HardwareBackend;
|
||||
|
||||
fn session() -> PoolSession {
|
||||
PoolSession::new(
|
||||
Subscription {
|
||||
extranonce1: vec![1, 2, 3, 4],
|
||||
extranonce2_size: 4,
|
||||
},
|
||||
Some(VersionMask::new(0x1fff_e000).unwrap()),
|
||||
true,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn notify(id: &str, clean: bool) -> String {
|
||||
serde_json::json!({
|
||||
"id": null,
|
||||
"method": "mining.notify",
|
||||
"params": [
|
||||
id,
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
"616263", "78797a", [], "20000000", "1d00ffff", "66554433", clean
|
||||
]
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn difficulty_changes_on_next_job_and_old_work_keeps_its_target() {
|
||||
let mut pool = session();
|
||||
assert_eq!(
|
||||
pool.issue_notify(notify("early", true).as_bytes()).err(),
|
||||
Some(SessionError::MissingDifficulty)
|
||||
);
|
||||
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
||||
.unwrap();
|
||||
let first = pool.issue_notify(notify("first", true).as_bytes()).unwrap();
|
||||
assert!(first.clean_jobs);
|
||||
assert_eq!(first.work_id, 0);
|
||||
assert_eq!(&first.target_le[26..28], &[0xff, 0xff]);
|
||||
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[2]}"#)
|
||||
.unwrap();
|
||||
let second = pool
|
||||
.issue_notify(notify("second", false).as_bytes())
|
||||
.unwrap();
|
||||
assert!(!second.clean_jobs);
|
||||
assert_eq!(second.work_id, 8);
|
||||
assert_eq!(&second.target_le[26..28], &[0xff, 0x7f]);
|
||||
assert_ne!(first.header, second.header);
|
||||
assert_eq!(
|
||||
pool.control(br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#),
|
||||
Ok(true)
|
||||
);
|
||||
assert_eq!(
|
||||
pool.submission(
|
||||
ValidShare {
|
||||
nonce: 0xedad_ff38,
|
||||
work_id: first.work_id,
|
||||
rolled_version: 0x2000_0000,
|
||||
hash_le: [0; 32],
|
||||
},
|
||||
"worker",
|
||||
10,
|
||||
),
|
||||
Err(SessionError::StaleShare)
|
||||
);
|
||||
assert_eq!(
|
||||
pool.control(br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#),
|
||||
Ok(false)
|
||||
);
|
||||
let clean = pool.issue_notify(notify("third", true).as_bytes()).unwrap();
|
||||
assert!(clean.clean_jobs);
|
||||
assert_eq!(clean.work_id, 16);
|
||||
let old = ValidShare {
|
||||
nonce: 0xedad_ff38,
|
||||
work_id: first.work_id,
|
||||
rolled_version: 0x2000_0000,
|
||||
hash_le: [0; 32],
|
||||
};
|
||||
assert_eq!(
|
||||
pool.submission(old, "worker", 10),
|
||||
Err(SessionError::StaleShare)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extranonce_change_invalidates_jobs_without_reusing_asic_ids() {
|
||||
let mut pool = session();
|
||||
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
||||
.unwrap();
|
||||
let old = pool.issue_notify(notify("old", true).as_bytes()).unwrap();
|
||||
assert!(pool
|
||||
.control(br#"{"id":null,"method":"mining.set_extranonce","params":["aabbccdd",4]}"#)
|
||||
.unwrap());
|
||||
let new = pool.issue_notify(notify("new", false).as_bytes()).unwrap();
|
||||
assert_eq!(new.work_id, 8);
|
||||
assert_ne!(old.header, new.header);
|
||||
for index in 2..32 {
|
||||
let work = pool.issue_notify(notify("more", false).as_bytes()).unwrap();
|
||||
assert_eq!(work.work_id, (index as u8) << 3);
|
||||
}
|
||||
assert_eq!(
|
||||
pool.issue_notify(notify("exhausted", false).as_bytes())
|
||||
.err(),
|
||||
Some(SessionError::WorkIdsExhausted)
|
||||
);
|
||||
pool.after_hardware_reset();
|
||||
assert_eq!(
|
||||
pool.issue_notify(notify("after-reset", false).as_bytes())
|
||||
.unwrap()
|
||||
.work_id,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_length_extranonce_requires_distinct_header_until_hardware_reset() {
|
||||
let mut pool = PoolSession::new(
|
||||
Subscription {
|
||||
extranonce1: vec![1],
|
||||
extranonce2_size: 0,
|
||||
},
|
||||
None,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
||||
.unwrap();
|
||||
pool.issue_notify(notify("one", true).as_bytes()).unwrap();
|
||||
assert_eq!(
|
||||
pool.issue_notify(notify("two", false).as_bytes()).err(),
|
||||
Some(SessionError::ExtranonceExhausted)
|
||||
);
|
||||
let distinct = notify("two", true).replace("66554433", "66554434");
|
||||
pool.issue_notify(distinct.as_bytes()).unwrap();
|
||||
pool.after_hardware_reset();
|
||||
pool.issue_notify(notify("three", true).as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_byte_extranonce_restarts_only_for_a_distinct_header_template() {
|
||||
let mut pool = PoolSession::new(
|
||||
Subscription {
|
||||
extranonce1: vec![1],
|
||||
extranonce2_size: 1,
|
||||
},
|
||||
None,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
pool.control(br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#)
|
||||
.unwrap();
|
||||
for index in 0..256 {
|
||||
if index != 0 && index % 32 == 0 {
|
||||
pool.after_hardware_reset();
|
||||
}
|
||||
pool.issue_notify(notify("same", false).as_bytes()).unwrap();
|
||||
}
|
||||
pool.after_hardware_reset();
|
||||
assert_eq!(
|
||||
pool.issue_notify(notify("same", false).as_bytes()).err(),
|
||||
Some(SessionError::ExtranonceExhausted)
|
||||
);
|
||||
let distinct = notify("new", false).replace("66554433", "66554434");
|
||||
pool.issue_notify(distinct.as_bytes()).unwrap();
|
||||
pool.after_hardware_reset();
|
||||
for index in 0..256_u32 {
|
||||
if index != 0 && index % 32 == 0 {
|
||||
pool.after_hardware_reset();
|
||||
}
|
||||
let ntime = format!("{index:08x}");
|
||||
let next = notify("fresh", false).replace("66554433", &ntime);
|
||||
pool.issue_notify(next.as_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
struct FakeBackend {
|
||||
powered: bool,
|
||||
sent: usize,
|
||||
report: Option<[u8; 11]>,
|
||||
}
|
||||
|
||||
impl HardwareBackend for FakeBackend {
|
||||
type Error = &'static str;
|
||||
|
||||
fn send_frame(&mut self, _chain: u8, frame: &[u8]) -> Result<(), Self::Error> {
|
||||
assert!(self.powered);
|
||||
assert_eq!(&frame[..4], &[0x55, 0xaa, 0x21, 0x54]);
|
||||
self.sent += 1;
|
||||
Ok(())
|
||||
}
|
||||
fn receive_frame(&mut self, _chain: u8, frame: &mut [u8]) -> Result<usize, Self::Error> {
|
||||
match self.report.take() {
|
||||
Some(report) => {
|
||||
frame[..11].copy_from_slice(&report);
|
||||
Ok(11)
|
||||
}
|
||||
None => Ok(0),
|
||||
}
|
||||
}
|
||||
fn set_chain_reset(&mut self, _chain: u8, _asserted: bool) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
fn set_fan_target(&mut self, _fan: u8, _percent: u8) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
fn read_temperature_mc(&mut self, _sensor: u8) -> Result<i32, Self::Error> {
|
||||
Ok(50_000)
|
||||
}
|
||||
fn set_psu_enabled(&mut self, enabled: bool) -> Result<(), Self::Error> {
|
||||
self.powered = enabled;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pool_job_dispatch_and_version_mask_invalidation_reach_the_miner() {
|
||||
let mut backend = FakeBackend {
|
||||
powered: false,
|
||||
sent: 0,
|
||||
report: Some([
|
||||
0xaa, 0x55, 0x38, 0xff, 0xad, 0xed, 0, 0x18, 0x04, 0xb3, 0x94,
|
||||
]),
|
||||
};
|
||||
{
|
||||
let mut miner = Bm1366Miner::new(&mut backend, 0, 0, 0, 100, 80_000).unwrap();
|
||||
miner.start().unwrap();
|
||||
let mut pool = session();
|
||||
pool.control_with_miner(
|
||||
&mut miner,
|
||||
br#"{"id":null,"method":"mining.set_difficulty","params":[1]}"#,
|
||||
)
|
||||
.unwrap();
|
||||
for index in 0..4 {
|
||||
let work = pool
|
||||
.issue_to_miner(&mut miner, notify("job", index == 0).as_bytes())
|
||||
.unwrap();
|
||||
assert_eq!(work.work_id, (index as u8) << 3);
|
||||
}
|
||||
pool.control_with_miner(
|
||||
&mut miner,
|
||||
br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(miner.poll(), Err(MinerError::StaleReport));
|
||||
miner.stop().unwrap();
|
||||
assert_eq!(
|
||||
pool.issue_to_miner(&mut miner, notify("stopped", false).as_bytes())
|
||||
.err(),
|
||||
Some(DispatchError::Miner(MinerError::NotRunning))
|
||||
);
|
||||
}
|
||||
assert!(!backend.powered);
|
||||
assert_eq!(backend.sent, 4);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::pow::VersionMask;
|
||||
use alkane_os::stratum_submit::{format_submission, Submission, SubmitError};
|
||||
|
||||
fn share() -> Submission<'static> {
|
||||
Submission {
|
||||
request_id: 7,
|
||||
worker: "miner.worker",
|
||||
job_id: "pool-job",
|
||||
extranonce2: &[0, 1, 0xfe, 0xff],
|
||||
ntime: 0x680f6a2e,
|
||||
nonce: 0xedadff38,
|
||||
base_version: 0x20000000,
|
||||
rolled_version: 0x20966000,
|
||||
version_mask: Some(VersionMask::new(0x1fffe000).unwrap()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_bm1366_version_is_represented_under_negotiated_mask() {
|
||||
assert_eq!(
|
||||
format_submission(&share()).unwrap(),
|
||||
"{\"id\":7,\"method\":\"mining.submit\",\"params\":[\"miner.worker\",\"pool-job\",\"0001feff\",\"680f6a2e\",\"edadff38\",\"00966000\"]}\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unnegotiated_or_narrow_mask_rejects_rolled_share() {
|
||||
let mut value = share();
|
||||
value.version_mask = None;
|
||||
assert_eq!(
|
||||
format_submission(&value),
|
||||
Err(SubmitError::UnrepresentableVersion)
|
||||
);
|
||||
value.version_mask = Some(VersionMask::new(0x0060_0000).unwrap());
|
||||
assert_eq!(
|
||||
format_submission(&value),
|
||||
Err(SubmitError::UnrepresentableVersion)
|
||||
);
|
||||
value.rolled_version = value.base_version;
|
||||
value.version_mask = None;
|
||||
assert_eq!(
|
||||
format_submission(&value).unwrap(),
|
||||
"{\"id\":7,\"method\":\"mining.submit\",\"params\":[\"miner.worker\",\"pool-job\",\"0001feff\",\"680f6a2e\",\"edadff38\"]}\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pool_fields_are_escaped_and_bounded() {
|
||||
let mut value = share();
|
||||
value.worker = "worker\"\\\n";
|
||||
value.job_id = "job\t\u{0001}";
|
||||
let wire = format_submission(&value).unwrap();
|
||||
assert!(wire.contains("\"worker\\\"\\\\\\n\""));
|
||||
assert!(wire.contains("\"job\\t\\u0001\""));
|
||||
value.worker = "";
|
||||
assert_eq!(format_submission(&value), Err(SubmitError::EmptyWorker));
|
||||
value.worker = "worker";
|
||||
value.job_id = "";
|
||||
assert_eq!(format_submission(&value), Err(SubmitError::EmptyJobId));
|
||||
value.job_id = "job";
|
||||
value.extranonce2 = &[0; 33];
|
||||
assert_eq!(
|
||||
format_submission(&value),
|
||||
Err(SubmitError::ExtranonceTooLong)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::difficulty::PoolDifficulty;
|
||||
use alkane_os::stratum_v1::{
|
||||
parse_boolean_response, parse_configure_response, parse_configure_result,
|
||||
parse_control_notification, parse_notify, parse_subscription, PoolMessageError,
|
||||
PoolNotification, Subscription,
|
||||
};
|
||||
|
||||
const SUBSCRIBE: &str =
|
||||
r#"{"id":1,"result":[[["mining.notify","session"]],"01020304",4],"error":null}"#;
|
||||
fn notify() -> String {
|
||||
serde_json::json!({
|
||||
"id": null,
|
||||
"method": "mining.notify",
|
||||
"params": [
|
||||
"job-1",
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
"616263", "78797a",
|
||||
["202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"],
|
||||
"20000000", "1d00ffff", "66554433", true
|
||||
]
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subscribe_and_notify_construct_expected_asic_header() {
|
||||
let subscription = parse_subscription(SUBSCRIBE.as_bytes(), 1).unwrap();
|
||||
let line = notify();
|
||||
let job = parse_notify(line.as_bytes(), &subscription).unwrap();
|
||||
assert_eq!(job.id, "job-1");
|
||||
assert!(job.clean_jobs);
|
||||
let header = job
|
||||
.work
|
||||
.nonce_zero_header(&[5, 6, 7, 8], job.work.ntime)
|
||||
.unwrap();
|
||||
let hex: String = header.iter().map(|byte| format!("{byte:02x}")).collect();
|
||||
assert_eq!(
|
||||
hex,
|
||||
concat!(
|
||||
"0000002003020100070605040b0a09080f0e0d0c13121110171615141b1a19181f1e1d1c",
|
||||
"9b9bc91f078af83ff63d00c6da09b5bc4dd26e2d8c0df8ab16b933a259a05881",
|
||||
"33445566ffff001d00000000"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_pool_messages_fail_before_work_issuance() {
|
||||
assert!(matches!(
|
||||
parse_subscription(SUBSCRIBE.as_bytes(), 2),
|
||||
Err(PoolMessageError::WrongMessage)
|
||||
));
|
||||
let subscription = parse_subscription(SUBSCRIBE.as_bytes(), 1).unwrap();
|
||||
let malformed = notify().replace("66554433", "6655443g");
|
||||
assert!(matches!(
|
||||
parse_notify(malformed.as_bytes(), &subscription),
|
||||
Err(PoolMessageError::InvalidHex)
|
||||
));
|
||||
let wrong_method = notify().replace("mining.notify", "mining.submit");
|
||||
assert!(matches!(
|
||||
parse_notify(wrong_method.as_bytes(), &subscription),
|
||||
Err(PoolMessageError::WrongMessage)
|
||||
));
|
||||
let too_large = vec![b' '; 131_073];
|
||||
assert!(matches!(
|
||||
parse_notify(&too_large, &subscription),
|
||||
Err(PoolMessageError::TooLarge)
|
||||
));
|
||||
let too_large_coinbase = notify().replace("616263", &"aa".repeat(65_537));
|
||||
assert!(matches!(
|
||||
parse_notify(too_large_coinbase.as_bytes(), &subscription),
|
||||
Err(PoolMessageError::TooLarge)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn version_rolling_negotiation_and_immediate_mask_change() {
|
||||
let response = br#"{"id":3,"result":{"version-rolling":true,"version-rolling.mask":"1fffe000"},"error":null}"#;
|
||||
let mask = parse_configure_response(response, 3).unwrap().unwrap();
|
||||
assert_eq!(mask.bits(), 0x1fff_e000);
|
||||
let update = br#"{"id":null,"method":"mining.set_version_mask","params":["00006000"]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(update, true, false).unwrap(),
|
||||
PoolNotification::SetVersionMask(alkane_os::pow::VersionMask::new(0x6000).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_control_notification(update, false, false),
|
||||
Err(PoolMessageError::Unnegotiated)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_configure_response(response, 4),
|
||||
Err(PoolMessageError::WrongMessage)
|
||||
);
|
||||
let declined = br#"{"id":3,"result":{"version-rolling":false},"error":null}"#;
|
||||
assert_eq!(parse_configure_response(declined, 3), Ok(None));
|
||||
let unsafe_mask = br#"{"id":3,"result":{"version-rolling":true,"version-rolling.mask":"ffffffff"},"error":null}"#;
|
||||
assert_eq!(
|
||||
parse_configure_response(unsafe_mask, 3),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
let absent_mask = br#"{"id":3,"result":{"version-rolling":true},"error":null}"#;
|
||||
assert_eq!(
|
||||
parse_configure_response(absent_mask, 3),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_bip310_extensions_and_boolean_responses_are_checked() {
|
||||
let accepted = br#"{"id":3,"result":{"version-rolling":true,"version-rolling.mask":"1fffe000","subscribe-extranonce":true},"error":null}"#;
|
||||
let result = parse_configure_result(accepted, 3).unwrap();
|
||||
assert_eq!(result.version_mask.unwrap().bits(), 0x1fff_e000);
|
||||
assert!(result.subscribe_extranonce);
|
||||
let declined =
|
||||
br#"{"id":3,"result":{"version-rolling":false,"subscribe-extranonce":false},"error":null}"#;
|
||||
let result = parse_configure_result(declined, 3).unwrap();
|
||||
assert_eq!(result.version_mask, None);
|
||||
assert!(!result.subscribe_extranonce);
|
||||
let unavailable = br#"{"id":3,"result":{"version-rolling":"unavailable","subscribe-extranonce":"unsupported"},"error":null}"#;
|
||||
let result = parse_configure_result(unavailable, 3).unwrap();
|
||||
assert_eq!(result.version_mask, None);
|
||||
assert!(!result.subscribe_extranonce);
|
||||
assert_eq!(
|
||||
parse_configure_result(
|
||||
br#"{"id":3,"result":{"version-rolling":false},"error":null}"#,
|
||||
3
|
||||
),
|
||||
Ok(alkane_os::stratum_v1::ConfigureResult {
|
||||
version_mask: None,
|
||||
subscribe_extranonce: false,
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
parse_configure_result(
|
||||
br#"{"id":3,"result":{"version-rolling":false,"subscribe-extranonce":null},"error":null}"#,
|
||||
3
|
||||
),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_boolean_response(br#"{"id":5,"result":true,"error":null}"#, 5),
|
||||
Ok(true)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_boolean_response(br#"{"id":5,"result":false,"error":[23,"stale"]}"#, 5),
|
||||
Ok(false)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_boolean_response(
|
||||
br#"{"id":5,"result":null,"error":[21,"Job not found",null]}"#,
|
||||
5
|
||||
),
|
||||
Ok(false)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_boolean_response(br#"{"id":5,"result":true,"error":[23,"stale"]}"#, 5),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_boolean_response(br#"{"id":6,"result":true,"error":null}"#, 5),
|
||||
Err(PoolMessageError::WrongMessage)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn difficulty_and_extranonce_notifications_validate_shape_and_negotiation() {
|
||||
let difficulty = br#"{"id":null,"method":"mining.set_difficulty","params":[2048.5]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(difficulty, false, false),
|
||||
Ok(PoolNotification::SetDifficulty(
|
||||
PoolDifficulty::from_json_number(&serde_json::from_str("2048.5").unwrap()).unwrap()
|
||||
))
|
||||
);
|
||||
let extranonce = br#"{"id":null,"method":"mining.set_extranonce","params":["aabbccdd",4]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(extranonce, false, true),
|
||||
Ok(PoolNotification::SetExtranonce(Subscription {
|
||||
extranonce1: vec![0xaa, 0xbb, 0xcc, 0xdd],
|
||||
extranonce2_size: 4,
|
||||
}))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_control_notification(extranonce, false, false),
|
||||
Err(PoolMessageError::Unnegotiated)
|
||||
);
|
||||
for value in ["0", "-1", "\"2\"", "null"] {
|
||||
let line =
|
||||
format!("{{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[{value}]}}");
|
||||
assert_eq!(
|
||||
parse_control_notification(line.as_bytes(), false, false),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
}
|
||||
let oversize = format!(
|
||||
"{{\"id\":null,\"method\":\"mining.set_extranonce\",\"params\":[\"{}\",1]}}",
|
||||
"ab".repeat(33)
|
||||
);
|
||||
assert_eq!(
|
||||
parse_control_notification(oversize.as_bytes(), false, true),
|
||||
Err(PoolMessageError::InvalidField)
|
||||
);
|
||||
let response_id = br#"{"id":5,"method":"mining.set_difficulty","params":[2]}"#;
|
||||
assert_eq!(
|
||||
parse_control_notification(response_id, false, false),
|
||||
Err(PoolMessageError::WrongMessage)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::stratum_v1::MAX_POOL_LINE_BYTES;
|
||||
use alkane_os::stratum_wire::{
|
||||
authorize_request, configure_request, subscribe_request, HandshakeError, StratumWire, WireError,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn request_frames_escape_pool_credentials_and_advertise_supported_bits() {
|
||||
let configure: Value = serde_json::from_str(&configure_request(1)).unwrap();
|
||||
assert_eq!(configure["method"], "mining.configure");
|
||||
assert_eq!(configure["params"][1]["version-rolling.mask"], "1fffe000");
|
||||
assert_eq!(configure["params"][0][1], "subscribe-extranonce");
|
||||
let subscribe: Value =
|
||||
serde_json::from_str(&subscribe_request(2, "alkane-os/0.1").unwrap()).unwrap();
|
||||
assert_eq!(subscribe["params"][0], "alkane-os/0.1");
|
||||
let authorized = authorize_request(3, "worker\"name", "secret\nvalue").unwrap();
|
||||
assert_eq!(authorized.as_bytes().last(), Some(&b'\n'));
|
||||
let authorize: Value = serde_json::from_str(&authorized).unwrap();
|
||||
assert_eq!(authorize["params"][0], "worker\"name");
|
||||
assert_eq!(authorize["params"][1], "secret\nvalue");
|
||||
assert!(matches!(
|
||||
subscribe_request(2, ""),
|
||||
Err(WireError::InvalidRequest)
|
||||
));
|
||||
assert!(matches!(
|
||||
authorize_request(3, "", "pass"),
|
||||
Err(WireError::InvalidRequest)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loopback_frames_preserve_pipelined_lines_and_reject_invalid_sends() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
stream.write_all(b"{\"id\":1}\n{\"id\":2}\n").unwrap();
|
||||
let mut request = [0_u8; 3];
|
||||
stream.read_exact(&mut request).unwrap();
|
||||
assert_eq!(&request, b"{}\n");
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_secs(2)).unwrap();
|
||||
assert_eq!(wire.read_line().unwrap(), b"{\"id\":1}\n");
|
||||
assert_eq!(wire.read_line().unwrap(), b"{\"id\":2}\n");
|
||||
assert!(matches!(
|
||||
wire.send_line(b"bad\nline\n"),
|
||||
Err(WireError::InvalidRequest)
|
||||
));
|
||||
wire.send_line(b"{}\n").unwrap();
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oversized_pool_line_poison_closes_the_connection() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
let mut line = vec![b'a'; MAX_POOL_LINE_BYTES];
|
||||
line.push(b'\n');
|
||||
let _ = stream.write_all(&line);
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_secs(2)).unwrap();
|
||||
assert!(matches!(wire.read_line(), Err(WireError::LineTooLong)));
|
||||
assert!(matches!(wire.read_line(), Err(WireError::Closed)));
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trickled_pool_line_hits_the_absolute_deadline() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
for byte in b"{123}\n" {
|
||||
if stream.write_all(&[*byte]).is_err() {
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(60));
|
||||
}
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_millis(130)).unwrap();
|
||||
assert!(matches!(wire.read_line(), Err(WireError::Timeout)));
|
||||
assert!(matches!(wire.send_line(b"{}\n"), Err(WireError::Closed)));
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn buffered_complete_lines_do_not_extend_a_partial_line_deadline() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
stream.write_all(b"first\nsecond\nunfinished").unwrap();
|
||||
thread::sleep(Duration::from_secs(3));
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_secs(2)).unwrap();
|
||||
let first = loop {
|
||||
if let Some(line) = wire.try_read_line().unwrap() {
|
||||
break line;
|
||||
}
|
||||
};
|
||||
assert_eq!(first, b"first\n");
|
||||
thread::sleep(Duration::from_millis(1300));
|
||||
assert_eq!(wire.try_read_line().unwrap(), Some(b"second\n".to_vec()));
|
||||
thread::sleep(Duration::from_millis(800));
|
||||
assert!(matches!(wire.try_read_line(), Err(WireError::Timeout)));
|
||||
assert!(wire.is_closed());
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idle_timeout_keeps_connection_available_for_later_messages() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let (release, wait) = std::sync::mpsc::channel();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
wait.recv().unwrap();
|
||||
stream
|
||||
.write_all(b"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[1]}\n")
|
||||
.unwrap();
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_millis(130)).unwrap();
|
||||
assert!(matches!(wire.read_line(), Err(WireError::Timeout)));
|
||||
release.send(()).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_slice::<Value>(&wire.read_line().unwrap()).unwrap()["method"],
|
||||
"mining.set_difficulty"
|
||||
);
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handshake_preserves_early_notifications_and_authorizes_worker() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
let mut input = BufReader::new(stream.try_clone().unwrap());
|
||||
let mut line = String::new();
|
||||
input.read_line(&mut line).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&line).unwrap()["method"],
|
||||
"mining.configure"
|
||||
);
|
||||
stream.write_all(b"{\"id\":1,\"result\":{\"version-rolling\":true,\"version-rolling.mask\":\"1fffe000\",\"subscribe-extranonce\":true},\"error\":null}\n").unwrap();
|
||||
line.clear();
|
||||
input.read_line(&mut line).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&line).unwrap()["method"],
|
||||
"mining.subscribe"
|
||||
);
|
||||
stream.write_all(b"{\"id\":null,\"method\":\"mining.set_difficulty\",\"params\":[1]}\n{\"id\":2,\"result\":[[],\"01020304\",4],\"error\":null}\n").unwrap();
|
||||
line.clear();
|
||||
input.read_line(&mut line).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&line).unwrap()["method"],
|
||||
"mining.authorize"
|
||||
);
|
||||
stream
|
||||
.write_all(b"{\"id\":3,\"result\":true,\"error\":null}\n")
|
||||
.unwrap();
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_secs(2)).unwrap();
|
||||
let result = wire
|
||||
.handshake(
|
||||
"alkane-os/0.1",
|
||||
"miner.worker",
|
||||
"password",
|
||||
Duration::from_secs(2),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result.subscription.extranonce1, [1, 2, 3, 4]);
|
||||
assert_eq!(result.subscription.extranonce2_size, 4);
|
||||
assert!(result.configure.subscribe_extranonce);
|
||||
assert_eq!(result.configure.version_mask.unwrap().bits(), 0x1fff_e000);
|
||||
assert_eq!(result.early_notifications.len(), 1);
|
||||
assert_eq!(
|
||||
serde_json::from_slice::<Value>(&result.early_notifications[0]).unwrap()["method"],
|
||||
"mining.set_difficulty"
|
||||
);
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejected_worker_closes_the_handshake_connection() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
let mut input = BufReader::new(stream.try_clone().unwrap());
|
||||
let mut line = String::new();
|
||||
input.read_line(&mut line).unwrap();
|
||||
stream.write_all(b"{\"id\":1,\"result\":{\"version-rolling\":false,\"subscribe-extranonce\":false},\"error\":null}\n").unwrap();
|
||||
line.clear();
|
||||
input.read_line(&mut line).unwrap();
|
||||
stream
|
||||
.write_all(b"{\"id\":2,\"result\":[[],\"01020304\",4],\"error\":null}\n")
|
||||
.unwrap();
|
||||
line.clear();
|
||||
input.read_line(&mut line).unwrap();
|
||||
stream
|
||||
.write_all(b"{\"id\":3,\"result\":false,\"error\":[24,\"unauthorized\"]}\n")
|
||||
.unwrap();
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_secs(2)).unwrap();
|
||||
assert!(matches!(
|
||||
wire.handshake("alkane-os/0.1", "bad", "password", Duration::from_secs(2)),
|
||||
Err(HandshakeError::Rejected)
|
||||
));
|
||||
assert!(matches!(wire.read_line(), Err(WireError::Closed)));
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_handshake_reply_id_is_rejected_and_connection_closed() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let server = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
let mut line = String::new();
|
||||
BufReader::new(stream.try_clone().unwrap())
|
||||
.read_line(&mut line)
|
||||
.unwrap();
|
||||
stream
|
||||
.write_all(b"{\"id\":9,\"result\":true,\"error\":null}\n")
|
||||
.unwrap();
|
||||
});
|
||||
let mut wire = StratumWire::connect(address, Duration::from_secs(2)).unwrap();
|
||||
assert!(matches!(
|
||||
wire.handshake(
|
||||
"alkane-os/0.1",
|
||||
"miner.worker",
|
||||
"password",
|
||||
Duration::from_secs(2)
|
||||
),
|
||||
Err(HandshakeError::Pool(
|
||||
alkane_os::stratum_v1::PoolMessageError::WrongMessage
|
||||
))
|
||||
));
|
||||
assert!(matches!(wire.read_line(), Err(WireError::Closed)));
|
||||
server.join().unwrap();
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
use alkane_os::bm1366::encode_work;
|
||||
use alkane_os::stratum_work::{JobError, Sha256Job};
|
||||
|
||||
fn fixture() -> Sha256Job {
|
||||
let mut prevhash_stratum = [0_u8; 32];
|
||||
let mut branch = [0_u8; 32];
|
||||
for (index, byte) in prevhash_stratum.iter_mut().enumerate() {
|
||||
*byte = index as u8;
|
||||
}
|
||||
for (index, byte) in branch.iter_mut().enumerate() {
|
||||
*byte = (index + 32) as u8;
|
||||
}
|
||||
Sha256Job {
|
||||
prevhash_stratum,
|
||||
coinb1: b"abc".to_vec(),
|
||||
coinb2: b"xyz".to_vec(),
|
||||
merkle_branches: vec![branch],
|
||||
version: 0x2000_0000,
|
||||
nbits: 0x1d00_ffff,
|
||||
ntime: 0x6655_4433,
|
||||
extranonce1: vec![1, 2, 3, 4],
|
||||
extranonce2_size: 4,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_cross_checked_header_and_asic_work() {
|
||||
let job = fixture();
|
||||
let header = job.nonce_zero_header(&[5, 6, 7, 8], job.ntime).unwrap();
|
||||
let expected = concat!(
|
||||
"0000002003020100070605040b0a09080f0e0d0c13121110171615141b1a19181f1e1d1c",
|
||||
"9b9bc91f078af83ff63d00c6da09b5bc4dd26e2d8c0df8ab16b933a259a05881",
|
||||
"33445566ffff001d00000000"
|
||||
);
|
||||
let actual: String = header.iter().map(|byte| format!("{byte:02x}")).collect();
|
||||
assert_eq!(actual, expected);
|
||||
let frame = encode_work(&header, 0x18).unwrap();
|
||||
assert_eq!(&frame[10..14], &header[72..76]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_pool_work_before_hashing() {
|
||||
let mut job = fixture();
|
||||
assert_eq!(
|
||||
job.nonce_zero_header(&[5, 6], job.ntime),
|
||||
Err(JobError::ExtranonceLength)
|
||||
);
|
||||
job.extranonce2_size = 33;
|
||||
assert_eq!(job.validate(), Err(JobError::InvalidExtranonceSize));
|
||||
job.extranonce2_size = 4;
|
||||
job.coinb1.resize(65_536, 0);
|
||||
assert_eq!(job.validate(), Err(JobError::CoinbaseTooLarge));
|
||||
job.coinb1 = b"abc".to_vec();
|
||||
job.merkle_branches.resize(65, [0; 32]);
|
||||
assert_eq!(job.validate(), Err(JobError::TooManyMerkleBranches));
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"e0165305f3b22a9ab4217a121f85379aa56c2675caa7afedc0a7468fc187600b",".github/FUNDING.yml":"b017158736b3c9751a2d21edfce7fe61c8954e2fced8da8dd3013c2f3e295bd9",".github/workflows/ci.yml":"89c4f60223daf03217ea7c1e8879135b11d2554bd1e57dd9eff5c02f1adc7909","Cargo.lock":"1cd64090b8e883963bb08d91e6968cc1e83ffdba4f4b2f47651ce9d351a8a947","Cargo.toml":"894c215e4e7e973b556b0e9627bf9b6a719a043fac9b5af9838660816392a09f","Cargo.toml.orig":"44ca1ae5d9b48743305b57f3367051289512037e7836b8b3693b68d48dec0483","LICENSE-APACHE":"62c7a1e35f56406896d7aa7ca52d0cc0d272ac022b5d2796e7d6905db8a3636a","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"1305bd79675db0194915d9bc91bd0b3cf9487bc1126e05bf46b48ab43edce44b","benches/bench.rs":"29a0bdb973848007d681e571c57487e21e1bfb7aed5980c2190dba354498b154","src/lib.rs":"e962338e1886873aa7c3399ec89aa5378ccbfed712b9690f043628079390a34f","src/u128_ext.rs":"498cc94f13805e551248fd82f47cc318f51d953a5002a9f8698858cd12fbae3c","tests/test.rs":"b22f8a8ed89edcbb4140b74f5edd7f2adf3cb0e363990dd6010cd6ebe0dc36b7"},"package":"8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "af77385d0daf4d0e949e81f2588be2e44f69f086"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
github: dtolnay
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
schedule: [cron: "40 1 * * *"]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
env:
|
||||
RUSTFLAGS: -Dwarnings
|
||||
|
||||
jobs:
|
||||
pre_ci:
|
||||
uses: dtolnay/.github/.github/workflows/pre_ci.yml@master
|
||||
|
||||
test:
|
||||
name: Rust ${{matrix.rust}}
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
rust: [nightly, beta, stable, 1.86.0]
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@master
|
||||
with:
|
||||
toolchain: ${{matrix.rust}}
|
||||
- name: Enable type layout randomization
|
||||
run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV
|
||||
if: matrix.rust == 'nightly'
|
||||
- run: cargo build
|
||||
- run: cargo test
|
||||
- run: cargo test --release
|
||||
- run: cargo build --no-default-features
|
||||
- run: cargo test --tests --no-default-features
|
||||
- run: cargo test --tests --no-default-features --release
|
||||
- run: cargo build --tests --features no-panic --release
|
||||
if: matrix.rust == 'nightly'
|
||||
- run: cargo bench --no-run
|
||||
if: matrix.rust == 'nightly'
|
||||
- uses: actions/upload-artifact@v6
|
||||
if: matrix.rust == 'nightly' && always()
|
||||
with:
|
||||
name: Cargo.lock
|
||||
path: Cargo.lock
|
||||
continue-on-error: true
|
||||
|
||||
msrv:
|
||||
name: Rust 1.68.0
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@1.68
|
||||
- run: cargo build
|
||||
- run: cargo build --no-default-features
|
||||
|
||||
doc:
|
||||
name: Documentation
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
env:
|
||||
RUSTDOCFLAGS: -Dwarnings
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
- uses: dtolnay/install@cargo-docs-rs
|
||||
- run: cargo docs-rs
|
||||
|
||||
miri:
|
||||
name: Miri
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@miri
|
||||
- run: cargo miri setup
|
||||
- run: cargo miri test
|
||||
env:
|
||||
MIRIFLAGS: -Zmiri-strict-provenance
|
||||
|
||||
clippy:
|
||||
name: Clippy
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request'
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@clippy
|
||||
- run: cargo clippy --tests --benches -- -Dclippy::all -Dclippy::pedantic
|
||||
|
||||
fuzz:
|
||||
name: Fuzz
|
||||
needs: pre_ci
|
||||
if: needs.pre_ci.outputs.continue
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@nightly
|
||||
- uses: dtolnay/install@cargo-fuzz
|
||||
- run: cargo fuzz check
|
||||
|
||||
outdated:
|
||||
name: Outdated
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name != 'pull_request'
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: dtolnay/install@cargo-outdated
|
||||
- run: cargo outdated --workspace --exit-code 1
|
||||
- run: cargo outdated --manifest-path fuzz/Cargo.toml --exit-code 1
|
||||
+460
@@ -0,0 +1,460 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "alloca"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5a7d05ea6aea7e9e64d25b9156ba2fee3fdd659e34e41063cd2fc7cd020d7f4"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anes"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a0dd1ca384932ff3641c8718a02769f1698e7563dc6974ffd03346116310423"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"ciborium-ll",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-io"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
|
||||
|
||||
[[package]]
|
||||
name = "ciborium-ll"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
|
||||
dependencies = [
|
||||
"ciborium-io",
|
||||
"half",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
||||
|
||||
[[package]]
|
||||
name = "criterion"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "950046b2aa2492f9a536f5f4f9a3de7b9e2476e575e05bd6c333371add4d98f3"
|
||||
dependencies = [
|
||||
"alloca",
|
||||
"anes",
|
||||
"cast",
|
||||
"ciborium",
|
||||
"clap",
|
||||
"criterion-plot",
|
||||
"itertools",
|
||||
"num-traits",
|
||||
"oorandom",
|
||||
"page_size",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tinytemplate",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "criterion-plot"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8d80a2f4f5b554395e47b5d8305bc3d27813bacb73493eb1001e8f76dae29ea"
|
||||
dependencies = [
|
||||
"cast",
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crunchy"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||
|
||||
[[package]]
|
||||
name = "half"
|
||||
version = "2.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"crunchy",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"no-panic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.183"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "no-panic"
|
||||
version = "0.1.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f967505aabc8af5752d098c34146544a43684817cdba8f9725b292530cabbf53"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "oorandom"
|
||||
version = "11.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
|
||||
|
||||
[[package]]
|
||||
name = "page_size"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.149"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
|
||||
dependencies = [
|
||||
"itoa 1.0.17",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinytemplate"
|
||||
version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "efbb2a062be311f2ba113ce66f697a4dc589f85e78a4aea276200804cea0ed87"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e8bc7269b54418e7aeeef514aa68f8690b8c0489a06b0136e5f57c4c5ccab89"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2021"
|
||||
rust-version = "1.68"
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
authors = ["David Tolnay <dtolnay@gmail.com>"]
|
||||
build = false
|
||||
exclude = ["*.png"]
|
||||
autolib = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
autobenches = false
|
||||
description = "Fast integer primitive to string conversion"
|
||||
documentation = "https://docs.rs/itoa"
|
||||
readme = "README.md"
|
||||
keywords = ["integer"]
|
||||
categories = [
|
||||
"value-formatting",
|
||||
"no-std",
|
||||
"no-std::no-alloc",
|
||||
]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/dtolnay/itoa"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
rustdoc-args = [
|
||||
"--generate-link-to-definition",
|
||||
"--generate-macro-expansion",
|
||||
"--extern-html-root-url=core=https://doc.rust-lang.org",
|
||||
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
|
||||
"--extern-html-root-url=std=https://doc.rust-lang.org",
|
||||
]
|
||||
|
||||
[lib]
|
||||
name = "itoa"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[test]]
|
||||
name = "test"
|
||||
path = "tests/test.rs"
|
||||
|
||||
[[bench]]
|
||||
name = "bench"
|
||||
path = "benches/bench.rs"
|
||||
harness = false
|
||||
|
||||
[dependencies.no-panic]
|
||||
version = "0.1"
|
||||
optional = true
|
||||
|
||||
[target."cfg(not(miri))".dev-dependencies.criterion]
|
||||
version = "0.8"
|
||||
default-features = false
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
[package]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
authors = ["David Tolnay <dtolnay@gmail.com>"]
|
||||
categories = ["value-formatting", "no-std", "no-std::no-alloc"]
|
||||
description = "Fast integer primitive to string conversion"
|
||||
documentation = "https://docs.rs/itoa"
|
||||
edition = "2021"
|
||||
exclude = ["*.png"]
|
||||
keywords = ["integer"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/dtolnay/itoa"
|
||||
rust-version = "1.68"
|
||||
|
||||
[dependencies]
|
||||
no-panic = { version = "0.1", optional = true }
|
||||
|
||||
[target.'cfg(not(miri))'.dev-dependencies]
|
||||
criterion = { version = "0.8", default-features = false }
|
||||
|
||||
[[bench]]
|
||||
name = "bench"
|
||||
harness = false
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
rustdoc-args = [
|
||||
"--generate-link-to-definition",
|
||||
"--generate-macro-expansion",
|
||||
"--extern-html-root-url=core=https://doc.rust-lang.org",
|
||||
"--extern-html-root-url=alloc=https://doc.rust-lang.org",
|
||||
"--extern-html-root-url=std=https://doc.rust-lang.org",
|
||||
]
|
||||
Vendored
+176
@@ -0,0 +1,176 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
Vendored
+65
@@ -0,0 +1,65 @@
|
||||
itoa
|
||||
====
|
||||
|
||||
[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/itoa-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/itoa)
|
||||
[<img alt="crates.io" src="https://img.shields.io/crates/v/itoa.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/itoa)
|
||||
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-itoa-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/itoa)
|
||||
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/itoa/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/itoa/actions?query=branch%3Amaster)
|
||||
|
||||
This crate provides a fast conversion of integer primitives to decimal strings.
|
||||
The implementation comes straight from [libcore] but avoids the performance
|
||||
penalty of going through [`core::fmt::Formatter`].
|
||||
|
||||
See also [`zmij`] for printing floating point primitives.
|
||||
|
||||
[libcore]: https://github.com/rust-lang/rust/blob/1.92.0/library/core/src/fmt/num.rs#L190-L253
|
||||
[`core::fmt::Formatter`]: https://doc.rust-lang.org/std/fmt/struct.Formatter.html
|
||||
[`zmij`]: https://github.com/dtolnay/zmij
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
itoa = "1.0"
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Example
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut buffer = itoa::Buffer::new();
|
||||
let printed = buffer.format(128u64);
|
||||
assert_eq!(printed, "128");
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Performance
|
||||
|
||||
The [itoa-benchmark] compares this library and other Rust integer formatting
|
||||
implementations across a range of integer sizes. The vertical axis in this chart
|
||||
shows nanoseconds taken by a single execution of
|
||||
`itoa::Buffer::new().format(value)` so a lower result indicates a faster
|
||||
library.
|
||||
|
||||
[itoa-benchmark]: https://github.com/dtolnay/itoa-benchmark
|
||||
|
||||

|
||||
|
||||
<br>
|
||||
|
||||
#### License
|
||||
|
||||
<sup>
|
||||
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
|
||||
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
|
||||
</sup>
|
||||
|
||||
<br>
|
||||
|
||||
<sub>
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
|
||||
be dual licensed as above, without any additional terms or conditions.
|
||||
</sub>
|
||||
Vendored
+41
@@ -0,0 +1,41 @@
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use std::fmt::Display;
|
||||
use std::hint;
|
||||
use std::io::Write;
|
||||
|
||||
fn do_bench(c: &mut Criterion, group_name: &str, int: impl itoa::Integer + Display) {
|
||||
let mut group = c.benchmark_group(group_name);
|
||||
group.bench_function("itoa", |b| {
|
||||
let mut buf = itoa::Buffer::new();
|
||||
b.iter(move || {
|
||||
let int = hint::black_box(int);
|
||||
let formatted = buf.format(int);
|
||||
hint::black_box(formatted);
|
||||
});
|
||||
});
|
||||
group.bench_function("std::fmt", |b| {
|
||||
let mut buf = Vec::with_capacity(20);
|
||||
b.iter(|| {
|
||||
buf.clear();
|
||||
let int = hint::black_box(int);
|
||||
write!(&mut buf, "{int}").unwrap();
|
||||
hint::black_box(buf.as_slice());
|
||||
});
|
||||
});
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench(c: &mut Criterion) {
|
||||
do_bench(c, "u64[0]", 0u64);
|
||||
do_bench(c, "u64[half]", u64::from(u32::MAX));
|
||||
do_bench(c, "u64[max]", u64::MAX);
|
||||
|
||||
do_bench(c, "i16[0]", 0i16);
|
||||
do_bench(c, "i16[min]", i16::MIN);
|
||||
|
||||
do_bench(c, "u128[0]", 0u128);
|
||||
do_bench(c, "u128[max]", u128::MAX);
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench);
|
||||
criterion_main!(benches);
|
||||
Vendored
+466
@@ -0,0 +1,466 @@
|
||||
//! [![github]](https://github.com/dtolnay/itoa) [![crates-io]](https://crates.io/crates/itoa) [![docs-rs]](https://docs.rs/itoa)
|
||||
//!
|
||||
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
|
||||
//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
|
||||
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
|
||||
//!
|
||||
//! <br>
|
||||
//!
|
||||
//! This crate provides a fast conversion of integer primitives to decimal
|
||||
//! strings. The implementation comes straight from [libcore] but avoids the
|
||||
//! performance penalty of going through [`core::fmt::Formatter`].
|
||||
//!
|
||||
//! See also [`zmij`] for printing floating point primitives.
|
||||
//!
|
||||
//! [libcore]: https://github.com/rust-lang/rust/blob/1.92.0/library/core/src/fmt/num.rs#L190-L253
|
||||
//! [`zmij`]: https://github.com/dtolnay/zmij
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```
|
||||
//! fn main() {
|
||||
//! let mut buffer = itoa::Buffer::new();
|
||||
//! let printed = buffer.format(128u64);
|
||||
//! assert_eq!(printed, "128");
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! # Performance
|
||||
//!
|
||||
//! The [itoa-benchmark] compares this library and other Rust integer formatting
|
||||
//! implementations across a range of integer sizes. The vertical axis in this
|
||||
//! chart shows nanoseconds taken by a single execution of
|
||||
//! `itoa::Buffer::new().format(value)` so a lower result indicates a faster
|
||||
//! library.
|
||||
//!
|
||||
//! [itoa-benchmark]: https://github.com/dtolnay/itoa-benchmark
|
||||
//!
|
||||
//! 
|
||||
|
||||
#![doc(html_root_url = "https://docs.rs/itoa/1.0.18")]
|
||||
#![no_std]
|
||||
#![allow(
|
||||
clippy::cast_lossless,
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::expl_impl_clone_on_copy,
|
||||
clippy::identity_op,
|
||||
clippy::items_after_statements,
|
||||
clippy::must_use_candidate,
|
||||
clippy::needless_doctest_main,
|
||||
clippy::unreadable_literal
|
||||
)]
|
||||
|
||||
mod u128_ext;
|
||||
|
||||
use core::hint;
|
||||
use core::mem::{self, MaybeUninit};
|
||||
use core::str;
|
||||
#[cfg(feature = "no-panic")]
|
||||
use no_panic::no_panic;
|
||||
|
||||
/// A correctly sized stack allocation for the formatted integer to be written
|
||||
/// into.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut buffer = itoa::Buffer::new();
|
||||
/// let printed = buffer.format(1234);
|
||||
/// assert_eq!(printed, "1234");
|
||||
/// ```
|
||||
pub struct Buffer {
|
||||
bytes: [MaybeUninit<u8>; i128::MAX_STR_LEN],
|
||||
}
|
||||
|
||||
impl Default for Buffer {
|
||||
#[inline]
|
||||
fn default() -> Buffer {
|
||||
Buffer::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Copy for Buffer {}
|
||||
|
||||
#[allow(clippy::non_canonical_clone_impl)]
|
||||
impl Clone for Buffer {
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
Buffer::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
/// This is a cheap operation; you don't need to worry about reusing buffers
|
||||
/// for efficiency.
|
||||
#[inline]
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
pub fn new() -> Buffer {
|
||||
let bytes = [MaybeUninit::<u8>::uninit(); i128::MAX_STR_LEN];
|
||||
Buffer { bytes }
|
||||
}
|
||||
|
||||
/// Print an integer into this buffer and return a reference to its string
|
||||
/// representation within the buffer.
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
pub fn format<I: Integer>(&mut self, i: I) -> &str {
|
||||
let buf_ptr = self.bytes.as_mut_ptr().cast::<I::Buffer>();
|
||||
let string = i.write(unsafe { &mut *buf_ptr });
|
||||
if string.len() > I::MAX_STR_LEN {
|
||||
unsafe { hint::unreachable_unchecked() };
|
||||
}
|
||||
string
|
||||
}
|
||||
}
|
||||
|
||||
/// An integer that can be written into an [`itoa::Buffer`][Buffer].
|
||||
///
|
||||
/// This trait is sealed and cannot be implemented for types outside of itoa.
|
||||
pub trait Integer: private::Sealed {
|
||||
/// The maximum length of string that formatting an integer of this type can
|
||||
/// produce on the current target platform.
|
||||
const MAX_STR_LEN: usize;
|
||||
}
|
||||
|
||||
// Seal to prevent downstream implementations of the Integer trait.
|
||||
mod private {
|
||||
#[doc(hidden)]
|
||||
pub trait Sealed: Copy {
|
||||
#[doc(hidden)]
|
||||
type Buffer: 'static;
|
||||
fn write(self, buf: &mut Self::Buffer) -> &str;
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_Integer {
|
||||
($Signed:ident, $Unsigned:ident) => {
|
||||
const _: () = {
|
||||
assert!($Signed::MIN < 0, "need signed");
|
||||
assert!($Unsigned::MIN == 0, "need unsigned");
|
||||
assert!($Signed::BITS == $Unsigned::BITS, "need counterparts");
|
||||
};
|
||||
|
||||
impl Integer for $Unsigned {
|
||||
const MAX_STR_LEN: usize = $Unsigned::MAX.ilog10() as usize + 1;
|
||||
}
|
||||
|
||||
impl private::Sealed for $Unsigned {
|
||||
type Buffer = [MaybeUninit<u8>; Self::MAX_STR_LEN];
|
||||
|
||||
#[inline]
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
fn write(self, buf: &mut Self::Buffer) -> &str {
|
||||
let offset = Unsigned::fmt(self, buf);
|
||||
// SAFETY: Starting from `offset`, all elements of the slice have been set.
|
||||
unsafe { slice_buffer_to_str(buf, offset) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Integer for $Signed {
|
||||
const MAX_STR_LEN: usize = $Signed::MAX.ilog10() as usize + 2;
|
||||
}
|
||||
|
||||
impl private::Sealed for $Signed {
|
||||
type Buffer = [MaybeUninit<u8>; Self::MAX_STR_LEN];
|
||||
|
||||
#[inline]
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
fn write(self, buf: &mut Self::Buffer) -> &str {
|
||||
let mut offset = Self::MAX_STR_LEN - $Unsigned::MAX_STR_LEN;
|
||||
offset += Unsigned::fmt(
|
||||
self.unsigned_abs(),
|
||||
(&mut buf[offset..]).try_into().unwrap(),
|
||||
);
|
||||
if self < 0 {
|
||||
offset -= 1;
|
||||
buf[offset].write(b'-');
|
||||
}
|
||||
// SAFETY: Starting from `offset`, all elements of the slice have been set.
|
||||
unsafe { slice_buffer_to_str(buf, offset) }
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_Integer!(i8, u8);
|
||||
impl_Integer!(i16, u16);
|
||||
impl_Integer!(i32, u32);
|
||||
impl_Integer!(i64, u64);
|
||||
impl_Integer!(i128, u128);
|
||||
|
||||
macro_rules! impl_Integer_size {
|
||||
($t:ty as $primitive:ident #[cfg(target_pointer_width = $width:literal)]) => {
|
||||
#[cfg(target_pointer_width = $width)]
|
||||
impl Integer for $t {
|
||||
const MAX_STR_LEN: usize = <$primitive as Integer>::MAX_STR_LEN;
|
||||
}
|
||||
|
||||
#[cfg(target_pointer_width = $width)]
|
||||
impl private::Sealed for $t {
|
||||
type Buffer = <$primitive as private::Sealed>::Buffer;
|
||||
|
||||
#[inline]
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
fn write(self, buf: &mut Self::Buffer) -> &str {
|
||||
(self as $primitive).write(buf)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_Integer_size!(isize as i16 #[cfg(target_pointer_width = "16")]);
|
||||
impl_Integer_size!(usize as u16 #[cfg(target_pointer_width = "16")]);
|
||||
impl_Integer_size!(isize as i32 #[cfg(target_pointer_width = "32")]);
|
||||
impl_Integer_size!(usize as u32 #[cfg(target_pointer_width = "32")]);
|
||||
impl_Integer_size!(isize as i64 #[cfg(target_pointer_width = "64")]);
|
||||
impl_Integer_size!(usize as u64 #[cfg(target_pointer_width = "64")]);
|
||||
|
||||
#[repr(C, align(2))]
|
||||
struct DecimalPairs([u8; 200]);
|
||||
|
||||
// The string of all two-digit numbers in range 00..99 is used as a lookup table.
|
||||
static DECIMAL_PAIRS: DecimalPairs = DecimalPairs(
|
||||
*b"0001020304050607080910111213141516171819\
|
||||
2021222324252627282930313233343536373839\
|
||||
4041424344454647484950515253545556575859\
|
||||
6061626364656667686970717273747576777879\
|
||||
8081828384858687888990919293949596979899",
|
||||
);
|
||||
|
||||
// Returns {value / 100, value % 100} correct for values of up to 4 digits.
|
||||
fn divmod100(value: u32) -> (u32, u32) {
|
||||
debug_assert!(value < 10_000);
|
||||
const EXP: u32 = 19; // 19 is faster or equal to 12 even for 3 digits.
|
||||
const SIG: u32 = (1 << EXP) / 100 + 1;
|
||||
let div = (value * SIG) >> EXP; // value / 100
|
||||
(div, value - div * 100)
|
||||
}
|
||||
|
||||
/// This function converts a slice of ascii characters into a `&str` starting
|
||||
/// from `offset`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// `buf` content starting from `offset` index MUST BE initialized and MUST BE
|
||||
/// ascii characters.
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
unsafe fn slice_buffer_to_str(buf: &[MaybeUninit<u8>], offset: usize) -> &str {
|
||||
// SAFETY: `offset` is always included between 0 and `buf`'s length.
|
||||
let written = unsafe { buf.get_unchecked(offset..) };
|
||||
// SAFETY: (`assume_init_ref`) All buf content since offset is set.
|
||||
// SAFETY: (`from_utf8_unchecked`) Writes use ASCII from the lookup table exclusively.
|
||||
unsafe { str::from_utf8_unchecked(&*(written as *const [MaybeUninit<u8>] as *const [u8])) }
|
||||
}
|
||||
|
||||
trait Unsigned: Integer {
|
||||
fn fmt(self, buf: &mut Self::Buffer) -> usize;
|
||||
}
|
||||
|
||||
macro_rules! impl_Unsigned {
|
||||
($Unsigned:ident) => {
|
||||
impl Unsigned for $Unsigned {
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
fn fmt(self, buf: &mut Self::Buffer) -> usize {
|
||||
// Count the number of bytes in buf that are not initialized.
|
||||
let mut offset = buf.len();
|
||||
// Consume the least-significant decimals from a working copy.
|
||||
let mut remain = self;
|
||||
|
||||
// Format per four digits from the lookup table.
|
||||
// Four digits need a 16-bit $Unsigned or wider.
|
||||
while mem::size_of::<Self>() > 1
|
||||
&& remain
|
||||
> 999
|
||||
.try_into()
|
||||
.expect("branch is not hit for types that cannot fit 999 (u8)")
|
||||
{
|
||||
offset -= 4;
|
||||
|
||||
// pull two pairs
|
||||
let scale: Self = 1_00_00
|
||||
.try_into()
|
||||
.expect("branch is not hit for types that cannot fit 1E4 (u8)");
|
||||
let quad = remain % scale;
|
||||
remain /= scale;
|
||||
let (pair1, pair2) = divmod100(quad as u32);
|
||||
unsafe {
|
||||
buf[offset + 0]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 0));
|
||||
buf[offset + 1]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 1));
|
||||
buf[offset + 2]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 0));
|
||||
buf[offset + 3]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Format per two digits from the lookup table.
|
||||
if remain > 9 {
|
||||
offset -= 2;
|
||||
|
||||
let (last, pair) = divmod100(remain as u32);
|
||||
remain = last as Self;
|
||||
unsafe {
|
||||
buf[offset + 0]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair as usize * 2 + 0));
|
||||
buf[offset + 1]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair as usize * 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Format the last remaining digit, if any.
|
||||
if remain != 0 || self == 0 {
|
||||
offset -= 1;
|
||||
|
||||
// Either the compiler sees that remain < 10, or it prevents
|
||||
// a boundary check up next.
|
||||
let last = remain as u8 & 15;
|
||||
buf[offset].write(b'0' + last);
|
||||
// not used: remain = 0;
|
||||
}
|
||||
|
||||
offset
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_Unsigned!(u8);
|
||||
impl_Unsigned!(u16);
|
||||
impl_Unsigned!(u32);
|
||||
impl_Unsigned!(u64);
|
||||
|
||||
impl Unsigned for u128 {
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
fn fmt(self, buf: &mut Self::Buffer) -> usize {
|
||||
// Optimize common-case zero, which would also need special treatment due to
|
||||
// its "leading" zero.
|
||||
if self == 0 {
|
||||
let offset = buf.len() - 1;
|
||||
buf[offset].write(b'0');
|
||||
return offset;
|
||||
}
|
||||
// Take the 16 least-significant decimals.
|
||||
let (quot_1e16, mod_1e16) = div_rem_1e16(self);
|
||||
let (mut remain, mut offset) = if quot_1e16 == 0 {
|
||||
(mod_1e16, u128::MAX_STR_LEN)
|
||||
} else {
|
||||
// Write digits at buf[23..39].
|
||||
enc_16lsd::<{ u128::MAX_STR_LEN - 16 }>(buf, mod_1e16);
|
||||
|
||||
// Take another 16 decimals.
|
||||
let (quot2, mod2) = div_rem_1e16(quot_1e16);
|
||||
if quot2 == 0 {
|
||||
(mod2, u128::MAX_STR_LEN - 16)
|
||||
} else {
|
||||
// Write digits at buf[7..23].
|
||||
enc_16lsd::<{ u128::MAX_STR_LEN - 32 }>(buf, mod2);
|
||||
// Quot2 has at most 7 decimals remaining after two 1e16 divisions.
|
||||
(quot2 as u64, u128::MAX_STR_LEN - 32)
|
||||
}
|
||||
};
|
||||
|
||||
// Format per four digits from the lookup table.
|
||||
while remain > 999 {
|
||||
offset -= 4;
|
||||
|
||||
// pull two pairs
|
||||
let quad = remain % 1_00_00;
|
||||
remain /= 1_00_00;
|
||||
let (pair1, pair2) = divmod100(quad as u32);
|
||||
unsafe {
|
||||
buf[offset + 0].write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 0));
|
||||
buf[offset + 1].write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 1));
|
||||
buf[offset + 2].write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 0));
|
||||
buf[offset + 3].write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Format per two digits from the lookup table.
|
||||
if remain > 9 {
|
||||
offset -= 2;
|
||||
|
||||
let (last, pair) = divmod100(remain as u32);
|
||||
remain = last as u64;
|
||||
unsafe {
|
||||
buf[offset + 0].write(*DECIMAL_PAIRS.0.get_unchecked(pair as usize * 2 + 0));
|
||||
buf[offset + 1].write(*DECIMAL_PAIRS.0.get_unchecked(pair as usize * 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Format the last remaining digit, if any.
|
||||
if remain != 0 {
|
||||
offset -= 1;
|
||||
|
||||
// Either the compiler sees that remain < 10, or it prevents
|
||||
// a boundary check up next.
|
||||
let last = remain as u8 & 15;
|
||||
buf[offset].write(b'0' + last);
|
||||
// not used: remain = 0;
|
||||
}
|
||||
offset
|
||||
}
|
||||
}
|
||||
|
||||
// Encodes the 16 least-significant decimals of n into `buf[OFFSET..OFFSET + 16]`.
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
fn enc_16lsd<const OFFSET: usize>(buf: &mut [MaybeUninit<u8>], n: u64) {
|
||||
// Consume the least-significant decimals from a working copy.
|
||||
let mut remain = n;
|
||||
|
||||
// Format per four digits from the lookup table.
|
||||
for quad_index in (1..4).rev() {
|
||||
// pull two pairs
|
||||
let quad = remain % 1_00_00;
|
||||
remain /= 1_00_00;
|
||||
let (pair1, pair2) = divmod100(quad as u32);
|
||||
unsafe {
|
||||
buf[quad_index * 4 + OFFSET + 0]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 0));
|
||||
buf[quad_index * 4 + OFFSET + 1]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 1));
|
||||
buf[quad_index * 4 + OFFSET + 2]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 0));
|
||||
buf[quad_index * 4 + OFFSET + 3]
|
||||
.write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// final two pairs
|
||||
let (pair1, pair2) = divmod100(remain as u32);
|
||||
unsafe {
|
||||
buf[OFFSET + 0].write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 0));
|
||||
buf[OFFSET + 1].write(*DECIMAL_PAIRS.0.get_unchecked(pair1 as usize * 2 + 1));
|
||||
buf[OFFSET + 2].write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 0));
|
||||
buf[OFFSET + 3].write(*DECIMAL_PAIRS.0.get_unchecked(pair2 as usize * 2 + 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Euclidean division plus remainder with constant 1E16 basically consumes 16
|
||||
// decimals from n.
|
||||
//
|
||||
// The integer division algorithm is based on the following paper:
|
||||
//
|
||||
// T. Granlund and P. Montgomery, “Division by Invariant Integers Using Multiplication”
|
||||
// in Proc. of the SIGPLAN94 Conference on Programming Language Design and
|
||||
// Implementation, 1994, pp. 61–72
|
||||
//
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
fn div_rem_1e16(n: u128) -> (u128, u64) {
|
||||
const D: u128 = 1_0000_0000_0000_0000;
|
||||
// The check inlines well with the caller flow.
|
||||
if n < D {
|
||||
return (0, n as u64);
|
||||
}
|
||||
|
||||
// These constant values are computed with the CHOOSE_MULTIPLIER procedure
|
||||
// from the Granlund & Montgomery paper, using N=128, prec=128 and d=1E16.
|
||||
const M_HIGH: u128 = 76624777043294442917917351357515459181;
|
||||
const SH_POST: u8 = 51;
|
||||
|
||||
// n.widening_mul(M_HIGH).1 >> SH_POST
|
||||
let quot = u128_ext::mulhi(n, M_HIGH) >> SH_POST;
|
||||
let rem = n - quot * D;
|
||||
(quot, rem as u64)
|
||||
}
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
#[cfg(feature = "no-panic")]
|
||||
use no_panic::no_panic;
|
||||
|
||||
/// Multiply unsigned 128 bit integers, return upper 128 bits of the result
|
||||
#[inline]
|
||||
#[cfg_attr(feature = "no-panic", no_panic)]
|
||||
pub(crate) fn mulhi(x: u128, y: u128) -> u128 {
|
||||
let x_lo = x as u64;
|
||||
let x_hi = (x >> 64) as u64;
|
||||
let y_lo = y as u64;
|
||||
let y_hi = (y >> 64) as u64;
|
||||
|
||||
// handle possibility of overflow
|
||||
let carry = (u128::from(x_lo) * u128::from(y_lo)) >> 64;
|
||||
let m = u128::from(x_lo) * u128::from(y_hi) + carry;
|
||||
let high1 = m >> 64;
|
||||
|
||||
let m_lo = m as u64;
|
||||
let high2 = (u128::from(x_hi) * u128::from(y_lo) + u128::from(m_lo)) >> 64;
|
||||
|
||||
u128::from(x_hi) * u128::from(y_hi) + high1 + high2
|
||||
}
|
||||
Vendored
+45
@@ -0,0 +1,45 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
macro_rules! test {
|
||||
($($name:ident($value:expr, $expected:expr))*) => {
|
||||
$(
|
||||
#[test]
|
||||
fn $name() {
|
||||
let mut buffer = itoa::Buffer::new();
|
||||
let s = buffer.format($value);
|
||||
assert_eq!(s, $expected);
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
test! {
|
||||
test_u64_0(0u64, "0")
|
||||
test_u64_half(u64::from(u32::MAX), "4294967295")
|
||||
test_u64_max(u64::MAX, "18446744073709551615")
|
||||
test_i64_min(i64::MIN, "-9223372036854775808")
|
||||
|
||||
test_i16_0(0i16, "0")
|
||||
test_i16_min(i16::MIN, "-32768")
|
||||
|
||||
test_u128_0(0u128, "0")
|
||||
test_u128_max(u128::MAX, "340282366920938463463374607431768211455")
|
||||
test_i128_min(i128::MIN, "-170141183460469231731687303715884105728")
|
||||
test_i128_max(i128::MAX, "170141183460469231731687303715884105727")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_max_str_len() {
|
||||
use itoa::Integer as _;
|
||||
|
||||
assert_eq!(i8::MAX_STR_LEN, 4);
|
||||
assert_eq!(u8::MAX_STR_LEN, 3);
|
||||
assert_eq!(i16::MAX_STR_LEN, 6);
|
||||
assert_eq!(u16::MAX_STR_LEN, 5);
|
||||
assert_eq!(i32::MAX_STR_LEN, 11);
|
||||
assert_eq!(u32::MAX_STR_LEN, 10);
|
||||
assert_eq!(i64::MAX_STR_LEN, 20);
|
||||
assert_eq!(u64::MAX_STR_LEN, 20);
|
||||
assert_eq!(i128::MAX_STR_LEN, 40);
|
||||
assert_eq!(u128::MAX_STR_LEN, 39);
|
||||
}
|
||||
+1
File diff suppressed because one or more lines are too long
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "5fdb40c054e1fff359a2f7bdf7f87a13b34b465d"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
!.github
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"rust-analyzer.cargo.allFeatures": false,
|
||||
"rust-analyzer.linkedProjects": [
|
||||
"benchmarks/engines/libc/Cargo.toml",
|
||||
"benchmarks/engines/rust-bytecount/Cargo.toml",
|
||||
"benchmarks/engines/rust-jetscii/Cargo.toml",
|
||||
"benchmarks/engines/rust-memchr/Cargo.toml",
|
||||
"benchmarks/engines/rust-memchrold/Cargo.toml",
|
||||
"benchmarks/engines/rust-sliceslice/Cargo.toml",
|
||||
"benchmarks/engines/rust-std/Cargo.toml",
|
||||
"benchmarks/engines/stringzilla/Cargo.toml",
|
||||
"benchmarks/shared/Cargo.toml",
|
||||
"fuzz/Cargo.toml",
|
||||
"Cargo.toml"
|
||||
]
|
||||
}
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
# AI Policy
|
||||
|
||||
Using AI (i.e., LLMs) as tools for coding is welcome. A high bar is held for
|
||||
all contributions to this project. Moreover, the project maintainers remain
|
||||
responsible for any code that is published as part of a release. Contributors
|
||||
are expected to be responsible for any code they publish.
|
||||
|
||||
**AI should not be used to generate comments when communicating with
|
||||
maintainers**. Comments are expected to be written by humans. Comments that are
|
||||
believed to be written by AI may be hidden without notice.
|
||||
|
||||
If you are opening an issue, you should be able to describe the problem in your
|
||||
own words.
|
||||
|
||||
If you are opening a pull request, you are expected to be able to explain the
|
||||
proposed changes in your own words. This includes the pull request body and
|
||||
responses to questions. **Do not copy responses from the AI when replying to
|
||||
questions from maintainers.**
|
||||
|
||||
This project requires a human in the loop who understands the work produced
|
||||
by AI. **Autonomous agents are not allowed to be used for contributing to our
|
||||
projects**. Pull requests that appear in violation of this will be closed,
|
||||
perhaps without notice.
|
||||
|
||||
If you wish to include context from an interaction with AI in your comments, it
|
||||
must be in a quote block (e.g., using `>`) and disclosed as such. It must be
|
||||
accompanied by human commentary explaining the relevance and implications of
|
||||
the context. Do not share long snippets.
|
||||
|
||||
AI is useful when communicating as a non-native English speaker. If you are
|
||||
using AI to edit your comments for this purpose, please take the time to ensure
|
||||
it reflects your own voice and ideas. If using AI for translation, we recommend
|
||||
writing in your native language and including the AI translation in a quote
|
||||
block.
|
||||
|
||||
This policy was adapted from [uv's AI policy].
|
||||
|
||||
[uv's AI policy]: https://github.com/astral-sh/.github/blob/c5187e200db51bfe11d56e13053d29bd3793fdd8/AI_POLICY.md
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
# Contributing
|
||||
|
||||
## Use of AI
|
||||
|
||||
All use of AI in contributions must follow the
|
||||
[AI Policy](https://github.com/BurntSushi/jiff/blob/master/AI_POLICY.md).
|
||||
|
||||
Contributions not following the AI Policy will be closed.
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
This project is dual-licensed under the Unlicense and MIT licenses.
|
||||
|
||||
You may use this code under the terms of either license.
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.176"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "58f929b4d672ea937a23a1ab494143d968337a5f47e56d0815df1e0890ddf174"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
dependencies = [
|
||||
"log",
|
||||
"quickcheck",
|
||||
"rustc-std-workspace-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quickcheck"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6"
|
||||
dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-std-workspace-core"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa9c45b374136f52f2d6311062c7146bff20fec063c3f5d46a410bd937746955"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||
Vendored
+89
@@ -0,0 +1,89 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
authors = [
|
||||
"Andrew Gallant <jamslam@gmail.com>",
|
||||
"bluss",
|
||||
]
|
||||
build = false
|
||||
exclude = [
|
||||
"/.github",
|
||||
"/benchmarks",
|
||||
"/fuzz",
|
||||
"/scripts",
|
||||
"/tmp",
|
||||
]
|
||||
autolib = false
|
||||
autobins = false
|
||||
autoexamples = false
|
||||
autotests = false
|
||||
autobenches = false
|
||||
description = """
|
||||
Provides extremely fast (uses SIMD on x86_64, aarch64 and wasm32) routines for
|
||||
1, 2 or 3 byte search and single substring search.
|
||||
"""
|
||||
homepage = "https://github.com/BurntSushi/memchr"
|
||||
documentation = "https://docs.rs/memchr/"
|
||||
readme = "README.md"
|
||||
keywords = [
|
||||
"memchr",
|
||||
"memmem",
|
||||
"substring",
|
||||
"find",
|
||||
"search",
|
||||
]
|
||||
license = "Unlicense OR MIT"
|
||||
repository = "https://github.com/BurntSushi/memchr"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustdoc-args = ["--generate-link-to-definition"]
|
||||
|
||||
[features]
|
||||
alloc = []
|
||||
default = ["std"]
|
||||
libc = []
|
||||
logging = ["dep:log"]
|
||||
rustc-dep-of-std = ["core"]
|
||||
std = ["alloc"]
|
||||
use_std = ["std"]
|
||||
|
||||
[lib]
|
||||
name = "memchr"
|
||||
path = "src/lib.rs"
|
||||
bench = false
|
||||
|
||||
[dependencies.core]
|
||||
version = "1.0.0"
|
||||
optional = true
|
||||
package = "rustc-std-workspace-core"
|
||||
|
||||
[dependencies.log]
|
||||
version = "0.4.20"
|
||||
optional = true
|
||||
|
||||
[dev-dependencies.quickcheck]
|
||||
version = "1.0.3"
|
||||
default-features = false
|
||||
|
||||
[profile.bench]
|
||||
debug = 2
|
||||
|
||||
[profile.release]
|
||||
debug = 2
|
||||
|
||||
[profile.test]
|
||||
opt-level = 3
|
||||
debug = 2
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
[package]
|
||||
name = "memchr"
|
||||
version = "2.8.3" #:version
|
||||
authors = ["Andrew Gallant <jamslam@gmail.com>", "bluss"]
|
||||
description = """
|
||||
Provides extremely fast (uses SIMD on x86_64, aarch64 and wasm32) routines for
|
||||
1, 2 or 3 byte search and single substring search.
|
||||
"""
|
||||
documentation = "https://docs.rs/memchr/"
|
||||
homepage = "https://github.com/BurntSushi/memchr"
|
||||
repository = "https://github.com/BurntSushi/memchr"
|
||||
readme = "README.md"
|
||||
keywords = ["memchr", "memmem", "substring", "find", "search"]
|
||||
license = "Unlicense OR MIT"
|
||||
exclude = ["/.github", "/benchmarks", "/fuzz", "/scripts", "/tmp"]
|
||||
edition = "2021"
|
||||
rust-version = "1.61"
|
||||
|
||||
[lib]
|
||||
name = "memchr"
|
||||
bench = false
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
|
||||
# The 'std' feature permits the memchr crate to use the standard library. This
|
||||
# permits this crate to use runtime CPU feature detection to automatically
|
||||
# accelerate searching via vector instructions. Without the standard library,
|
||||
# this automatic detection is not possible.
|
||||
std = ["alloc"]
|
||||
|
||||
# The 'alloc' feature enables some APIs that require allocation, such as
|
||||
# 'Finder::into_owned'. Note that this feature does not enable runtime CPU
|
||||
# feature detection. That still requires 'std'.
|
||||
alloc = []
|
||||
|
||||
# When enabled (it's disabled by default), the `log` crate will be used to
|
||||
# emit a spattering of log messages. For the most part, the log messages are
|
||||
# meant to indicate what strategies are being employed. For example, whether
|
||||
# a vector or a scalar algorithm is used for substring search. This can be
|
||||
# useful when debugging performance problems.
|
||||
#
|
||||
# This is disabled by default.
|
||||
logging = ["dep:log"]
|
||||
|
||||
# The 'use_std' feature is DEPRECATED. It will be removed in memchr 3. Until
|
||||
# then, it is alias for the 'std' feature.
|
||||
use_std = ["std"]
|
||||
|
||||
# The 'libc' feature has been DEPRECATED and no longer has any effect.
|
||||
libc = []
|
||||
|
||||
# Internal feature, only used when building as part of libstd, not part of the
|
||||
# stable interface of this crate.
|
||||
rustc-dep-of-std = ['core']
|
||||
|
||||
[dependencies]
|
||||
# Only used when the `logging` feature is enabled (disabled by default).
|
||||
log = { version = "0.4.20", optional = true }
|
||||
# Internal feature, only used when building as part of libstd, not part of the
|
||||
# stable interface of this crate.
|
||||
core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' }
|
||||
|
||||
[dev-dependencies]
|
||||
quickcheck = { version = "1.0.3", default-features = false }
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
|
||||
[profile.bench]
|
||||
debug = true
|
||||
|
||||
[profile.test]
|
||||
opt-level = 3
|
||||
debug = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
rustdoc-args = ["--generate-link-to-definition"]
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Andrew Gallant
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
Vendored
+196
@@ -0,0 +1,196 @@
|
||||
memchr
|
||||
======
|
||||
This library provides heavily optimized routines for string search primitives.
|
||||
|
||||
[](https://github.com/BurntSushi/memchr/actions)
|
||||
[](https://crates.io/crates/memchr)
|
||||
|
||||
Dual-licensed under MIT or the [UNLICENSE](https://unlicense.org/).
|
||||
|
||||
|
||||
### Documentation
|
||||
|
||||
[https://docs.rs/memchr](https://docs.rs/memchr)
|
||||
|
||||
|
||||
### Overview
|
||||
|
||||
* The top-level module provides routines for searching for 1, 2 or 3 bytes
|
||||
in the forward or reverse direction. When searching for more than one byte,
|
||||
positions are considered a match if the byte at that position matches any
|
||||
of the bytes.
|
||||
* The `memmem` sub-module provides forward and reverse substring search
|
||||
routines.
|
||||
|
||||
In all such cases, routines operate on `&[u8]` without regard to encoding. This
|
||||
is exactly what you want when searching either UTF-8 or arbitrary bytes.
|
||||
|
||||
### Compiling without the standard library
|
||||
|
||||
memchr links to the standard library by default, but you can disable the
|
||||
`std` feature if you want to use it in a `#![no_std]` crate:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
memchr = { version = "2", default-features = false }
|
||||
```
|
||||
|
||||
On `x86_64` platforms, when the `std` feature is disabled, the SSE2 accelerated
|
||||
implementations will be used. When `std` is enabled, AVX2 accelerated
|
||||
implementations will be used if the CPU is determined to support it at runtime.
|
||||
|
||||
SIMD accelerated routines are also available on the `wasm32` and `aarch64`
|
||||
targets. The `std` feature is not required to use them.
|
||||
|
||||
When a SIMD version is not available, then this crate falls back to
|
||||
[SWAR](https://en.wikipedia.org/wiki/SWAR) techniques.
|
||||
|
||||
### Minimum Rust version policy
|
||||
|
||||
This crate's minimum supported `rustc` version is `1.61.0`.
|
||||
|
||||
The current policy is that the minimum Rust version required to use this crate
|
||||
can be increased in minor version updates. For example, if `crate 1.0` requires
|
||||
Rust 1.20.0, then `crate 1.0.z` for all values of `z` will also require Rust
|
||||
1.20.0 or newer. However, `crate 1.y` for `y > 0` may require a newer minimum
|
||||
version of Rust.
|
||||
|
||||
In general, this crate will be conservative with respect to the minimum
|
||||
supported version of Rust.
|
||||
|
||||
|
||||
### Testing strategy
|
||||
|
||||
Given the complexity of the code in this crate, along with the pervasive use
|
||||
of `unsafe`, this crate has an extensive testing strategy. It combines multiple
|
||||
approaches:
|
||||
|
||||
* Hand-written tests.
|
||||
* Exhaustive-style testing meant to exercise all possible branching and offset
|
||||
calculations.
|
||||
* Property based testing through [`quickcheck`](https://github.com/BurntSushi/quickcheck).
|
||||
* Fuzz testing through [`cargo fuzz`](https://github.com/rust-fuzz/cargo-fuzz).
|
||||
* A huge suite of benchmarks that are also run as tests. Benchmarks always
|
||||
confirm that the expected result occurs.
|
||||
|
||||
Improvements to the testing infrastructure are very welcome.
|
||||
|
||||
|
||||
### Algorithms used
|
||||
|
||||
At time of writing, this crate's implementation of substring search actually
|
||||
has a few different algorithms to choose from depending on the situation.
|
||||
|
||||
* For very small haystacks,
|
||||
[Rabin-Karp](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm)
|
||||
is used to reduce latency. Rabin-Karp has very small overhead and can often
|
||||
complete before other searchers have even been constructed.
|
||||
* For small needles, a variant of the
|
||||
["Generic SIMD"](http://0x80.pl/articles/simd-strfind.html#algorithm-1-generic-simd)
|
||||
algorithm is used. Instead of using the first and last bytes, a heuristic is
|
||||
used to select bytes based on a background distribution of byte frequencies.
|
||||
* In all other cases,
|
||||
[Two-Way](https://en.wikipedia.org/wiki/Two-way_string-matching_algorithm)
|
||||
is used. If possible, a prefilter based on the "Generic SIMD" algorithm
|
||||
linked above is used to find candidates quickly. A dynamic heuristic is used
|
||||
to detect if the prefilter is ineffective, and if so, disables it.
|
||||
|
||||
|
||||
### Why is the standard library's substring search so much slower?
|
||||
|
||||
We'll start by establishing what the difference in performance actually
|
||||
is. There are two relevant benchmark classes to consider: `prebuilt` and
|
||||
`oneshot`. The `prebuilt` benchmarks are designed to measure---to the extent
|
||||
possible---search time only. That is, the benchmark first starts by building a
|
||||
searcher and then only tracking the time for _using_ the searcher:
|
||||
|
||||
```
|
||||
$ rebar rank benchmarks/record/x86_64/2023-08-26.csv --intersection -e memchr/memmem/prebuilt -e std/memmem/prebuilt
|
||||
Engine Version Geometric mean of speed ratios Benchmark count
|
||||
------ ------- ------------------------------ ---------------
|
||||
rust/memchr/memmem/prebuilt 2.5.0 1.03 53
|
||||
rust/std/memmem/prebuilt 1.73.0-nightly 180dffba1 6.50 53
|
||||
```
|
||||
|
||||
Conversely, the `oneshot` benchmark class measures the time it takes to both
|
||||
build the searcher _and_ use it:
|
||||
|
||||
```
|
||||
$ rebar rank benchmarks/record/x86_64/2023-08-26.csv --intersection -e memchr/memmem/oneshot -e std/memmem/oneshot
|
||||
Engine Version Geometric mean of speed ratios Benchmark count
|
||||
------ ------- ------------------------------ ---------------
|
||||
rust/memchr/memmem/oneshot 2.5.0 1.04 53
|
||||
rust/std/memmem/oneshot 1.73.0-nightly 180dffba1 5.26 53
|
||||
```
|
||||
|
||||
**NOTE:** Replace `rebar rank` with `rebar cmp` in the above commands to
|
||||
explore the specific benchmarks and their differences.
|
||||
|
||||
So in both cases, this crate is quite a bit faster over a broad sampling of
|
||||
benchmarks regardless of whether you measure only search time or search time
|
||||
plus construction time. The difference is a little smaller when you include
|
||||
construction time in your measurements.
|
||||
|
||||
These two different types of benchmark classes make for a nice segue into
|
||||
one reason why the standard library's substring search can be slower: API
|
||||
design. In the standard library, the only APIs available to you require
|
||||
one to re-construct the searcher for every search. While you can benefit
|
||||
from building a searcher once and iterating over all matches in a single
|
||||
string, you cannot reuse that searcher to search other strings. This might
|
||||
come up when, for example, searching a file one line at a time. You'll need
|
||||
to re-build the searcher for every line searched, and this can [really
|
||||
matter][burntsushi-bstr-blog].
|
||||
|
||||
**NOTE:** The `prebuilt` benchmark for the standard library can't actually
|
||||
avoid measuring searcher construction at some level, because there is no API
|
||||
for it. Instead, the benchmark consists of building the searcher once and then
|
||||
finding all matches in a single string via an iterator. This tends to
|
||||
approximate a benchmark where searcher construction isn't measured, but it
|
||||
isn't perfect. While this means the comparison is not strictly
|
||||
apples-to-apples, it does reflect what is maximally possible with the standard
|
||||
library, and thus reflects the best that one could do in a real world scenario.
|
||||
|
||||
While there is more to the story than just API design here, it's important to
|
||||
point out that even if the standard library's substring search were a precise
|
||||
clone of this crate internally, it would still be at a disadvantage in some
|
||||
workloads because of its API. (The same also applies to C's standard library
|
||||
`memmem` function. There is no way to amortize construction of the searcher.
|
||||
You need to pay for it on every call.)
|
||||
|
||||
The other reason for the difference in performance is that
|
||||
the standard library has trouble using SIMD. In particular, substring search
|
||||
is implemented in the `core` library, where platform specific code generally
|
||||
can't exist. That's an issue because in order to utilize SIMD beyond SSE2
|
||||
while maintaining portable binaries, one needs to use [dynamic CPU feature
|
||||
detection][dynamic-cpu], and that in turn requires platform specific code.
|
||||
While there is [an RFC for enabling target feature detection in
|
||||
`core`][core-feature], it doesn't yet exist.
|
||||
|
||||
The bottom line here is that `core`'s substring search implementation is
|
||||
limited to making use of SSE2, but not AVX.
|
||||
|
||||
Still though, this crate does accelerate substring search even when only SSE2
|
||||
is available. The standard library could therefore adopt the techniques in this
|
||||
crate just for SSE2. The reason why that hasn't happened yet isn't totally
|
||||
clear to me. It likely needs a champion to push it through. The standard
|
||||
library tends to be more conservative in these things. With that said, the
|
||||
standard library does use some [SSE2 acceleration on `x86-64`][std-sse2] added
|
||||
in [this PR][std-sse2-pr]. However, at the time of writing, it is only used
|
||||
for short needles and doesn't use the frequency based heuristics found in this
|
||||
crate.
|
||||
|
||||
**NOTE:** Another thing worth mentioning is that the standard library's
|
||||
substring search routine requires that both the needle and haystack have type
|
||||
`&str`. Unless you can assume that your data is valid UTF-8, building a `&str`
|
||||
will come with the overhead of UTF-8 validation. This may in turn result in
|
||||
overall slower searching depending on your workload. In contrast, the `memchr`
|
||||
crate permits both the needle and the haystack to have type `&[u8]`, where
|
||||
`&[u8]` can be created from a `&str` with zero cost. Therefore, the substring
|
||||
search in this crate is strictly more flexible than what the standard library
|
||||
provides.
|
||||
|
||||
[burntsushi-bstr-blog]: https://blog.burntsushi.net/bstr/#motivation-based-on-performance
|
||||
[dynamic-cpu]: https://doc.rust-lang.org/std/arch/index.html#dynamic-cpu-feature-detection
|
||||
[core-feature]: https://github.com/rust-lang/rfcs/pull/3469
|
||||
[std-sse2]: https://github.com/rust-lang/rust/blob/bf9229a2e366b4c311f059014a4aa08af16de5d8/library/core/src/str/pattern.rs#L1719-L1857
|
||||
[std-sse2-pr]: https://github.com/rust-lang/rust/pull/103779
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
distribute this software, either in source code form or as a compiled
|
||||
binary, for any purpose, commercial or non-commercial, and by any
|
||||
means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors
|
||||
of this software dedicate any and all copyright interest in the
|
||||
software to the public domain. We make this dedication for the benefit
|
||||
of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of
|
||||
relinquishment in perpetuity of all present and future rights to this
|
||||
software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
max_width = 79
|
||||
use_small_heuristics = "max"
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
/*!
|
||||
Wrapper routines for `memchr` and friends.
|
||||
|
||||
These routines choose the best implementation at compile time. (This is
|
||||
different from `x86_64` because it is expected that `neon` is almost always
|
||||
available for `aarch64` targets.)
|
||||
*/
|
||||
|
||||
macro_rules! defraw {
|
||||
($ty:ident, $find:ident, $start:ident, $end:ident, $($needles:ident),+) => {{
|
||||
#[cfg(target_feature = "neon")]
|
||||
{
|
||||
use crate::arch::aarch64::neon::memchr::$ty;
|
||||
|
||||
debug!("chose neon for {}", stringify!($ty));
|
||||
debug_assert!($ty::is_available());
|
||||
// SAFETY: We know that wasm memchr is always available whenever
|
||||
// code is compiled for `aarch64` with the `neon` target feature
|
||||
// enabled.
|
||||
$ty::new_unchecked($($needles),+).$find($start, $end)
|
||||
}
|
||||
#[cfg(not(target_feature = "neon"))]
|
||||
{
|
||||
use crate::arch::all::memchr::$ty;
|
||||
|
||||
debug!(
|
||||
"no neon feature available, using fallback for {}",
|
||||
stringify!($ty),
|
||||
);
|
||||
$ty::new($($needles),+).$find($start, $end)
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
/// memchr, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `One::find_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memchr_raw(
|
||||
n1: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(One, find_raw, start, end, n1)
|
||||
}
|
||||
|
||||
/// memrchr, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `One::rfind_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memrchr_raw(
|
||||
n1: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(One, rfind_raw, start, end, n1)
|
||||
}
|
||||
|
||||
/// memchr2, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Two::find_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memchr2_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Two, find_raw, start, end, n1, n2)
|
||||
}
|
||||
|
||||
/// memrchr2, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Two::rfind_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memrchr2_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Two, rfind_raw, start, end, n1, n2)
|
||||
}
|
||||
|
||||
/// memchr3, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Three::find_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memchr3_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
n3: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Three, find_raw, start, end, n1, n2, n3)
|
||||
}
|
||||
|
||||
/// memrchr3, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Three::rfind_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memrchr3_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
n3: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Three, rfind_raw, start, end, n1, n2, n3)
|
||||
}
|
||||
|
||||
/// Count all matching bytes, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `One::count_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn count_raw(
|
||||
n1: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> usize {
|
||||
defraw!(One, count_raw, start, end, n1)
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
/*!
|
||||
Vector algorithms for the `aarch64` target.
|
||||
*/
|
||||
|
||||
pub mod neon;
|
||||
|
||||
pub(crate) mod memchr;
|
||||
+1031
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,6 @@
|
||||
/*!
|
||||
Algorithms for the `aarch64` target using 128-bit vectors via NEON.
|
||||
*/
|
||||
|
||||
pub mod memchr;
|
||||
pub mod packedpair;
|
||||
@@ -0,0 +1,236 @@
|
||||
/*!
|
||||
A 128-bit vector implementation of the "packed pair" SIMD algorithm.
|
||||
|
||||
The "packed pair" algorithm is based on the [generic SIMD] algorithm. The main
|
||||
difference is that it (by default) uses a background distribution of byte
|
||||
frequencies to heuristically select the pair of bytes to search for.
|
||||
|
||||
[generic SIMD]: http://0x80.pl/articles/simd-strfind.html#first-and-last
|
||||
*/
|
||||
|
||||
use core::arch::aarch64::uint8x16_t;
|
||||
|
||||
use crate::arch::{all::packedpair::Pair, generic::packedpair};
|
||||
|
||||
/// A "packed pair" finder that uses 128-bit vector operations.
|
||||
///
|
||||
/// This finder picks two bytes that it believes have high predictive power
|
||||
/// for indicating an overall match of a needle. Depending on whether
|
||||
/// `Finder::find` or `Finder::find_prefilter` is used, it reports offsets
|
||||
/// where the needle matches or could match. In the prefilter case, candidates
|
||||
/// are reported whenever the [`Pair`] of bytes given matches.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Finder(packedpair::Finder<uint8x16_t>);
|
||||
|
||||
/// A "packed pair" finder that uses 128-bit vector operations.
|
||||
///
|
||||
/// This finder picks two bytes that it believes have high predictive power
|
||||
/// for indicating an overall match of a needle. Depending on whether
|
||||
/// `Finder::find` or `Finder::find_prefilter` is used, it reports offsets
|
||||
/// where the needle matches or could match. In the prefilter case, candidates
|
||||
/// are reported whenever the [`Pair`] of bytes given matches.
|
||||
impl Finder {
|
||||
/// Create a new pair searcher. The searcher returned can either report
|
||||
/// exact matches of `needle` or act as a prefilter and report candidate
|
||||
/// positions of `needle`.
|
||||
///
|
||||
/// If neon is unavailable in the current environment or if a [`Pair`]
|
||||
/// could not be constructed from the needle given, then `None` is
|
||||
/// returned.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> Option<Finder> {
|
||||
Finder::with_pair(needle, Pair::new(needle)?)
|
||||
}
|
||||
|
||||
/// Create a new "packed pair" finder using the pair of bytes given.
|
||||
///
|
||||
/// This constructor permits callers to control precisely which pair of
|
||||
/// bytes is used as a predicate.
|
||||
///
|
||||
/// If neon is unavailable in the current environment, then `None` is
|
||||
/// returned.
|
||||
#[inline]
|
||||
pub fn with_pair(needle: &[u8], pair: Pair) -> Option<Finder> {
|
||||
if Finder::is_available() {
|
||||
// SAFETY: we check that NEON is available above. We are also
|
||||
// guaranteed to have needle.len() > 1 because we have a valid
|
||||
// Pair.
|
||||
unsafe { Some(Finder::with_pair_impl(needle, pair)) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new `Finder` specific to neon vectors and routines.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Same as the safety for `packedpair::Finder::new`, and callers must also
|
||||
/// ensure that neon is available.
|
||||
#[target_feature(enable = "neon")]
|
||||
#[inline]
|
||||
unsafe fn with_pair_impl(needle: &[u8], pair: Pair) -> Finder {
|
||||
let finder = packedpair::Finder::<uint8x16_t>::new(needle, pair);
|
||||
Finder(finder)
|
||||
}
|
||||
|
||||
/// Returns true when this implementation is available in the current
|
||||
/// environment.
|
||||
///
|
||||
/// When this is true, it is guaranteed that [`Finder::with_pair`] will
|
||||
/// return a `Some` value. Similarly, when it is false, it is guaranteed
|
||||
/// that `Finder::with_pair` will return a `None` value. Notice that this
|
||||
/// does not guarantee that [`Finder::new`] will return a `Finder`. Namely,
|
||||
/// even when `Finder::is_available` is true, it is not guaranteed that a
|
||||
/// valid [`Pair`] can be found from the needle given.
|
||||
///
|
||||
/// Note also that for the lifetime of a single program, if this returns
|
||||
/// true then it will always return true.
|
||||
#[inline]
|
||||
pub fn is_available() -> bool {
|
||||
#[cfg(target_feature = "neon")]
|
||||
{
|
||||
true
|
||||
}
|
||||
#[cfg(not(target_feature = "neon"))]
|
||||
{
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute a search using neon vectors and routines.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When `haystack.len()` is less than [`Finder::min_haystack_len`].
|
||||
#[inline]
|
||||
pub fn find(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
|
||||
// SAFETY: Building a `Finder` means it's safe to call 'neon' routines.
|
||||
unsafe { self.find_impl(haystack, needle) }
|
||||
}
|
||||
|
||||
/// Execute a search using neon vectors and routines.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When `haystack.len()` is less than [`Finder::min_haystack_len`].
|
||||
#[inline]
|
||||
pub fn find_prefilter(&self, haystack: &[u8]) -> Option<usize> {
|
||||
// SAFETY: Building a `Finder` means it's safe to call 'neon' routines.
|
||||
unsafe { self.find_prefilter_impl(haystack) }
|
||||
}
|
||||
|
||||
/// Execute a search using neon vectors and routines.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When `haystack.len()` is less than [`Finder::min_haystack_len`].
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// (The target feature safety obligation is automatically fulfilled by
|
||||
/// virtue of being a method on `Finder`, which can only be constructed
|
||||
/// when it is safe to call `neon` routines.)
|
||||
#[target_feature(enable = "neon")]
|
||||
#[inline]
|
||||
unsafe fn find_impl(
|
||||
&self,
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
) -> Option<usize> {
|
||||
self.0.find(haystack, needle)
|
||||
}
|
||||
|
||||
/// Execute a prefilter search using neon vectors and routines.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When `haystack.len()` is less than [`Finder::min_haystack_len`].
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// (The target feature safety obligation is automatically fulfilled by
|
||||
/// virtue of being a method on `Finder`, which can only be constructed
|
||||
/// when it is safe to call `neon` routines.)
|
||||
#[target_feature(enable = "neon")]
|
||||
#[inline]
|
||||
unsafe fn find_prefilter_impl(&self, haystack: &[u8]) -> Option<usize> {
|
||||
self.0.find_prefilter(haystack)
|
||||
}
|
||||
|
||||
/// Returns the pair of offsets (into the needle) used to check as a
|
||||
/// predicate before confirming whether a needle exists at a particular
|
||||
/// position.
|
||||
#[inline]
|
||||
pub fn pair(&self) -> &Pair {
|
||||
self.0.pair()
|
||||
}
|
||||
|
||||
/// Returns the minimum haystack length that this `Finder` can search.
|
||||
///
|
||||
/// Using a haystack with length smaller than this in a search will result
|
||||
/// in a panic. The reason for this restriction is that this finder is
|
||||
/// meant to be a low-level component that is part of a larger substring
|
||||
/// strategy. In that sense, it avoids trying to handle all cases and
|
||||
/// instead only handles the cases that it can handle very well.
|
||||
#[inline]
|
||||
pub fn min_haystack_len(&self) -> usize {
|
||||
self.0.min_haystack_len()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn find(haystack: &[u8], needle: &[u8]) -> Option<Option<usize>> {
|
||||
let f = Finder::new(needle)?;
|
||||
if haystack.len() < f.min_haystack_len() {
|
||||
return None;
|
||||
}
|
||||
Some(f.find(haystack, needle))
|
||||
}
|
||||
|
||||
define_substring_forward_quickcheck!(find);
|
||||
|
||||
#[test]
|
||||
fn forward_substring() {
|
||||
crate::tests::substring::Runner::new().fwd(find).run()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forward_packedpair() {
|
||||
fn find(
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
index1: u8,
|
||||
index2: u8,
|
||||
) -> Option<Option<usize>> {
|
||||
let pair = Pair::with_indices(needle, index1, index2)?;
|
||||
let f = Finder::with_pair(needle, pair)?;
|
||||
if haystack.len() < f.min_haystack_len() {
|
||||
return None;
|
||||
}
|
||||
Some(f.find(haystack, needle))
|
||||
}
|
||||
crate::tests::packedpair::Runner::new().fwd(find).run()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forward_packedpair_prefilter() {
|
||||
fn find(
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
index1: u8,
|
||||
index2: u8,
|
||||
) -> Option<Option<usize>> {
|
||||
let pair = Pair::with_indices(needle, index1, index2)?;
|
||||
let f = Finder::with_pair(needle, pair)?;
|
||||
if haystack.len() < f.min_haystack_len() {
|
||||
return None;
|
||||
}
|
||||
Some(f.find_prefilter(haystack))
|
||||
}
|
||||
crate::tests::packedpair::Runner::new().fwd(find).run()
|
||||
}
|
||||
}
|
||||
+1022
File diff suppressed because it is too large
Load Diff
+234
@@ -0,0 +1,234 @@
|
||||
/*!
|
||||
Contains architecture independent routines.
|
||||
|
||||
These routines are often used as a "fallback" implementation when the more
|
||||
specialized architecture dependent routines are unavailable.
|
||||
*/
|
||||
|
||||
pub mod memchr;
|
||||
pub mod packedpair;
|
||||
pub mod rabinkarp;
|
||||
#[cfg(feature = "alloc")]
|
||||
pub mod shiftor;
|
||||
pub mod twoway;
|
||||
|
||||
/// Returns true if and only if `needle` is a prefix of `haystack`.
|
||||
///
|
||||
/// This uses a latency optimized variant of `memcmp` internally which *might*
|
||||
/// make this faster for very short strings.
|
||||
///
|
||||
/// # Inlining
|
||||
///
|
||||
/// This routine is marked `inline(always)`. If you want to call this function
|
||||
/// in a way that is not always inlined, you'll need to wrap a call to it in
|
||||
/// another function that is marked as `inline(never)` or just `inline`.
|
||||
#[inline(always)]
|
||||
pub fn is_prefix(haystack: &[u8], needle: &[u8]) -> bool {
|
||||
needle.len() <= haystack.len()
|
||||
&& is_equal(&haystack[..needle.len()], needle)
|
||||
}
|
||||
|
||||
/// Returns true if and only if `needle` is a suffix of `haystack`.
|
||||
///
|
||||
/// This uses a latency optimized variant of `memcmp` internally which *might*
|
||||
/// make this faster for very short strings.
|
||||
///
|
||||
/// # Inlining
|
||||
///
|
||||
/// This routine is marked `inline(always)`. If you want to call this function
|
||||
/// in a way that is not always inlined, you'll need to wrap a call to it in
|
||||
/// another function that is marked as `inline(never)` or just `inline`.
|
||||
#[inline(always)]
|
||||
pub fn is_suffix(haystack: &[u8], needle: &[u8]) -> bool {
|
||||
needle.len() <= haystack.len()
|
||||
&& is_equal(&haystack[haystack.len() - needle.len()..], needle)
|
||||
}
|
||||
|
||||
/// Compare corresponding bytes in `x` and `y` for equality.
|
||||
///
|
||||
/// That is, this returns true if and only if `x.len() == y.len()` and
|
||||
/// `x[i] == y[i]` for all `0 <= i < x.len()`.
|
||||
///
|
||||
/// # Inlining
|
||||
///
|
||||
/// This routine is marked `inline(always)`. If you want to call this function
|
||||
/// in a way that is not always inlined, you'll need to wrap a call to it in
|
||||
/// another function that is marked as `inline(never)` or just `inline`.
|
||||
///
|
||||
/// # Motivation
|
||||
///
|
||||
/// Why not use slice equality instead? Well, slice equality usually results in
|
||||
/// a call out to the current platform's `libc` which might not be inlineable
|
||||
/// or have other overhead. This routine isn't guaranteed to be a win, but it
|
||||
/// might be in some cases.
|
||||
#[inline(always)]
|
||||
pub fn is_equal(x: &[u8], y: &[u8]) -> bool {
|
||||
if x.len() != y.len() {
|
||||
return false;
|
||||
}
|
||||
// SAFETY: Our pointers are derived directly from borrowed slices which
|
||||
// uphold all of our safety guarantees except for length. We account for
|
||||
// length with the check above.
|
||||
unsafe { is_equal_raw(x.as_ptr(), y.as_ptr(), x.len()) }
|
||||
}
|
||||
|
||||
/// Compare `n` bytes at the given pointers for equality.
|
||||
///
|
||||
/// This returns true if and only if `*x.add(i) == *y.add(i)` for all
|
||||
/// `0 <= i < n`.
|
||||
///
|
||||
/// # Inlining
|
||||
///
|
||||
/// This routine is marked `inline(always)`. If you want to call this function
|
||||
/// in a way that is not always inlined, you'll need to wrap a call to it in
|
||||
/// another function that is marked as `inline(never)` or just `inline`.
|
||||
///
|
||||
/// # Motivation
|
||||
///
|
||||
/// Why not use slice equality instead? Well, slice equality usually results in
|
||||
/// a call out to the current platform's `libc` which might not be inlineable
|
||||
/// or have other overhead. This routine isn't guaranteed to be a win, but it
|
||||
/// might be in some cases.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// * Both `x` and `y` must be valid for reads of up to `n` bytes.
|
||||
/// * Both `x` and `y` must point to an initialized value.
|
||||
/// * Both `x` and `y` must each point to an allocated object and
|
||||
/// must either be in bounds or at most one byte past the end of the
|
||||
/// allocated object. `x` and `y` do not need to point to the same allocated
|
||||
/// object, but they may.
|
||||
/// * Both `x` and `y` must be _derived from_ a pointer to their respective
|
||||
/// allocated objects.
|
||||
/// * The distance between `x` and `x+n` must not overflow `isize`. Similarly
|
||||
/// for `y` and `y+n`.
|
||||
/// * The distance being in bounds must not rely on "wrapping around" the
|
||||
/// address space.
|
||||
#[inline(always)]
|
||||
pub unsafe fn is_equal_raw(
|
||||
mut x: *const u8,
|
||||
mut y: *const u8,
|
||||
mut n: usize,
|
||||
) -> bool {
|
||||
// When we have 4 or more bytes to compare, then proceed in chunks of 4 at
|
||||
// a time using unaligned loads.
|
||||
//
|
||||
// Also, why do 4 byte loads instead of, say, 8 byte loads? The reason is
|
||||
// that this particular version of memcmp is likely to be called with tiny
|
||||
// needles. That means that if we do 8 byte loads, then a higher proportion
|
||||
// of memcmp calls will use the slower variant above. With that said, this
|
||||
// is a hypothesis and is only loosely supported by benchmarks. There's
|
||||
// likely some improvement that could be made here. The main thing here
|
||||
// though is to optimize for latency, not throughput.
|
||||
|
||||
// SAFETY: The caller is responsible for ensuring the pointers we get are
|
||||
// valid and readable for at least `n` bytes. We also do unaligned loads,
|
||||
// so there's no need to ensure we're aligned. (This is justified by this
|
||||
// routine being specifically for short strings.)
|
||||
while n >= 4 {
|
||||
let vx = x.cast::<u32>().read_unaligned();
|
||||
let vy = y.cast::<u32>().read_unaligned();
|
||||
if vx != vy {
|
||||
return false;
|
||||
}
|
||||
x = x.add(4);
|
||||
y = y.add(4);
|
||||
n -= 4;
|
||||
}
|
||||
// If we don't have enough bytes to do 4-byte at a time loads, then
|
||||
// do partial loads. Note that I used to have a byte-at-a-time
|
||||
// loop here and that turned out to be quite a bit slower for the
|
||||
// memmem/pathological/defeat-simple-vector-alphabet benchmark.
|
||||
if n >= 2 {
|
||||
let vx = x.cast::<u16>().read_unaligned();
|
||||
let vy = y.cast::<u16>().read_unaligned();
|
||||
if vx != vy {
|
||||
return false;
|
||||
}
|
||||
x = x.add(2);
|
||||
y = y.add(2);
|
||||
n -= 2;
|
||||
}
|
||||
if n > 0 {
|
||||
if x.read() != y.read() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn equals_different_lengths() {
|
||||
assert!(!is_equal(b"", b"a"));
|
||||
assert!(!is_equal(b"a", b""));
|
||||
assert!(!is_equal(b"ab", b"a"));
|
||||
assert!(!is_equal(b"a", b"ab"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equals_mismatch() {
|
||||
let one_mismatch = [
|
||||
(&b"a"[..], &b"x"[..]),
|
||||
(&b"ab"[..], &b"ax"[..]),
|
||||
(&b"abc"[..], &b"abx"[..]),
|
||||
(&b"abcd"[..], &b"abcx"[..]),
|
||||
(&b"abcde"[..], &b"abcdx"[..]),
|
||||
(&b"abcdef"[..], &b"abcdex"[..]),
|
||||
(&b"abcdefg"[..], &b"abcdefx"[..]),
|
||||
(&b"abcdefgh"[..], &b"abcdefgx"[..]),
|
||||
(&b"abcdefghi"[..], &b"abcdefghx"[..]),
|
||||
(&b"abcdefghij"[..], &b"abcdefghix"[..]),
|
||||
(&b"abcdefghijk"[..], &b"abcdefghijx"[..]),
|
||||
(&b"abcdefghijkl"[..], &b"abcdefghijkx"[..]),
|
||||
(&b"abcdefghijklm"[..], &b"abcdefghijklx"[..]),
|
||||
(&b"abcdefghijklmn"[..], &b"abcdefghijklmx"[..]),
|
||||
];
|
||||
for (x, y) in one_mismatch {
|
||||
assert_eq!(x.len(), y.len(), "lengths should match");
|
||||
assert!(!is_equal(x, y));
|
||||
assert!(!is_equal(y, x));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equals_yes() {
|
||||
assert!(is_equal(b"", b""));
|
||||
assert!(is_equal(b"a", b"a"));
|
||||
assert!(is_equal(b"ab", b"ab"));
|
||||
assert!(is_equal(b"abc", b"abc"));
|
||||
assert!(is_equal(b"abcd", b"abcd"));
|
||||
assert!(is_equal(b"abcde", b"abcde"));
|
||||
assert!(is_equal(b"abcdef", b"abcdef"));
|
||||
assert!(is_equal(b"abcdefg", b"abcdefg"));
|
||||
assert!(is_equal(b"abcdefgh", b"abcdefgh"));
|
||||
assert!(is_equal(b"abcdefghi", b"abcdefghi"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix() {
|
||||
assert!(is_prefix(b"", b""));
|
||||
assert!(is_prefix(b"a", b""));
|
||||
assert!(is_prefix(b"ab", b""));
|
||||
assert!(is_prefix(b"foo", b"foo"));
|
||||
assert!(is_prefix(b"foobar", b"foo"));
|
||||
|
||||
assert!(!is_prefix(b"foo", b"fob"));
|
||||
assert!(!is_prefix(b"foobar", b"fob"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suffix() {
|
||||
assert!(is_suffix(b"", b""));
|
||||
assert!(is_suffix(b"a", b""));
|
||||
assert!(is_suffix(b"ab", b""));
|
||||
assert!(is_suffix(b"foo", b"foo"));
|
||||
assert!(is_suffix(b"foobar", b"bar"));
|
||||
|
||||
assert!(!is_suffix(b"foo", b"goo"));
|
||||
assert!(!is_suffix(b"foobar", b"gar"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
pub(crate) const RANK: [u8; 256] = [
|
||||
55, // '\x00'
|
||||
52, // '\x01'
|
||||
51, // '\x02'
|
||||
50, // '\x03'
|
||||
49, // '\x04'
|
||||
48, // '\x05'
|
||||
47, // '\x06'
|
||||
46, // '\x07'
|
||||
45, // '\x08'
|
||||
103, // '\t'
|
||||
242, // '\n'
|
||||
66, // '\x0b'
|
||||
67, // '\x0c'
|
||||
229, // '\r'
|
||||
44, // '\x0e'
|
||||
43, // '\x0f'
|
||||
42, // '\x10'
|
||||
41, // '\x11'
|
||||
40, // '\x12'
|
||||
39, // '\x13'
|
||||
38, // '\x14'
|
||||
37, // '\x15'
|
||||
36, // '\x16'
|
||||
35, // '\x17'
|
||||
34, // '\x18'
|
||||
33, // '\x19'
|
||||
56, // '\x1a'
|
||||
32, // '\x1b'
|
||||
31, // '\x1c'
|
||||
30, // '\x1d'
|
||||
29, // '\x1e'
|
||||
28, // '\x1f'
|
||||
255, // ' '
|
||||
148, // '!'
|
||||
164, // '"'
|
||||
149, // '#'
|
||||
136, // '$'
|
||||
160, // '%'
|
||||
155, // '&'
|
||||
173, // "'"
|
||||
221, // '('
|
||||
222, // ')'
|
||||
134, // '*'
|
||||
122, // '+'
|
||||
232, // ','
|
||||
202, // '-'
|
||||
215, // '.'
|
||||
224, // '/'
|
||||
208, // '0'
|
||||
220, // '1'
|
||||
204, // '2'
|
||||
187, // '3'
|
||||
183, // '4'
|
||||
179, // '5'
|
||||
177, // '6'
|
||||
168, // '7'
|
||||
178, // '8'
|
||||
200, // '9'
|
||||
226, // ':'
|
||||
195, // ';'
|
||||
154, // '<'
|
||||
184, // '='
|
||||
174, // '>'
|
||||
126, // '?'
|
||||
120, // '@'
|
||||
191, // 'A'
|
||||
157, // 'B'
|
||||
194, // 'C'
|
||||
170, // 'D'
|
||||
189, // 'E'
|
||||
162, // 'F'
|
||||
161, // 'G'
|
||||
150, // 'H'
|
||||
193, // 'I'
|
||||
142, // 'J'
|
||||
137, // 'K'
|
||||
171, // 'L'
|
||||
176, // 'M'
|
||||
185, // 'N'
|
||||
167, // 'O'
|
||||
186, // 'P'
|
||||
112, // 'Q'
|
||||
175, // 'R'
|
||||
192, // 'S'
|
||||
188, // 'T'
|
||||
156, // 'U'
|
||||
140, // 'V'
|
||||
143, // 'W'
|
||||
123, // 'X'
|
||||
133, // 'Y'
|
||||
128, // 'Z'
|
||||
147, // '['
|
||||
138, // '\\'
|
||||
146, // ']'
|
||||
114, // '^'
|
||||
223, // '_'
|
||||
151, // '`'
|
||||
249, // 'a'
|
||||
216, // 'b'
|
||||
238, // 'c'
|
||||
236, // 'd'
|
||||
253, // 'e'
|
||||
227, // 'f'
|
||||
218, // 'g'
|
||||
230, // 'h'
|
||||
247, // 'i'
|
||||
135, // 'j'
|
||||
180, // 'k'
|
||||
241, // 'l'
|
||||
233, // 'm'
|
||||
246, // 'n'
|
||||
244, // 'o'
|
||||
231, // 'p'
|
||||
139, // 'q'
|
||||
245, // 'r'
|
||||
243, // 's'
|
||||
251, // 't'
|
||||
235, // 'u'
|
||||
201, // 'v'
|
||||
196, // 'w'
|
||||
240, // 'x'
|
||||
214, // 'y'
|
||||
152, // 'z'
|
||||
182, // '{'
|
||||
205, // '|'
|
||||
181, // '}'
|
||||
127, // '~'
|
||||
27, // '\x7f'
|
||||
212, // '\x80'
|
||||
211, // '\x81'
|
||||
210, // '\x82'
|
||||
213, // '\x83'
|
||||
228, // '\x84'
|
||||
197, // '\x85'
|
||||
169, // '\x86'
|
||||
159, // '\x87'
|
||||
131, // '\x88'
|
||||
172, // '\x89'
|
||||
105, // '\x8a'
|
||||
80, // '\x8b'
|
||||
98, // '\x8c'
|
||||
96, // '\x8d'
|
||||
97, // '\x8e'
|
||||
81, // '\x8f'
|
||||
207, // '\x90'
|
||||
145, // '\x91'
|
||||
116, // '\x92'
|
||||
115, // '\x93'
|
||||
144, // '\x94'
|
||||
130, // '\x95'
|
||||
153, // '\x96'
|
||||
121, // '\x97'
|
||||
107, // '\x98'
|
||||
132, // '\x99'
|
||||
109, // '\x9a'
|
||||
110, // '\x9b'
|
||||
124, // '\x9c'
|
||||
111, // '\x9d'
|
||||
82, // '\x9e'
|
||||
108, // '\x9f'
|
||||
118, // '\xa0'
|
||||
141, // '¡'
|
||||
113, // '¢'
|
||||
129, // '£'
|
||||
119, // '¤'
|
||||
125, // '¥'
|
||||
165, // '¦'
|
||||
117, // '§'
|
||||
92, // '¨'
|
||||
106, // '©'
|
||||
83, // 'ª'
|
||||
72, // '«'
|
||||
99, // '¬'
|
||||
93, // '\xad'
|
||||
65, // '®'
|
||||
79, // '¯'
|
||||
166, // '°'
|
||||
237, // '±'
|
||||
163, // '²'
|
||||
199, // '³'
|
||||
190, // '´'
|
||||
225, // 'µ'
|
||||
209, // '¶'
|
||||
203, // '·'
|
||||
198, // '¸'
|
||||
217, // '¹'
|
||||
219, // 'º'
|
||||
206, // '»'
|
||||
234, // '¼'
|
||||
248, // '½'
|
||||
158, // '¾'
|
||||
239, // '¿'
|
||||
255, // 'À'
|
||||
255, // 'Á'
|
||||
255, // 'Â'
|
||||
255, // 'Ã'
|
||||
255, // 'Ä'
|
||||
255, // 'Å'
|
||||
255, // 'Æ'
|
||||
255, // 'Ç'
|
||||
255, // 'È'
|
||||
255, // 'É'
|
||||
255, // 'Ê'
|
||||
255, // 'Ë'
|
||||
255, // 'Ì'
|
||||
255, // 'Í'
|
||||
255, // 'Î'
|
||||
255, // 'Ï'
|
||||
255, // 'Ð'
|
||||
255, // 'Ñ'
|
||||
255, // 'Ò'
|
||||
255, // 'Ó'
|
||||
255, // 'Ô'
|
||||
255, // 'Õ'
|
||||
255, // 'Ö'
|
||||
255, // '×'
|
||||
255, // 'Ø'
|
||||
255, // 'Ù'
|
||||
255, // 'Ú'
|
||||
255, // 'Û'
|
||||
255, // 'Ü'
|
||||
255, // 'Ý'
|
||||
255, // 'Þ'
|
||||
255, // 'ß'
|
||||
255, // 'à'
|
||||
255, // 'á'
|
||||
255, // 'â'
|
||||
255, // 'ã'
|
||||
255, // 'ä'
|
||||
255, // 'å'
|
||||
255, // 'æ'
|
||||
255, // 'ç'
|
||||
255, // 'è'
|
||||
255, // 'é'
|
||||
255, // 'ê'
|
||||
255, // 'ë'
|
||||
255, // 'ì'
|
||||
255, // 'í'
|
||||
255, // 'î'
|
||||
255, // 'ï'
|
||||
255, // 'ð'
|
||||
255, // 'ñ'
|
||||
255, // 'ò'
|
||||
255, // 'ó'
|
||||
255, // 'ô'
|
||||
255, // 'õ'
|
||||
255, // 'ö'
|
||||
255, // '÷'
|
||||
255, // 'ø'
|
||||
255, // 'ù'
|
||||
255, // 'ú'
|
||||
255, // 'û'
|
||||
255, // 'ü'
|
||||
255, // 'ý'
|
||||
255, // 'þ'
|
||||
255, // 'ÿ'
|
||||
];
|
||||
+359
@@ -0,0 +1,359 @@
|
||||
/*!
|
||||
Provides an architecture independent implementation of the "packed pair"
|
||||
algorithm.
|
||||
|
||||
The "packed pair" algorithm is based on the [generic SIMD] algorithm. The main
|
||||
difference is that it (by default) uses a background distribution of byte
|
||||
frequencies to heuristically select the pair of bytes to search for. Note that
|
||||
this module provides an architecture independent version that doesn't do as
|
||||
good of a job keeping the search for candidates inside a SIMD hot path. It
|
||||
however can be good enough in many circumstances.
|
||||
|
||||
[generic SIMD]: http://0x80.pl/articles/simd-strfind.html#first-and-last
|
||||
*/
|
||||
|
||||
use crate::memchr;
|
||||
|
||||
mod default_rank;
|
||||
|
||||
/// An architecture independent "packed pair" finder.
|
||||
///
|
||||
/// This finder picks two bytes that it believes have high predictive power for
|
||||
/// indicating an overall match of a needle. At search time, it reports offsets
|
||||
/// where the needle could match based on whether the pair of bytes it chose
|
||||
/// match.
|
||||
///
|
||||
/// This is architecture independent because it utilizes `memchr` to find the
|
||||
/// occurrence of one of the bytes in the pair, and then checks whether the
|
||||
/// second byte matches. If it does, in the case of [`Finder::find_prefilter`],
|
||||
/// the location at which the needle could match is returned.
|
||||
///
|
||||
/// It is generally preferred to use architecture specific routines for a
|
||||
/// "packed pair" prefilter, but this can be a useful fallback when the
|
||||
/// architecture independent routines are unavailable.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Finder {
|
||||
pair: Pair,
|
||||
byte1: u8,
|
||||
byte2: u8,
|
||||
}
|
||||
|
||||
impl Finder {
|
||||
/// Create a new prefilter that reports possible locations where the given
|
||||
/// needle matches.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> Option<Finder> {
|
||||
Finder::with_pair(needle, Pair::new(needle)?)
|
||||
}
|
||||
|
||||
/// Create a new prefilter using the pair given.
|
||||
///
|
||||
/// If the prefilter could not be constructed, then `None` is returned.
|
||||
///
|
||||
/// This constructor permits callers to control precisely which pair of
|
||||
/// bytes is used as a predicate.
|
||||
#[inline]
|
||||
pub fn with_pair(needle: &[u8], pair: Pair) -> Option<Finder> {
|
||||
let byte1 = needle[usize::from(pair.index1())];
|
||||
let byte2 = needle[usize::from(pair.index2())];
|
||||
// Currently this can never fail so we could just return a Finder,
|
||||
// but it's conceivable this could change.
|
||||
Some(Finder { pair, byte1, byte2 })
|
||||
}
|
||||
|
||||
/// Run this finder on the given haystack as a prefilter.
|
||||
///
|
||||
/// If a candidate match is found, then an offset where the needle *could*
|
||||
/// begin in the haystack is returned.
|
||||
#[inline]
|
||||
pub fn find_prefilter(&self, haystack: &[u8]) -> Option<usize> {
|
||||
let mut i = 0;
|
||||
let index1 = usize::from(self.pair.index1());
|
||||
let index2 = usize::from(self.pair.index2());
|
||||
loop {
|
||||
// Use a fast vectorized implementation to skip to the next
|
||||
// occurrence of the rarest byte (heuristically chosen) in the
|
||||
// needle.
|
||||
i += memchr(self.byte1, &haystack[i..])?;
|
||||
let found = i;
|
||||
i += 1;
|
||||
|
||||
// If we can't align our first byte match with the haystack, then a
|
||||
// match is impossible.
|
||||
let aligned1 = match found.checked_sub(index1) {
|
||||
None => continue,
|
||||
Some(aligned1) => aligned1,
|
||||
};
|
||||
|
||||
// Now align the second byte match with the haystack. A mismatch
|
||||
// means that a match is impossible.
|
||||
let aligned2 = match aligned1.checked_add(index2) {
|
||||
None => continue,
|
||||
Some(aligned_index2) => aligned_index2,
|
||||
};
|
||||
if haystack.get(aligned2).map_or(true, |&b| b != self.byte2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// We've done what we can. There might be a match here.
|
||||
return Some(aligned1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the pair of offsets (into the needle) used to check as a
|
||||
/// predicate before confirming whether a needle exists at a particular
|
||||
/// position.
|
||||
#[inline]
|
||||
pub fn pair(&self) -> &Pair {
|
||||
&self.pair
|
||||
}
|
||||
}
|
||||
|
||||
/// A pair of byte offsets into a needle to use as a predicate.
|
||||
///
|
||||
/// This pair is used as a predicate to quickly filter out positions in a
|
||||
/// haystack in which a needle cannot match. In some cases, this pair can even
|
||||
/// be used in vector algorithms such that the vector algorithm only switches
|
||||
/// over to scalar code once this pair has been found.
|
||||
///
|
||||
/// A pair of offsets can be used in both substring search implementations and
|
||||
/// in prefilters. The former will report matches of a needle in a haystack
|
||||
/// where as the latter will only report possible matches of a needle.
|
||||
///
|
||||
/// The offsets are limited each to a maximum of 255 to keep memory usage low.
|
||||
/// Moreover, it's rarely advantageous to create a predicate using offsets
|
||||
/// greater than 255 anyway.
|
||||
///
|
||||
/// The only guarantee enforced on the pair of offsets is that they are not
|
||||
/// equivalent. It is not necessarily the case that `index1 < index2` for
|
||||
/// example. By convention, `index1` corresponds to the byte in the needle
|
||||
/// that is believed to be most the predictive. Note also that because of the
|
||||
/// requirement that the indices be both valid for the needle used to build
|
||||
/// the pair and not equal, it follows that a pair can only be constructed for
|
||||
/// needles with length at least 2.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Pair {
|
||||
index1: u8,
|
||||
index2: u8,
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
/// Create a new pair of offsets from the given needle.
|
||||
///
|
||||
/// If a pair could not be created (for example, if the needle is too
|
||||
/// short), then `None` is returned.
|
||||
///
|
||||
/// This chooses the pair in the needle that is believed to be as
|
||||
/// predictive of an overall match of the needle as possible.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> Option<Pair> {
|
||||
Pair::with_ranker(needle, DefaultFrequencyRank)
|
||||
}
|
||||
|
||||
/// Create a new pair of offsets from the given needle and ranker.
|
||||
///
|
||||
/// This permits the caller to choose a background frequency distribution
|
||||
/// with which bytes are selected. The idea is to select a pair of bytes
|
||||
/// that is believed to strongly predict a match in the haystack. This
|
||||
/// usually means selecting bytes that occur rarely in a haystack.
|
||||
///
|
||||
/// If a pair could not be created (for example, if the needle is too
|
||||
/// short), then `None` is returned.
|
||||
#[inline]
|
||||
pub fn with_ranker<R: HeuristicFrequencyRank>(
|
||||
needle: &[u8],
|
||||
ranker: R,
|
||||
) -> Option<Pair> {
|
||||
if needle.len() <= 1 {
|
||||
return None;
|
||||
}
|
||||
// Find the rarest two bytes. We make them distinct indices by
|
||||
// construction. (The actual byte value may be the same in degenerate
|
||||
// cases, but that's OK.)
|
||||
let (mut rare1, mut index1) = (needle[0], 0);
|
||||
let (mut rare2, mut index2) = (needle[1], 1);
|
||||
if ranker.rank(rare2) < ranker.rank(rare1) {
|
||||
core::mem::swap(&mut rare1, &mut rare2);
|
||||
core::mem::swap(&mut index1, &mut index2);
|
||||
}
|
||||
let max = usize::from(core::u8::MAX);
|
||||
for (i, &b) in needle.iter().enumerate().take(max).skip(2) {
|
||||
if ranker.rank(b) < ranker.rank(rare1) {
|
||||
rare2 = rare1;
|
||||
index2 = index1;
|
||||
rare1 = b;
|
||||
index1 = u8::try_from(i).unwrap();
|
||||
} else if b != rare1 && ranker.rank(b) < ranker.rank(rare2) {
|
||||
rare2 = b;
|
||||
index2 = u8::try_from(i).unwrap();
|
||||
}
|
||||
}
|
||||
// While not strictly required for how a Pair is normally used, we
|
||||
// really don't want these to be equivalent. If they were, it would
|
||||
// reduce the effectiveness of candidate searching using these rare
|
||||
// bytes by increasing the rate of false positives.
|
||||
assert_ne!(index1, index2);
|
||||
Some(Pair { index1, index2 })
|
||||
}
|
||||
|
||||
/// Create a new pair using the offsets given for the needle given.
|
||||
///
|
||||
/// This bypasses any sort of heuristic process for choosing the offsets
|
||||
/// and permits the caller to choose the offsets themselves.
|
||||
///
|
||||
/// Indices are limited to valid `u8` values so that a `Pair` uses less
|
||||
/// memory. It is not possible to create a `Pair` with offsets bigger than
|
||||
/// `u8::MAX`. It's likely that such a thing is not needed, but if it is,
|
||||
/// it's suggested to build your own bespoke algorithm because you're
|
||||
/// likely working on a very niche case. (File an issue if this suggestion
|
||||
/// does not make sense to you.)
|
||||
///
|
||||
/// If a pair could not be created (for example, if the needle is too
|
||||
/// short), then `None` is returned.
|
||||
#[inline]
|
||||
pub fn with_indices(
|
||||
needle: &[u8],
|
||||
index1: u8,
|
||||
index2: u8,
|
||||
) -> Option<Pair> {
|
||||
// While not strictly required for how a Pair is normally used, we
|
||||
// really don't want these to be equivalent. If they were, it would
|
||||
// reduce the effectiveness of candidate searching using these rare
|
||||
// bytes by increasing the rate of false positives.
|
||||
if index1 == index2 {
|
||||
return None;
|
||||
}
|
||||
// Similarly, invalid indices means the Pair is invalid too.
|
||||
if usize::from(index1) >= needle.len() {
|
||||
return None;
|
||||
}
|
||||
if usize::from(index2) >= needle.len() {
|
||||
return None;
|
||||
}
|
||||
Some(Pair { index1, index2 })
|
||||
}
|
||||
|
||||
/// Returns the first offset of the pair.
|
||||
#[inline]
|
||||
pub fn index1(&self) -> u8 {
|
||||
self.index1
|
||||
}
|
||||
|
||||
/// Returns the second offset of the pair.
|
||||
#[inline]
|
||||
pub fn index2(&self) -> u8 {
|
||||
self.index2
|
||||
}
|
||||
}
|
||||
|
||||
/// This trait allows the user to customize the heuristic used to determine the
|
||||
/// relative frequency of a given byte in the dataset being searched.
|
||||
///
|
||||
/// The use of this trait can have a dramatic impact on performance depending
|
||||
/// on the type of data being searched. The details of why are explained in the
|
||||
/// docs of [`crate::memmem::Prefilter`]. To summarize, the core algorithm uses
|
||||
/// a prefilter to quickly identify candidate matches that are later verified
|
||||
/// more slowly. This prefilter is implemented in terms of trying to find
|
||||
/// `rare` bytes at specific offsets that will occur less frequently in the
|
||||
/// dataset. While the concept of a `rare` byte is similar for most datasets,
|
||||
/// there are some specific datasets (like binary executables) that have
|
||||
/// dramatically different byte distributions. For these datasets customizing
|
||||
/// the byte frequency heuristic can have a massive impact on performance, and
|
||||
/// might even need to be done at runtime.
|
||||
///
|
||||
/// The default implementation of `HeuristicFrequencyRank` reads from the
|
||||
/// static frequency table defined in `src/memmem/byte_frequencies.rs`. This
|
||||
/// is optimal for most inputs, so if you are unsure of the impact of using a
|
||||
/// custom `HeuristicFrequencyRank` you should probably just use the default.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use memchr::{
|
||||
/// arch::all::packedpair::HeuristicFrequencyRank,
|
||||
/// memmem::FinderBuilder,
|
||||
/// };
|
||||
///
|
||||
/// /// A byte-frequency table that is good for scanning binary executables.
|
||||
/// struct Binary;
|
||||
///
|
||||
/// impl HeuristicFrequencyRank for Binary {
|
||||
/// fn rank(&self, byte: u8) -> u8 {
|
||||
/// const TABLE: [u8; 256] = [
|
||||
/// 255, 128, 61, 43, 50, 41, 27, 28, 57, 15, 21, 13, 24, 17, 17,
|
||||
/// 89, 58, 16, 11, 7, 14, 23, 7, 6, 24, 9, 6, 5, 9, 4, 7, 16,
|
||||
/// 68, 11, 9, 6, 88, 7, 4, 4, 23, 9, 4, 8, 8, 5, 10, 4, 30, 11,
|
||||
/// 9, 24, 11, 5, 5, 5, 19, 11, 6, 17, 9, 9, 6, 8,
|
||||
/// 48, 58, 11, 14, 53, 40, 9, 9, 254, 35, 3, 6, 52, 23, 6, 6, 27,
|
||||
/// 4, 7, 11, 14, 13, 10, 11, 11, 5, 2, 10, 16, 12, 6, 19,
|
||||
/// 19, 20, 5, 14, 16, 31, 19, 7, 14, 20, 4, 4, 19, 8, 18, 20, 24,
|
||||
/// 1, 25, 19, 58, 29, 10, 5, 15, 20, 2, 2, 9, 4, 3, 5,
|
||||
/// 51, 11, 4, 53, 23, 39, 6, 4, 13, 81, 4, 186, 5, 67, 3, 2, 15,
|
||||
/// 0, 0, 1, 3, 2, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0,
|
||||
/// 12, 2, 1, 1, 3, 1, 1, 1, 6, 1, 2, 1, 3, 1, 1, 2, 9, 1, 1, 0,
|
||||
/// 2, 2, 4, 4, 11, 6, 7, 3, 6, 9, 4, 5,
|
||||
/// 46, 18, 8, 18, 17, 3, 8, 20, 16, 10, 3, 7, 175, 4, 6, 7, 13,
|
||||
/// 3, 7, 3, 3, 1, 3, 3, 10, 3, 1, 5, 2, 0, 1, 2,
|
||||
/// 16, 3, 5, 1, 6, 1, 1, 2, 58, 20, 3, 14, 12, 2, 1, 3, 16, 3, 5,
|
||||
/// 8, 3, 1, 8, 6, 17, 6, 5, 3, 8, 6, 13, 175,
|
||||
/// ];
|
||||
/// TABLE[byte as usize]
|
||||
/// }
|
||||
/// }
|
||||
/// // Create a new finder with the custom heuristic.
|
||||
/// let finder = FinderBuilder::new()
|
||||
/// .build_forward_with_ranker(Binary, b"\x00\x00\xdd\xdd");
|
||||
/// // Find needle with custom heuristic.
|
||||
/// assert!(finder.find(b"\x00\x00\x00\xdd\xdd").is_some());
|
||||
/// ```
|
||||
pub trait HeuristicFrequencyRank {
|
||||
/// Return the heuristic frequency rank of the given byte. A lower rank
|
||||
/// means the byte is believed to occur less frequently in the haystack.
|
||||
///
|
||||
/// Some uses of this heuristic may treat arbitrary absolute rank values as
|
||||
/// significant. For example, an implementation detail in this crate may
|
||||
/// determine that heuristic prefilters are inappropriate if every byte in
|
||||
/// the needle has a "high" rank.
|
||||
fn rank(&self, byte: u8) -> u8;
|
||||
}
|
||||
|
||||
/// The default byte frequency heuristic that is good for most haystacks.
|
||||
pub(crate) struct DefaultFrequencyRank;
|
||||
|
||||
impl HeuristicFrequencyRank for DefaultFrequencyRank {
|
||||
fn rank(&self, byte: u8) -> u8 {
|
||||
self::default_rank::RANK[usize::from(byte)]
|
||||
}
|
||||
}
|
||||
|
||||
/// This permits passing any implementation of `HeuristicFrequencyRank` as a
|
||||
/// borrowed version of itself.
|
||||
impl<'a, R> HeuristicFrequencyRank for &'a R
|
||||
where
|
||||
R: HeuristicFrequencyRank,
|
||||
{
|
||||
fn rank(&self, byte: u8) -> u8 {
|
||||
(**self).rank(byte)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn forward_packedpair() {
|
||||
fn find(
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
_index1: u8,
|
||||
_index2: u8,
|
||||
) -> Option<Option<usize>> {
|
||||
// We ignore the index positions requested since it winds up making
|
||||
// this test too slow overall.
|
||||
let f = Finder::new(needle)?;
|
||||
Some(f.find_prefilter(haystack))
|
||||
}
|
||||
crate::tests::packedpair::Runner::new().fwd(find).run()
|
||||
}
|
||||
}
|
||||
+390
@@ -0,0 +1,390 @@
|
||||
/*!
|
||||
An implementation of the [Rabin-Karp substring search algorithm][rabinkarp].
|
||||
|
||||
Rabin-Karp works by creating a hash of the needle provided and then computing
|
||||
a rolling hash for each needle sized window in the haystack. When the rolling
|
||||
hash matches the hash of the needle, a byte-wise comparison is done to check
|
||||
if a match exists. The worst case time complexity of Rabin-Karp is `O(m *
|
||||
n)` where `m ~ len(needle)` and `n ~ len(haystack)`. Its worst case space
|
||||
complexity is constant.
|
||||
|
||||
The main utility of Rabin-Karp is that the searcher can be constructed very
|
||||
quickly with very little memory. This makes it especially useful when searching
|
||||
for small needles in small haystacks, as it might finish its search before a
|
||||
beefier algorithm (like Two-Way) even starts.
|
||||
|
||||
[rabinkarp]: https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
|
||||
*/
|
||||
|
||||
/*
|
||||
(This was the comment I wrote for this module originally when it was not
|
||||
exposed. The comment still looks useful, but it's a bit in the weeds, so it's
|
||||
not public itself.)
|
||||
|
||||
This module implements the classical Rabin-Karp substring search algorithm,
|
||||
with no extra frills. While its use would seem to break our time complexity
|
||||
guarantee of O(m+n) (RK's time complexity is O(mn)), we are careful to only
|
||||
ever use RK on a constant subset of haystacks. The main point here is that
|
||||
RK has good latency properties for small needles/haystacks. It's very quick
|
||||
to compute a needle hash and zip through the haystack when compared to
|
||||
initializing Two-Way, for example. And this is especially useful for cases
|
||||
where the haystack is just too short for vector instructions to do much good.
|
||||
|
||||
The hashing function used here is the same one recommended by ESMAJ.
|
||||
|
||||
Another choice instead of Rabin-Karp would be Shift-Or. But its latency
|
||||
isn't quite as good since its preprocessing time is a bit more expensive
|
||||
(both in practice and in theory). However, perhaps Shift-Or has a place
|
||||
somewhere else for short patterns. I think the main problem is that it
|
||||
requires space proportional to the alphabet and the needle. If we, for
|
||||
example, supported needles up to length 16, then the total table size would be
|
||||
len(alphabet)*size_of::<u16>()==512 bytes. Which isn't exactly small, and it's
|
||||
probably bad to put that on the stack. So ideally, we'd throw it on the heap,
|
||||
but we'd really like to write as much code without using alloc/std as possible.
|
||||
But maybe it's worth the special casing. It's a TODO to benchmark.
|
||||
|
||||
Wikipedia has a decent explanation, if a bit heavy on the theory:
|
||||
https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
|
||||
|
||||
But ESMAJ provides something a bit more concrete:
|
||||
http://www-igm.univ-mlv.fr/~lecroq/string/node5.html
|
||||
|
||||
Finally, aho-corasick uses Rabin-Karp for multiple pattern match in some cases:
|
||||
https://github.com/BurntSushi/aho-corasick/blob/3852632f10587db0ff72ef29e88d58bf305a0946/src/packed/rabinkarp.rs
|
||||
*/
|
||||
|
||||
use crate::ext::Pointer;
|
||||
|
||||
/// A forward substring searcher using the Rabin-Karp algorithm.
|
||||
///
|
||||
/// Note that, as a lower level API, a `Finder` does not have access to the
|
||||
/// needle it was constructed with. For this reason, executing a search
|
||||
/// with a `Finder` requires passing both the needle and the haystack,
|
||||
/// where the needle is exactly equivalent to the one given to the `Finder`
|
||||
/// at construction time. This design was chosen so that callers can have
|
||||
/// more precise control over where and how many times a needle is stored.
|
||||
/// For example, in cases where Rabin-Karp is just one of several possible
|
||||
/// substring search algorithms.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Finder {
|
||||
/// The actual hash.
|
||||
hash: Hash,
|
||||
/// The factor needed to multiply a byte by in order to subtract it from
|
||||
/// the hash. It is defined to be 2^(n-1) (using wrapping exponentiation),
|
||||
/// where n is the length of the needle. This is how we "remove" a byte
|
||||
/// from the hash once the hash window rolls past it.
|
||||
hash_2pow: u32,
|
||||
}
|
||||
|
||||
impl Finder {
|
||||
/// Create a new Rabin-Karp forward searcher for the given `needle`.
|
||||
///
|
||||
/// The needle may be empty. The empty needle matches at every byte offset.
|
||||
///
|
||||
/// Note that callers must pass the same needle to all search calls using
|
||||
/// this `Finder`.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> Finder {
|
||||
let mut s = Finder { hash: Hash::new(), hash_2pow: 1 };
|
||||
let first_byte = match needle.first() {
|
||||
None => return s,
|
||||
Some(&first_byte) => first_byte,
|
||||
};
|
||||
s.hash.add(first_byte);
|
||||
for b in needle.iter().copied().skip(1) {
|
||||
s.hash.add(b);
|
||||
s.hash_2pow = s.hash_2pow.wrapping_shl(1);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// Return the first occurrence of the `needle` in the `haystack`
|
||||
/// given. If no such occurrence exists, then `None` is returned.
|
||||
///
|
||||
/// The `needle` provided must match the needle given to this finder at
|
||||
/// construction time.
|
||||
///
|
||||
/// The maximum value this can return is `haystack.len()`, which can only
|
||||
/// occur when the needle and haystack both have length zero. Otherwise,
|
||||
/// for non-empty haystacks, the maximum value is `haystack.len() - 1`.
|
||||
#[inline]
|
||||
pub fn find(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
|
||||
unsafe {
|
||||
let hstart = haystack.as_ptr();
|
||||
let hend = hstart.add(haystack.len());
|
||||
let nstart = needle.as_ptr();
|
||||
let nend = nstart.add(needle.len());
|
||||
let found = self.find_raw(hstart, hend, nstart, nend)?;
|
||||
Some(found.distance(hstart))
|
||||
}
|
||||
}
|
||||
|
||||
/// Like `find`, but accepts and returns raw pointers.
|
||||
///
|
||||
/// When a match is found, the pointer returned is guaranteed to be
|
||||
/// `>= start` and `<= end`. The pointer returned is only ever equivalent
|
||||
/// to `end` when both the needle and haystack are empty. (That is, the
|
||||
/// empty string matches the empty string.)
|
||||
///
|
||||
/// This routine is useful if you're already using raw pointers and would
|
||||
/// like to avoid converting back to a slice before executing a search.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Note that `start` and `end` below refer to both pairs of pointers given
|
||||
/// to this routine. That is, the conditions apply to both `hstart`/`hend`
|
||||
/// and `nstart`/`nend`.
|
||||
///
|
||||
/// * Both `start` and `end` must be valid for reads.
|
||||
/// * Both `start` and `end` must point to an initialized value.
|
||||
/// * Both `start` and `end` must point to the same allocated object and
|
||||
/// must either be in bounds or at most one byte past the end of the
|
||||
/// allocated object.
|
||||
/// * Both `start` and `end` must be _derived from_ a pointer to the same
|
||||
/// object.
|
||||
/// * The distance between `start` and `end` must not overflow `isize`.
|
||||
/// * The distance being in bounds must not rely on "wrapping around" the
|
||||
/// address space.
|
||||
/// * It must be the case that `start <= end`.
|
||||
#[inline]
|
||||
pub unsafe fn find_raw(
|
||||
&self,
|
||||
hstart: *const u8,
|
||||
hend: *const u8,
|
||||
nstart: *const u8,
|
||||
nend: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
let hlen = hend.distance(hstart);
|
||||
let nlen = nend.distance(nstart);
|
||||
if nlen > hlen {
|
||||
return None;
|
||||
}
|
||||
let mut cur = hstart;
|
||||
let end = hend.sub(nlen);
|
||||
let mut hash = Hash::forward(cur, cur.add(nlen));
|
||||
loop {
|
||||
if self.hash == hash && is_equal_raw(cur, nstart, nlen) {
|
||||
return Some(cur);
|
||||
}
|
||||
if cur >= end {
|
||||
return None;
|
||||
}
|
||||
hash.roll(self, cur.read(), cur.add(nlen).read());
|
||||
cur = cur.add(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A reverse substring searcher using the Rabin-Karp algorithm.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FinderRev(Finder);
|
||||
|
||||
impl FinderRev {
|
||||
/// Create a new Rabin-Karp reverse searcher for the given `needle`.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> FinderRev {
|
||||
let mut s = FinderRev(Finder { hash: Hash::new(), hash_2pow: 1 });
|
||||
let last_byte = match needle.last() {
|
||||
None => return s,
|
||||
Some(&last_byte) => last_byte,
|
||||
};
|
||||
s.0.hash.add(last_byte);
|
||||
for b in needle.iter().rev().copied().skip(1) {
|
||||
s.0.hash.add(b);
|
||||
s.0.hash_2pow = s.0.hash_2pow.wrapping_shl(1);
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// Return the last occurrence of the `needle` in the `haystack`
|
||||
/// given. If no such occurrence exists, then `None` is returned.
|
||||
///
|
||||
/// The `needle` provided must match the needle given to this finder at
|
||||
/// construction time.
|
||||
///
|
||||
/// The maximum value this can return is `haystack.len()`, which can only
|
||||
/// occur when the needle and haystack both have length zero. Otherwise,
|
||||
/// for non-empty haystacks, the maximum value is `haystack.len() - 1`.
|
||||
#[inline]
|
||||
pub fn rfind(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
|
||||
unsafe {
|
||||
let hstart = haystack.as_ptr();
|
||||
let hend = hstart.add(haystack.len());
|
||||
let nstart = needle.as_ptr();
|
||||
let nend = nstart.add(needle.len());
|
||||
let found = self.rfind_raw(hstart, hend, nstart, nend)?;
|
||||
Some(found.distance(hstart))
|
||||
}
|
||||
}
|
||||
|
||||
/// Like `rfind`, but accepts and returns raw pointers.
|
||||
///
|
||||
/// When a match is found, the pointer returned is guaranteed to be
|
||||
/// `>= start` and `<= end`. The pointer returned is only ever equivalent
|
||||
/// to `end` when both the needle and haystack are empty. (That is, the
|
||||
/// empty string matches the empty string.)
|
||||
///
|
||||
/// This routine is useful if you're already using raw pointers and would
|
||||
/// like to avoid converting back to a slice before executing a search.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Note that `start` and `end` below refer to both pairs of pointers given
|
||||
/// to this routine. That is, the conditions apply to both `hstart`/`hend`
|
||||
/// and `nstart`/`nend`.
|
||||
///
|
||||
/// * Both `start` and `end` must be valid for reads.
|
||||
/// * Both `start` and `end` must point to an initialized value.
|
||||
/// * Both `start` and `end` must point to the same allocated object and
|
||||
/// must either be in bounds or at most one byte past the end of the
|
||||
/// allocated object.
|
||||
/// * Both `start` and `end` must be _derived from_ a pointer to the same
|
||||
/// object.
|
||||
/// * The distance between `start` and `end` must not overflow `isize`.
|
||||
/// * The distance being in bounds must not rely on "wrapping around" the
|
||||
/// address space.
|
||||
/// * It must be the case that `start <= end`.
|
||||
#[inline]
|
||||
pub unsafe fn rfind_raw(
|
||||
&self,
|
||||
hstart: *const u8,
|
||||
hend: *const u8,
|
||||
nstart: *const u8,
|
||||
nend: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
let hlen = hend.distance(hstart);
|
||||
let nlen = nend.distance(nstart);
|
||||
if nlen > hlen {
|
||||
return None;
|
||||
}
|
||||
let mut cur = hend.sub(nlen);
|
||||
let start = hstart;
|
||||
let mut hash = Hash::reverse(cur, cur.add(nlen));
|
||||
loop {
|
||||
if self.0.hash == hash && is_equal_raw(cur, nstart, nlen) {
|
||||
return Some(cur);
|
||||
}
|
||||
if cur <= start {
|
||||
return None;
|
||||
}
|
||||
cur = cur.sub(1);
|
||||
hash.roll(&self.0, cur.add(nlen).read(), cur.read());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether RK is believed to be very fast for the given needle/haystack.
|
||||
#[inline]
|
||||
pub(crate) fn is_fast(haystack: &[u8], _needle: &[u8]) -> bool {
|
||||
haystack.len() < 16
|
||||
}
|
||||
|
||||
/// A Rabin-Karp hash. This might represent the hash of a needle, or the hash
|
||||
/// of a rolling window in the haystack.
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
struct Hash(u32);
|
||||
|
||||
impl Hash {
|
||||
/// Create a new hash that represents the empty string.
|
||||
#[inline(always)]
|
||||
fn new() -> Hash {
|
||||
Hash(0)
|
||||
}
|
||||
|
||||
/// Create a new hash from the bytes given for use in forward searches.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The given pointers must be valid to read from within their range.
|
||||
#[inline(always)]
|
||||
unsafe fn forward(mut start: *const u8, end: *const u8) -> Hash {
|
||||
let mut hash = Hash::new();
|
||||
while start < end {
|
||||
hash.add(start.read());
|
||||
start = start.add(1);
|
||||
}
|
||||
hash
|
||||
}
|
||||
|
||||
/// Create a new hash from the bytes given for use in reverse searches.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The given pointers must be valid to read from within their range.
|
||||
#[inline(always)]
|
||||
unsafe fn reverse(start: *const u8, mut end: *const u8) -> Hash {
|
||||
let mut hash = Hash::new();
|
||||
while start < end {
|
||||
end = end.sub(1);
|
||||
hash.add(end.read());
|
||||
}
|
||||
hash
|
||||
}
|
||||
|
||||
/// Add 'new' and remove 'old' from this hash. The given needle hash should
|
||||
/// correspond to the hash computed for the needle being searched for.
|
||||
///
|
||||
/// This is meant to be used when the rolling window of the haystack is
|
||||
/// advanced.
|
||||
#[inline(always)]
|
||||
fn roll(&mut self, finder: &Finder, old: u8, new: u8) {
|
||||
self.del(finder, old);
|
||||
self.add(new);
|
||||
}
|
||||
|
||||
/// Add a byte to this hash.
|
||||
#[inline(always)]
|
||||
fn add(&mut self, byte: u8) {
|
||||
self.0 = self.0.wrapping_shl(1).wrapping_add(u32::from(byte));
|
||||
}
|
||||
|
||||
/// Remove a byte from this hash. The given needle hash should correspond
|
||||
/// to the hash computed for the needle being searched for.
|
||||
#[inline(always)]
|
||||
fn del(&mut self, finder: &Finder, byte: u8) {
|
||||
let factor = finder.hash_2pow;
|
||||
self.0 = self.0.wrapping_sub(u32::from(byte).wrapping_mul(factor));
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true when `x[i] == y[i]` for all `0 <= i < n`.
|
||||
///
|
||||
/// We forcefully don't inline this to hint at the compiler that it is unlikely
|
||||
/// to be called. This causes the inner rabinkarp loop above to be a bit
|
||||
/// tighter and leads to some performance improvement. See the
|
||||
/// memmem/krate/prebuilt/sliceslice-words/words benchmark.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Same as `crate::arch::all::is_equal_raw`.
|
||||
#[cold]
|
||||
#[inline(never)]
|
||||
unsafe fn is_equal_raw(x: *const u8, y: *const u8, n: usize) -> bool {
|
||||
crate::arch::all::is_equal_raw(x, y, n)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
define_substring_forward_quickcheck!(|h, n| Some(
|
||||
Finder::new(n).find(h, n)
|
||||
));
|
||||
define_substring_reverse_quickcheck!(|h, n| Some(
|
||||
FinderRev::new(n).rfind(h, n)
|
||||
));
|
||||
|
||||
#[test]
|
||||
fn forward() {
|
||||
crate::tests::substring::Runner::new()
|
||||
.fwd(|h, n| Some(Finder::new(n).find(h, n)))
|
||||
.run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse() {
|
||||
crate::tests::substring::Runner::new()
|
||||
.rev(|h, n| Some(FinderRev::new(n).rfind(h, n)))
|
||||
.run();
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*!
|
||||
An implementation of the [Shift-Or substring search algorithm][shiftor].
|
||||
|
||||
[shiftor]: https://en.wikipedia.org/wiki/Bitap_algorithm
|
||||
*/
|
||||
|
||||
use alloc::boxed::Box;
|
||||
|
||||
/// The type of our mask.
|
||||
///
|
||||
/// While we don't expose anyway to configure this in the public API, if one
|
||||
/// really needs less memory usage or support for longer needles, then it is
|
||||
/// suggested to copy the code from this module and modify it to fit your
|
||||
/// needs. The code below is written to be correct regardless of whether Mask
|
||||
/// is a u8, u16, u32, u64 or u128.
|
||||
type Mask = u16;
|
||||
|
||||
/// A forward substring searcher using the Shift-Or algorithm.
|
||||
#[derive(Debug)]
|
||||
pub struct Finder {
|
||||
masks: Box<[Mask; 256]>,
|
||||
needle_len: usize,
|
||||
}
|
||||
|
||||
impl Finder {
|
||||
const MAX_NEEDLE_LEN: usize = (Mask::BITS - 1) as usize;
|
||||
|
||||
/// Create a new Shift-Or forward searcher for the given `needle`.
|
||||
///
|
||||
/// The needle may be empty. The empty needle matches at every byte offset.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> Option<Finder> {
|
||||
let needle_len = needle.len();
|
||||
if needle_len > Finder::MAX_NEEDLE_LEN {
|
||||
// A match is found when bit 7 is set in 'result' in the search
|
||||
// routine below. So our needle can't be bigger than 7. We could
|
||||
// permit bigger needles by using u16, u32 or u64 for our mask
|
||||
// entries. But this is all we need for this example.
|
||||
return None;
|
||||
}
|
||||
let mut searcher = Finder { masks: Box::from([!0; 256]), needle_len };
|
||||
for (i, &byte) in needle.iter().enumerate() {
|
||||
searcher.masks[usize::from(byte)] &= !(1 << i);
|
||||
}
|
||||
Some(searcher)
|
||||
}
|
||||
|
||||
/// Return the first occurrence of the needle given to `Finder::new` in
|
||||
/// the `haystack` given. If no such occurrence exists, then `None` is
|
||||
/// returned.
|
||||
///
|
||||
/// Unlike most other substring search implementations in this crate, this
|
||||
/// finder does not require passing the needle at search time. A match can
|
||||
/// be determined without the needle at all since the required information
|
||||
/// is already encoded into this finder at construction time.
|
||||
///
|
||||
/// The maximum value this can return is `haystack.len()`, which can only
|
||||
/// occur when the needle and haystack both have length zero. Otherwise,
|
||||
/// for non-empty haystacks, the maximum value is `haystack.len() - 1`.
|
||||
#[inline]
|
||||
pub fn find(&self, haystack: &[u8]) -> Option<usize> {
|
||||
if self.needle_len == 0 {
|
||||
return Some(0);
|
||||
}
|
||||
let mut result = !1;
|
||||
for (i, &byte) in haystack.iter().enumerate() {
|
||||
result |= self.masks[usize::from(byte)];
|
||||
result <<= 1;
|
||||
if result & (1 << self.needle_len) == 0 {
|
||||
return Some(i + 1 - self.needle_len);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
define_substring_forward_quickcheck!(|h, n| Some(Finder::new(n)?.find(h)));
|
||||
|
||||
#[test]
|
||||
fn forward() {
|
||||
crate::tests::substring::Runner::new()
|
||||
.fwd(|h, n| Some(Finder::new(n)?.find(h)))
|
||||
.run();
|
||||
}
|
||||
}
|
||||
+877
@@ -0,0 +1,877 @@
|
||||
/*!
|
||||
An implementation of the [Two-Way substring search algorithm][two-way].
|
||||
|
||||
[`Finder`] can be built for forward searches, while [`FinderRev`] can be built
|
||||
for reverse searches.
|
||||
|
||||
Two-Way makes for a nice general purpose substring search algorithm because of
|
||||
its time and space complexity properties. It also performs well in practice.
|
||||
Namely, with `m = len(needle)` and `n = len(haystack)`, Two-Way takes `O(m)`
|
||||
time to create a finder, `O(1)` space and `O(n)` search time. In other words,
|
||||
the preprocessing step is quick, doesn't require any heap memory and the worst
|
||||
case search time is guaranteed to be linear in the haystack regardless of the
|
||||
size of the needle.
|
||||
|
||||
While vector algorithms will usually beat Two-Way handedly, vector algorithms
|
||||
also usually have pathological or edge cases that are better handled by Two-Way.
|
||||
Moreover, not all targets support vector algorithms or implementations for them
|
||||
simply may not exist yet.
|
||||
|
||||
Two-Way can be found in the `memmem` implementations in at least [GNU libc] and
|
||||
[musl].
|
||||
|
||||
[two-way]: https://en.wikipedia.org/wiki/Two-way_string-matching_algorithm
|
||||
[GNU libc]: https://www.gnu.org/software/libc/
|
||||
[musl]: https://www.musl-libc.org/
|
||||
*/
|
||||
|
||||
use core::cmp;
|
||||
|
||||
use crate::{
|
||||
arch::all::{is_prefix, is_suffix},
|
||||
memmem::Pre,
|
||||
};
|
||||
|
||||
/// A forward substring searcher that uses the Two-Way algorithm.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Finder(TwoWay);
|
||||
|
||||
/// A reverse substring searcher that uses the Two-Way algorithm.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct FinderRev(TwoWay);
|
||||
|
||||
/// An implementation of the TwoWay substring search algorithm.
|
||||
///
|
||||
/// This searcher supports forward and reverse search, although not
|
||||
/// simultaneously. It runs in `O(n + m)` time and `O(1)` space, where
|
||||
/// `n ~ len(needle)` and `m ~ len(haystack)`.
|
||||
///
|
||||
/// The implementation here roughly matches that which was developed by
|
||||
/// Crochemore and Perrin in their 1991 paper "Two-way string-matching." The
|
||||
/// changes in this implementation are 1) the use of zero-based indices, 2) a
|
||||
/// heuristic skip table based on the last byte (borrowed from Rust's standard
|
||||
/// library) and 3) the addition of heuristics for a fast skip loop. For (3),
|
||||
/// callers can pass any kind of prefilter they want, but usually it's one
|
||||
/// based on a heuristic that uses an approximate background frequency of bytes
|
||||
/// to choose rare bytes to quickly look for candidate match positions. Note
|
||||
/// though that currently, this prefilter functionality is not exposed directly
|
||||
/// in the public API. (File an issue if you want it and provide a use case
|
||||
/// please.)
|
||||
///
|
||||
/// The heuristic for fast skipping is automatically shut off if it's
|
||||
/// detected to be ineffective at search time. Generally, this only occurs in
|
||||
/// pathological cases. But this is generally necessary in order to preserve
|
||||
/// a `O(n + m)` time bound.
|
||||
///
|
||||
/// The code below is fairly complex and not obviously correct at all. It's
|
||||
/// likely necessary to read the Two-Way paper cited above in order to fully
|
||||
/// grok this code. The essence of it is:
|
||||
///
|
||||
/// 1. Do something to detect a "critical" position in the needle.
|
||||
/// 2. For the current position in the haystack, look if `needle[critical..]`
|
||||
/// matches at that position.
|
||||
/// 3. If so, look if `needle[..critical]` matches.
|
||||
/// 4. If a mismatch occurs, shift the search by some amount based on the
|
||||
/// critical position and a pre-computed shift.
|
||||
///
|
||||
/// This type is wrapped in the forward and reverse finders that expose
|
||||
/// consistent forward or reverse APIs.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct TwoWay {
|
||||
/// A small bitset used as a quick prefilter (in addition to any prefilter
|
||||
/// given by the caller). Namely, a bit `i` is set if and only if `b%64==i`
|
||||
/// for any `b == needle[i]`.
|
||||
///
|
||||
/// When used as a prefilter, if the last byte at the current candidate
|
||||
/// position is NOT in this set, then we can skip that entire candidate
|
||||
/// position (the length of the needle). This is essentially the shift
|
||||
/// trick found in Boyer-Moore, but only applied to bytes that don't appear
|
||||
/// in the needle.
|
||||
///
|
||||
/// N.B. This trick was inspired by something similar in std's
|
||||
/// implementation of Two-Way.
|
||||
byteset: ApproximateByteSet,
|
||||
/// A critical position in needle. Specifically, this position corresponds
|
||||
/// to beginning of either the minimal or maximal suffix in needle. (N.B.
|
||||
/// See SuffixType below for why "minimal" isn't quite the correct word
|
||||
/// here.)
|
||||
///
|
||||
/// This is the position at which every search begins. Namely, search
|
||||
/// starts by scanning text to the right of this position, and only if
|
||||
/// there's a match does the text to the left of this position get scanned.
|
||||
critical_pos: usize,
|
||||
/// The amount we shift by in the Two-Way search algorithm. This
|
||||
/// corresponds to the "small period" and "large period" cases.
|
||||
shift: Shift,
|
||||
}
|
||||
|
||||
impl Finder {
|
||||
/// Create a searcher that finds occurrences of the given `needle`.
|
||||
///
|
||||
/// An empty `needle` results in a match at every position in a haystack,
|
||||
/// including at `haystack.len()`.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> Finder {
|
||||
let byteset = ApproximateByteSet::new(needle);
|
||||
let min_suffix = Suffix::forward(needle, SuffixKind::Minimal);
|
||||
let max_suffix = Suffix::forward(needle, SuffixKind::Maximal);
|
||||
let (period_lower_bound, critical_pos) =
|
||||
if min_suffix.pos > max_suffix.pos {
|
||||
(min_suffix.period, min_suffix.pos)
|
||||
} else {
|
||||
(max_suffix.period, max_suffix.pos)
|
||||
};
|
||||
let shift = Shift::forward(needle, period_lower_bound, critical_pos);
|
||||
Finder(TwoWay { byteset, critical_pos, shift })
|
||||
}
|
||||
|
||||
/// Returns the first occurrence of `needle` in the given `haystack`, or
|
||||
/// `None` if no such occurrence could be found.
|
||||
///
|
||||
/// The `needle` given must be the same as the `needle` provided to
|
||||
/// [`Finder::new`].
|
||||
///
|
||||
/// An empty `needle` results in a match at every position in a haystack,
|
||||
/// including at `haystack.len()`.
|
||||
#[inline]
|
||||
pub fn find(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
|
||||
self.find_with_prefilter(None, haystack, needle)
|
||||
}
|
||||
|
||||
/// This is like [`Finder::find`], but it accepts a prefilter for
|
||||
/// accelerating searches.
|
||||
///
|
||||
/// Currently this is not exposed in the public API because, at the time
|
||||
/// of writing, I didn't want to spend time thinking about how to expose
|
||||
/// the prefilter infrastructure (if at all). If you have a compelling use
|
||||
/// case for exposing this routine, please create an issue. Do *not* open
|
||||
/// a PR that just exposes `Pre` and friends. Exporting this routine will
|
||||
/// require API design.
|
||||
#[inline(always)]
|
||||
pub(crate) fn find_with_prefilter(
|
||||
&self,
|
||||
pre: Option<Pre<'_>>,
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
) -> Option<usize> {
|
||||
match self.0.shift {
|
||||
Shift::Small { period } => {
|
||||
self.find_small_imp(pre, haystack, needle, period)
|
||||
}
|
||||
Shift::Large { shift } => {
|
||||
self.find_large_imp(pre, haystack, needle, shift)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Each of the two search implementations below can be accelerated by a
|
||||
// prefilter, but it is not always enabled. To avoid its overhead when
|
||||
// its disabled, we explicitly inline each search implementation based on
|
||||
// whether a prefilter will be used or not. The decision on which to use
|
||||
// is made in the parent meta searcher.
|
||||
|
||||
#[inline(always)]
|
||||
fn find_small_imp(
|
||||
&self,
|
||||
mut pre: Option<Pre<'_>>,
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
period: usize,
|
||||
) -> Option<usize> {
|
||||
let mut pos = 0;
|
||||
let mut shift = 0;
|
||||
let last_byte_pos = match needle.len().checked_sub(1) {
|
||||
None => return Some(pos),
|
||||
Some(last_byte) => last_byte,
|
||||
};
|
||||
while pos + needle.len() <= haystack.len() {
|
||||
let mut i = cmp::max(self.0.critical_pos, shift);
|
||||
if let Some(pre) = pre.as_mut() {
|
||||
if pre.is_effective() {
|
||||
pos += pre.find(&haystack[pos..])?;
|
||||
shift = 0;
|
||||
i = self.0.critical_pos;
|
||||
if pos + needle.len() > haystack.len() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !self.0.byteset.contains(haystack[pos + last_byte_pos]) {
|
||||
pos += needle.len();
|
||||
shift = 0;
|
||||
continue;
|
||||
}
|
||||
while i < needle.len() && needle[i] == haystack[pos + i] {
|
||||
i += 1;
|
||||
}
|
||||
if i < needle.len() {
|
||||
pos += i - self.0.critical_pos + 1;
|
||||
shift = 0;
|
||||
} else {
|
||||
let mut j = self.0.critical_pos;
|
||||
while j > shift && needle[j] == haystack[pos + j] {
|
||||
j -= 1;
|
||||
}
|
||||
if j <= shift && needle[shift] == haystack[pos + shift] {
|
||||
return Some(pos);
|
||||
}
|
||||
pos += period;
|
||||
shift = needle.len() - period;
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn find_large_imp(
|
||||
&self,
|
||||
mut pre: Option<Pre<'_>>,
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
shift: usize,
|
||||
) -> Option<usize> {
|
||||
let mut pos = 0;
|
||||
let last_byte_pos = match needle.len().checked_sub(1) {
|
||||
None => return Some(pos),
|
||||
Some(last_byte) => last_byte,
|
||||
};
|
||||
'outer: while pos + needle.len() <= haystack.len() {
|
||||
if let Some(pre) = pre.as_mut() {
|
||||
if pre.is_effective() {
|
||||
pos += pre.find(&haystack[pos..])?;
|
||||
if pos + needle.len() > haystack.len() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !self.0.byteset.contains(haystack[pos + last_byte_pos]) {
|
||||
pos += needle.len();
|
||||
continue;
|
||||
}
|
||||
let mut i = self.0.critical_pos;
|
||||
while i < needle.len() && needle[i] == haystack[pos + i] {
|
||||
i += 1;
|
||||
}
|
||||
if i < needle.len() {
|
||||
pos += i - self.0.critical_pos + 1;
|
||||
} else {
|
||||
for j in (0..self.0.critical_pos).rev() {
|
||||
if needle[j] != haystack[pos + j] {
|
||||
pos += shift;
|
||||
continue 'outer;
|
||||
}
|
||||
}
|
||||
return Some(pos);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl FinderRev {
|
||||
/// Create a searcher that finds occurrences of the given `needle`.
|
||||
///
|
||||
/// An empty `needle` results in a match at every position in a haystack,
|
||||
/// including at `haystack.len()`.
|
||||
#[inline]
|
||||
pub fn new(needle: &[u8]) -> FinderRev {
|
||||
let byteset = ApproximateByteSet::new(needle);
|
||||
let min_suffix = Suffix::reverse(needle, SuffixKind::Minimal);
|
||||
let max_suffix = Suffix::reverse(needle, SuffixKind::Maximal);
|
||||
let (period_lower_bound, critical_pos) =
|
||||
if min_suffix.pos < max_suffix.pos {
|
||||
(min_suffix.period, min_suffix.pos)
|
||||
} else {
|
||||
(max_suffix.period, max_suffix.pos)
|
||||
};
|
||||
let shift = Shift::reverse(needle, period_lower_bound, critical_pos);
|
||||
FinderRev(TwoWay { byteset, critical_pos, shift })
|
||||
}
|
||||
|
||||
/// Returns the last occurrence of `needle` in the given `haystack`, or
|
||||
/// `None` if no such occurrence could be found.
|
||||
///
|
||||
/// The `needle` given must be the same as the `needle` provided to
|
||||
/// [`FinderRev::new`].
|
||||
///
|
||||
/// An empty `needle` results in a match at every position in a haystack,
|
||||
/// including at `haystack.len()`.
|
||||
#[inline]
|
||||
pub fn rfind(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
|
||||
// For the reverse case, we don't use a prefilter. It's plausible that
|
||||
// perhaps we should, but it's a lot of additional code to do it, and
|
||||
// it's not clear that it's actually worth it. If you have a really
|
||||
// compelling use case for this, please file an issue.
|
||||
match self.0.shift {
|
||||
Shift::Small { period } => {
|
||||
self.rfind_small_imp(haystack, needle, period)
|
||||
}
|
||||
Shift::Large { shift } => {
|
||||
self.rfind_large_imp(haystack, needle, shift)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn rfind_small_imp(
|
||||
&self,
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
period: usize,
|
||||
) -> Option<usize> {
|
||||
let nlen = needle.len();
|
||||
let mut pos = haystack.len();
|
||||
let mut shift = nlen;
|
||||
let first_byte = match needle.first() {
|
||||
None => return Some(pos),
|
||||
Some(&first_byte) => first_byte,
|
||||
};
|
||||
while pos >= nlen {
|
||||
if !self.0.byteset.contains(haystack[pos - nlen]) {
|
||||
pos -= nlen;
|
||||
shift = nlen;
|
||||
continue;
|
||||
}
|
||||
let mut i = cmp::min(self.0.critical_pos, shift);
|
||||
while i > 0 && needle[i - 1] == haystack[pos - nlen + i - 1] {
|
||||
i -= 1;
|
||||
}
|
||||
if i > 0 || first_byte != haystack[pos - nlen] {
|
||||
pos -= self.0.critical_pos - i + 1;
|
||||
shift = nlen;
|
||||
} else {
|
||||
let mut j = self.0.critical_pos;
|
||||
while j < shift && needle[j] == haystack[pos - nlen + j] {
|
||||
j += 1;
|
||||
}
|
||||
if j >= shift {
|
||||
return Some(pos - nlen);
|
||||
}
|
||||
pos -= period;
|
||||
shift = period;
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn rfind_large_imp(
|
||||
&self,
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
shift: usize,
|
||||
) -> Option<usize> {
|
||||
let nlen = needle.len();
|
||||
let mut pos = haystack.len();
|
||||
let first_byte = match needle.first() {
|
||||
None => return Some(pos),
|
||||
Some(&first_byte) => first_byte,
|
||||
};
|
||||
while pos >= nlen {
|
||||
if !self.0.byteset.contains(haystack[pos - nlen]) {
|
||||
pos -= nlen;
|
||||
continue;
|
||||
}
|
||||
let mut i = self.0.critical_pos;
|
||||
while i > 0 && needle[i - 1] == haystack[pos - nlen + i - 1] {
|
||||
i -= 1;
|
||||
}
|
||||
if i > 0 || first_byte != haystack[pos - nlen] {
|
||||
pos -= self.0.critical_pos - i + 1;
|
||||
} else {
|
||||
let mut j = self.0.critical_pos;
|
||||
while j < nlen && needle[j] == haystack[pos - nlen + j] {
|
||||
j += 1;
|
||||
}
|
||||
if j == nlen {
|
||||
return Some(pos - nlen);
|
||||
}
|
||||
pos -= shift;
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// A representation of the amount we're allowed to shift by during Two-Way
|
||||
/// search.
|
||||
///
|
||||
/// When computing a critical factorization of the needle, we find the position
|
||||
/// of the critical factorization by finding the needle's maximal (or minimal)
|
||||
/// suffix, along with the period of that suffix. It turns out that the period
|
||||
/// of that suffix is a lower bound on the period of the needle itself.
|
||||
///
|
||||
/// This lower bound is equivalent to the actual period of the needle in
|
||||
/// some cases. To describe that case, we denote the needle as `x` where
|
||||
/// `x = uv` and `v` is the lexicographic maximal suffix of `v`. The lower
|
||||
/// bound given here is always the period of `v`, which is `<= period(x)`. The
|
||||
/// case where `period(v) == period(x)` occurs when `len(u) < (len(x) / 2)` and
|
||||
/// where `u` is a suffix of `v[0..period(v)]`.
|
||||
///
|
||||
/// This case is important because the search algorithm for when the
|
||||
/// periods are equivalent is slightly different than the search algorithm
|
||||
/// for when the periods are not equivalent. In particular, when they aren't
|
||||
/// equivalent, we know that the period of the needle is no less than half its
|
||||
/// length. In this case, we shift by an amount less than or equal to the
|
||||
/// period of the needle (determined by the maximum length of the components
|
||||
/// of the critical factorization of `x`, i.e., `max(len(u), len(v))`)..
|
||||
///
|
||||
/// The above two cases are represented by the variants below. Each entails
|
||||
/// a different instantiation of the Two-Way search algorithm.
|
||||
///
|
||||
/// N.B. If we could find a way to compute the exact period in all cases,
|
||||
/// then we could collapse this case analysis and simplify the algorithm. The
|
||||
/// Two-Way paper suggests this is possible, but more reading is required to
|
||||
/// grok why the authors didn't pursue that path.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Shift {
|
||||
Small { period: usize },
|
||||
Large { shift: usize },
|
||||
}
|
||||
|
||||
impl Shift {
|
||||
/// Compute the shift for a given needle in the forward direction.
|
||||
///
|
||||
/// This requires a lower bound on the period and a critical position.
|
||||
/// These can be computed by extracting both the minimal and maximal
|
||||
/// lexicographic suffixes, and choosing the right-most starting position.
|
||||
/// The lower bound on the period is then the period of the chosen suffix.
|
||||
fn forward(
|
||||
needle: &[u8],
|
||||
period_lower_bound: usize,
|
||||
critical_pos: usize,
|
||||
) -> Shift {
|
||||
let large = cmp::max(critical_pos, needle.len() - critical_pos);
|
||||
if critical_pos * 2 >= needle.len() {
|
||||
return Shift::Large { shift: large };
|
||||
}
|
||||
|
||||
let (u, v) = needle.split_at(critical_pos);
|
||||
if !is_suffix(&v[..period_lower_bound], u) {
|
||||
return Shift::Large { shift: large };
|
||||
}
|
||||
Shift::Small { period: period_lower_bound }
|
||||
}
|
||||
|
||||
/// Compute the shift for a given needle in the reverse direction.
|
||||
///
|
||||
/// This requires a lower bound on the period and a critical position.
|
||||
/// These can be computed by extracting both the minimal and maximal
|
||||
/// lexicographic suffixes, and choosing the left-most starting position.
|
||||
/// The lower bound on the period is then the period of the chosen suffix.
|
||||
fn reverse(
|
||||
needle: &[u8],
|
||||
period_lower_bound: usize,
|
||||
critical_pos: usize,
|
||||
) -> Shift {
|
||||
let large = cmp::max(critical_pos, needle.len() - critical_pos);
|
||||
if (needle.len() - critical_pos) * 2 >= needle.len() {
|
||||
return Shift::Large { shift: large };
|
||||
}
|
||||
|
||||
let (v, u) = needle.split_at(critical_pos);
|
||||
if !is_prefix(&v[v.len() - period_lower_bound..], u) {
|
||||
return Shift::Large { shift: large };
|
||||
}
|
||||
Shift::Small { period: period_lower_bound }
|
||||
}
|
||||
}
|
||||
|
||||
/// A suffix extracted from a needle along with its period.
|
||||
#[derive(Debug)]
|
||||
struct Suffix {
|
||||
/// The starting position of this suffix.
|
||||
///
|
||||
/// If this is a forward suffix, then `&bytes[pos..]` can be used. If this
|
||||
/// is a reverse suffix, then `&bytes[..pos]` can be used. That is, for
|
||||
/// forward suffixes, this is an inclusive starting position, where as for
|
||||
/// reverse suffixes, this is an exclusive ending position.
|
||||
pos: usize,
|
||||
/// The period of this suffix.
|
||||
///
|
||||
/// Note that this is NOT necessarily the period of the string from which
|
||||
/// this suffix comes from. (It is always less than or equal to the period
|
||||
/// of the original string.)
|
||||
period: usize,
|
||||
}
|
||||
|
||||
impl Suffix {
|
||||
fn forward(needle: &[u8], kind: SuffixKind) -> Suffix {
|
||||
// suffix represents our maximal (or minimal) suffix, along with
|
||||
// its period.
|
||||
let mut suffix = Suffix { pos: 0, period: 1 };
|
||||
// The start of a suffix in `needle` that we are considering as a
|
||||
// more maximal (or minimal) suffix than what's in `suffix`.
|
||||
let mut candidate_start = 1;
|
||||
// The current offset of our suffixes that we're comparing.
|
||||
//
|
||||
// When the characters at this offset are the same, then we mush on
|
||||
// to the next position since no decision is possible. When the
|
||||
// candidate's character is greater (or lesser) than the corresponding
|
||||
// character than our current maximal (or minimal) suffix, then the
|
||||
// current suffix is changed over to the candidate and we restart our
|
||||
// search. Otherwise, the candidate suffix is no good and we restart
|
||||
// our search on the next candidate.
|
||||
//
|
||||
// The three cases above correspond to the three cases in the loop
|
||||
// below.
|
||||
let mut offset = 0;
|
||||
|
||||
while candidate_start + offset < needle.len() {
|
||||
let current = needle[suffix.pos + offset];
|
||||
let candidate = needle[candidate_start + offset];
|
||||
match kind.cmp(current, candidate) {
|
||||
SuffixOrdering::Accept => {
|
||||
suffix = Suffix { pos: candidate_start, period: 1 };
|
||||
candidate_start += 1;
|
||||
offset = 0;
|
||||
}
|
||||
SuffixOrdering::Skip => {
|
||||
candidate_start += offset + 1;
|
||||
offset = 0;
|
||||
suffix.period = candidate_start - suffix.pos;
|
||||
}
|
||||
SuffixOrdering::Push => {
|
||||
if offset + 1 == suffix.period {
|
||||
candidate_start += suffix.period;
|
||||
offset = 0;
|
||||
} else {
|
||||
offset += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
suffix
|
||||
}
|
||||
|
||||
fn reverse(needle: &[u8], kind: SuffixKind) -> Suffix {
|
||||
// See the comments in `forward` for how this works.
|
||||
let mut suffix = Suffix { pos: needle.len(), period: 1 };
|
||||
if needle.len() == 1 {
|
||||
return suffix;
|
||||
}
|
||||
let mut candidate_start = match needle.len().checked_sub(1) {
|
||||
None => return suffix,
|
||||
Some(candidate_start) => candidate_start,
|
||||
};
|
||||
let mut offset = 0;
|
||||
|
||||
while offset < candidate_start {
|
||||
let current = needle[suffix.pos - offset - 1];
|
||||
let candidate = needle[candidate_start - offset - 1];
|
||||
match kind.cmp(current, candidate) {
|
||||
SuffixOrdering::Accept => {
|
||||
suffix = Suffix { pos: candidate_start, period: 1 };
|
||||
candidate_start -= 1;
|
||||
offset = 0;
|
||||
}
|
||||
SuffixOrdering::Skip => {
|
||||
candidate_start -= offset + 1;
|
||||
offset = 0;
|
||||
suffix.period = suffix.pos - candidate_start;
|
||||
}
|
||||
SuffixOrdering::Push => {
|
||||
if offset + 1 == suffix.period {
|
||||
candidate_start -= suffix.period;
|
||||
offset = 0;
|
||||
} else {
|
||||
offset += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
suffix
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of suffix to extract.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum SuffixKind {
|
||||
/// Extract the smallest lexicographic suffix from a string.
|
||||
///
|
||||
/// Technically, this doesn't actually pick the smallest lexicographic
|
||||
/// suffix. e.g., Given the choice between `a` and `aa`, this will choose
|
||||
/// the latter over the former, even though `a < aa`. The reasoning for
|
||||
/// this isn't clear from the paper, but it still smells like a minimal
|
||||
/// suffix.
|
||||
Minimal,
|
||||
/// Extract the largest lexicographic suffix from a string.
|
||||
///
|
||||
/// Unlike `Minimal`, this really does pick the maximum suffix. e.g., Given
|
||||
/// the choice between `z` and `zz`, this will choose the latter over the
|
||||
/// former.
|
||||
Maximal,
|
||||
}
|
||||
|
||||
/// The result of comparing corresponding bytes between two suffixes.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum SuffixOrdering {
|
||||
/// This occurs when the given candidate byte indicates that the candidate
|
||||
/// suffix is better than the current maximal (or minimal) suffix. That is,
|
||||
/// the current candidate suffix should supplant the current maximal (or
|
||||
/// minimal) suffix.
|
||||
Accept,
|
||||
/// This occurs when the given candidate byte excludes the candidate suffix
|
||||
/// from being better than the current maximal (or minimal) suffix. That
|
||||
/// is, the current candidate suffix should be dropped and the next one
|
||||
/// should be considered.
|
||||
Skip,
|
||||
/// This occurs when no decision to accept or skip the candidate suffix
|
||||
/// can be made, e.g., when corresponding bytes are equivalent. In this
|
||||
/// case, the next corresponding bytes should be compared.
|
||||
Push,
|
||||
}
|
||||
|
||||
impl SuffixKind {
|
||||
/// Returns true if and only if the given candidate byte indicates that
|
||||
/// it should replace the current suffix as the maximal (or minimal)
|
||||
/// suffix.
|
||||
fn cmp(self, current: u8, candidate: u8) -> SuffixOrdering {
|
||||
use self::SuffixOrdering::*;
|
||||
|
||||
match self {
|
||||
SuffixKind::Minimal if candidate < current => Accept,
|
||||
SuffixKind::Minimal if candidate > current => Skip,
|
||||
SuffixKind::Minimal => Push,
|
||||
SuffixKind::Maximal if candidate > current => Accept,
|
||||
SuffixKind::Maximal if candidate < current => Skip,
|
||||
SuffixKind::Maximal => Push,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A bitset used to track whether a particular byte exists in a needle or not.
|
||||
///
|
||||
/// Namely, bit 'i' is set if and only if byte%64==i for any byte in the
|
||||
/// needle. If a particular byte in the haystack is NOT in this set, then one
|
||||
/// can conclude that it is also not in the needle, and thus, one can advance
|
||||
/// in the haystack by needle.len() bytes.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct ApproximateByteSet(u64);
|
||||
|
||||
impl ApproximateByteSet {
|
||||
/// Create a new set from the given needle.
|
||||
fn new(needle: &[u8]) -> ApproximateByteSet {
|
||||
let mut bits = 0;
|
||||
for &b in needle {
|
||||
bits |= 1 << (b % 64);
|
||||
}
|
||||
ApproximateByteSet(bits)
|
||||
}
|
||||
|
||||
/// Return true if and only if the given byte might be in this set. This
|
||||
/// may return a false positive, but will never return a false negative.
|
||||
#[inline(always)]
|
||||
fn contains(&self, byte: u8) -> bool {
|
||||
self.0 & (1 << (byte % 64)) != 0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Convenience wrapper for computing the suffix as a byte string.
|
||||
fn get_suffix_forward(needle: &[u8], kind: SuffixKind) -> (&[u8], usize) {
|
||||
let s = Suffix::forward(needle, kind);
|
||||
(&needle[s.pos..], s.period)
|
||||
}
|
||||
|
||||
/// Convenience wrapper for computing the reverse suffix as a byte string.
|
||||
fn get_suffix_reverse(needle: &[u8], kind: SuffixKind) -> (&[u8], usize) {
|
||||
let s = Suffix::reverse(needle, kind);
|
||||
(&needle[..s.pos], s.period)
|
||||
}
|
||||
|
||||
/// Return all of the non-empty suffixes in the given byte string.
|
||||
fn suffixes(bytes: &[u8]) -> Vec<&[u8]> {
|
||||
(0..bytes.len()).map(|i| &bytes[i..]).collect()
|
||||
}
|
||||
|
||||
/// Return the lexicographically maximal suffix of the given byte string.
|
||||
fn naive_maximal_suffix_forward(needle: &[u8]) -> &[u8] {
|
||||
let mut sufs = suffixes(needle);
|
||||
sufs.sort();
|
||||
sufs.pop().unwrap()
|
||||
}
|
||||
|
||||
/// Return the lexicographically maximal suffix of the reverse of the given
|
||||
/// byte string.
|
||||
fn naive_maximal_suffix_reverse(needle: &[u8]) -> Vec<u8> {
|
||||
let mut reversed = needle.to_vec();
|
||||
reversed.reverse();
|
||||
let mut got = naive_maximal_suffix_forward(&reversed).to_vec();
|
||||
got.reverse();
|
||||
got
|
||||
}
|
||||
|
||||
define_substring_forward_quickcheck!(|h, n| Some(
|
||||
Finder::new(n).find(h, n)
|
||||
));
|
||||
define_substring_reverse_quickcheck!(|h, n| Some(
|
||||
FinderRev::new(n).rfind(h, n)
|
||||
));
|
||||
|
||||
#[test]
|
||||
fn forward() {
|
||||
crate::tests::substring::Runner::new()
|
||||
.fwd(|h, n| Some(Finder::new(n).find(h, n)))
|
||||
.run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reverse() {
|
||||
crate::tests::substring::Runner::new()
|
||||
.rev(|h, n| Some(FinderRev::new(n).rfind(h, n)))
|
||||
.run();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suffix_forward() {
|
||||
macro_rules! assert_suffix_min {
|
||||
($given:expr, $expected:expr, $period:expr) => {
|
||||
let (got_suffix, got_period) =
|
||||
get_suffix_forward($given.as_bytes(), SuffixKind::Minimal);
|
||||
let got_suffix = core::str::from_utf8(got_suffix).unwrap();
|
||||
assert_eq!(($expected, $period), (got_suffix, got_period));
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! assert_suffix_max {
|
||||
($given:expr, $expected:expr, $period:expr) => {
|
||||
let (got_suffix, got_period) =
|
||||
get_suffix_forward($given.as_bytes(), SuffixKind::Maximal);
|
||||
let got_suffix = core::str::from_utf8(got_suffix).unwrap();
|
||||
assert_eq!(($expected, $period), (got_suffix, got_period));
|
||||
};
|
||||
}
|
||||
|
||||
assert_suffix_min!("a", "a", 1);
|
||||
assert_suffix_max!("a", "a", 1);
|
||||
|
||||
assert_suffix_min!("ab", "ab", 2);
|
||||
assert_suffix_max!("ab", "b", 1);
|
||||
|
||||
assert_suffix_min!("ba", "a", 1);
|
||||
assert_suffix_max!("ba", "ba", 2);
|
||||
|
||||
assert_suffix_min!("abc", "abc", 3);
|
||||
assert_suffix_max!("abc", "c", 1);
|
||||
|
||||
assert_suffix_min!("acb", "acb", 3);
|
||||
assert_suffix_max!("acb", "cb", 2);
|
||||
|
||||
assert_suffix_min!("cba", "a", 1);
|
||||
assert_suffix_max!("cba", "cba", 3);
|
||||
|
||||
assert_suffix_min!("abcabc", "abcabc", 3);
|
||||
assert_suffix_max!("abcabc", "cabc", 3);
|
||||
|
||||
assert_suffix_min!("abcabcabc", "abcabcabc", 3);
|
||||
assert_suffix_max!("abcabcabc", "cabcabc", 3);
|
||||
|
||||
assert_suffix_min!("abczz", "abczz", 5);
|
||||
assert_suffix_max!("abczz", "zz", 1);
|
||||
|
||||
assert_suffix_min!("zzabc", "abc", 3);
|
||||
assert_suffix_max!("zzabc", "zzabc", 5);
|
||||
|
||||
assert_suffix_min!("aaa", "aaa", 1);
|
||||
assert_suffix_max!("aaa", "aaa", 1);
|
||||
|
||||
assert_suffix_min!("foobar", "ar", 2);
|
||||
assert_suffix_max!("foobar", "r", 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suffix_reverse() {
|
||||
macro_rules! assert_suffix_min {
|
||||
($given:expr, $expected:expr, $period:expr) => {
|
||||
let (got_suffix, got_period) =
|
||||
get_suffix_reverse($given.as_bytes(), SuffixKind::Minimal);
|
||||
let got_suffix = core::str::from_utf8(got_suffix).unwrap();
|
||||
assert_eq!(($expected, $period), (got_suffix, got_period));
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! assert_suffix_max {
|
||||
($given:expr, $expected:expr, $period:expr) => {
|
||||
let (got_suffix, got_period) =
|
||||
get_suffix_reverse($given.as_bytes(), SuffixKind::Maximal);
|
||||
let got_suffix = core::str::from_utf8(got_suffix).unwrap();
|
||||
assert_eq!(($expected, $period), (got_suffix, got_period));
|
||||
};
|
||||
}
|
||||
|
||||
assert_suffix_min!("a", "a", 1);
|
||||
assert_suffix_max!("a", "a", 1);
|
||||
|
||||
assert_suffix_min!("ab", "a", 1);
|
||||
assert_suffix_max!("ab", "ab", 2);
|
||||
|
||||
assert_suffix_min!("ba", "ba", 2);
|
||||
assert_suffix_max!("ba", "b", 1);
|
||||
|
||||
assert_suffix_min!("abc", "a", 1);
|
||||
assert_suffix_max!("abc", "abc", 3);
|
||||
|
||||
assert_suffix_min!("acb", "a", 1);
|
||||
assert_suffix_max!("acb", "ac", 2);
|
||||
|
||||
assert_suffix_min!("cba", "cba", 3);
|
||||
assert_suffix_max!("cba", "c", 1);
|
||||
|
||||
assert_suffix_min!("abcabc", "abca", 3);
|
||||
assert_suffix_max!("abcabc", "abcabc", 3);
|
||||
|
||||
assert_suffix_min!("abcabcabc", "abcabca", 3);
|
||||
assert_suffix_max!("abcabcabc", "abcabcabc", 3);
|
||||
|
||||
assert_suffix_min!("abczz", "a", 1);
|
||||
assert_suffix_max!("abczz", "abczz", 5);
|
||||
|
||||
assert_suffix_min!("zzabc", "zza", 3);
|
||||
assert_suffix_max!("zzabc", "zz", 1);
|
||||
|
||||
assert_suffix_min!("aaa", "aaa", 1);
|
||||
assert_suffix_max!("aaa", "aaa", 1);
|
||||
}
|
||||
|
||||
#[cfg(not(miri))]
|
||||
quickcheck::quickcheck! {
|
||||
fn qc_suffix_forward_maximal(bytes: Vec<u8>) -> bool {
|
||||
if bytes.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
let (got, _) = get_suffix_forward(&bytes, SuffixKind::Maximal);
|
||||
let expected = naive_maximal_suffix_forward(&bytes);
|
||||
got == expected
|
||||
}
|
||||
|
||||
fn qc_suffix_reverse_maximal(bytes: Vec<u8>) -> bool {
|
||||
if bytes.is_empty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
let (got, _) = get_suffix_reverse(&bytes, SuffixKind::Maximal);
|
||||
let expected = naive_maximal_suffix_reverse(&bytes);
|
||||
expected == got
|
||||
}
|
||||
}
|
||||
|
||||
// This is a regression test caught by quickcheck that exercised a bug in
|
||||
// the reverse small period handling. The bug was that we were using 'if j
|
||||
// == shift' to determine if a match occurred, but the correct guard is 'if
|
||||
// j >= shift', which matches the corresponding guard in the forward impl.
|
||||
#[test]
|
||||
fn regression_rev_small_period() {
|
||||
let rfind = |h, n| FinderRev::new(n).rfind(h, n);
|
||||
let haystack = "ababaz";
|
||||
let needle = "abab";
|
||||
assert_eq!(Some(0), rfind(haystack.as_bytes(), needle.as_bytes()));
|
||||
}
|
||||
}
|
||||
+1220
File diff suppressed because it is too large
Load Diff
+14
@@ -0,0 +1,14 @@
|
||||
/*!
|
||||
This module defines "generic" routines that can be specialized to specific
|
||||
architectures.
|
||||
|
||||
We don't expose this module primarily because it would require exposing all
|
||||
of the internal infrastructure required to write these generic routines.
|
||||
That infrastructure should be treated as an implementation detail so that
|
||||
it is allowed to evolve. Instead, what we expose are architecture specific
|
||||
instantiations of these generic implementations. The generic code just lets us
|
||||
write the code once (usually).
|
||||
*/
|
||||
|
||||
pub(crate) mod memchr;
|
||||
pub(crate) mod packedpair;
|
||||
+317
@@ -0,0 +1,317 @@
|
||||
/*!
|
||||
Generic crate-internal routines for the "packed pair" SIMD algorithm.
|
||||
|
||||
The "packed pair" algorithm is based on the [generic SIMD] algorithm. The main
|
||||
difference is that it (by default) uses a background distribution of byte
|
||||
frequencies to heuristically select the pair of bytes to search for.
|
||||
|
||||
[generic SIMD]: http://0x80.pl/articles/simd-strfind.html#first-and-last
|
||||
*/
|
||||
|
||||
use crate::{
|
||||
arch::all::{is_equal_raw, packedpair::Pair},
|
||||
ext::Pointer,
|
||||
vector::{MoveMask, Vector},
|
||||
};
|
||||
|
||||
/// A generic architecture dependent "packed pair" finder.
|
||||
///
|
||||
/// This finder picks two bytes that it believes have high predictive power
|
||||
/// for indicating an overall match of a needle. Depending on whether
|
||||
/// `Finder::find` or `Finder::find_prefilter` is used, it reports offsets
|
||||
/// where the needle matches or could match. In the prefilter case, candidates
|
||||
/// are reported whenever the [`Pair`] of bytes given matches.
|
||||
///
|
||||
/// This is architecture dependent because it uses specific vector operations
|
||||
/// to look for occurrences of the pair of bytes.
|
||||
///
|
||||
/// This type is not meant to be exported and is instead meant to be used as
|
||||
/// the implementation for architecture specific facades. Why? Because it's a
|
||||
/// bit of a quirky API that requires `inline(always)` annotations. And pretty
|
||||
/// much everything has safety obligations due (at least) to the caller needing
|
||||
/// to inline calls into routines marked with
|
||||
/// `#[target_feature(enable = "...")]`.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct Finder<V> {
|
||||
pair: Pair,
|
||||
v1: V,
|
||||
v2: V,
|
||||
min_haystack_len: usize,
|
||||
}
|
||||
|
||||
impl<V: Vector> Finder<V> {
|
||||
/// Create a new pair searcher. The searcher returned can either report
|
||||
/// exact matches of `needle` or act as a prefilter and report candidate
|
||||
/// positions of `needle`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Callers must ensure that whatever vector type this routine is called
|
||||
/// with is supported by the current environment.
|
||||
///
|
||||
/// Callers must also ensure that `needle.len() >= 2`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn new(needle: &[u8], pair: Pair) -> Finder<V> {
|
||||
let max_index = pair.index1().max(pair.index2());
|
||||
let min_haystack_len =
|
||||
core::cmp::max(needle.len(), usize::from(max_index) + V::BYTES);
|
||||
let v1 = V::splat(needle[usize::from(pair.index1())]);
|
||||
let v2 = V::splat(needle[usize::from(pair.index2())]);
|
||||
Finder { pair, v1, v2, min_haystack_len }
|
||||
}
|
||||
|
||||
/// Searches the given haystack for the given needle. The needle given
|
||||
/// should be the same as the needle that this finder was initialized
|
||||
/// with.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When `haystack.len()` is less than [`Finder::min_haystack_len`].
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Since this is meant to be used with vector functions, callers need to
|
||||
/// specialize this inside of a function with a `target_feature` attribute.
|
||||
/// Therefore, callers must ensure that whatever target feature is being
|
||||
/// used supports the vector functions that this function is specialized
|
||||
/// for. (For the specific vector functions used, see the Vector trait
|
||||
/// implementations.)
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn find(
|
||||
&self,
|
||||
haystack: &[u8],
|
||||
needle: &[u8],
|
||||
) -> Option<usize> {
|
||||
assert!(
|
||||
haystack.len() >= self.min_haystack_len,
|
||||
"haystack too small, should be at least {} but got {}",
|
||||
self.min_haystack_len,
|
||||
haystack.len(),
|
||||
);
|
||||
|
||||
let all = V::Mask::all_zeros_except_least_significant(0);
|
||||
let start = haystack.as_ptr();
|
||||
let end = start.add(haystack.len());
|
||||
let max = end.sub(self.min_haystack_len);
|
||||
let mut cur = start;
|
||||
|
||||
// N.B. I did experiment with unrolling the loop to deal with size(V)
|
||||
// bytes at a time and 2*size(V) bytes at a time. The double unroll
|
||||
// was marginally faster while the quadruple unroll was unambiguously
|
||||
// slower. In the end, I decided the complexity from unrolling wasn't
|
||||
// worth it. I used the memmem/krate/prebuilt/huge-en/ benchmarks to
|
||||
// compare.
|
||||
while cur <= max {
|
||||
if let Some(chunki) = self.find_in_chunk(needle, cur, end, all) {
|
||||
return Some(matched(start, cur, chunki));
|
||||
}
|
||||
cur = cur.add(V::BYTES);
|
||||
}
|
||||
if cur < end {
|
||||
let remaining = end.distance(cur);
|
||||
debug_assert!(
|
||||
remaining < self.min_haystack_len,
|
||||
"remaining bytes should be smaller than the minimum haystack \
|
||||
length of {}, but there are {} bytes remaining",
|
||||
self.min_haystack_len,
|
||||
remaining,
|
||||
);
|
||||
if remaining < needle.len() {
|
||||
return None;
|
||||
}
|
||||
debug_assert!(
|
||||
max < cur,
|
||||
"after main loop, cur should have exceeded max",
|
||||
);
|
||||
let overlap = cur.distance(max);
|
||||
debug_assert!(
|
||||
overlap > 0,
|
||||
"overlap ({}) must always be non-zero",
|
||||
overlap,
|
||||
);
|
||||
debug_assert!(
|
||||
overlap < V::BYTES,
|
||||
"overlap ({}) cannot possibly be >= than a vector ({})",
|
||||
overlap,
|
||||
V::BYTES,
|
||||
);
|
||||
// The mask has all of its bits set except for the first N least
|
||||
// significant bits, where N=overlap. This way, any matches that
|
||||
// occur in find_in_chunk within the overlap are automatically
|
||||
// ignored.
|
||||
let mask = V::Mask::all_zeros_except_least_significant(overlap);
|
||||
cur = max;
|
||||
let m = self.find_in_chunk(needle, cur, end, mask);
|
||||
if let Some(chunki) = m {
|
||||
return Some(matched(start, cur, chunki));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Searches the given haystack for offsets that represent candidate
|
||||
/// matches of the `needle` given to this finder's constructor. The offsets
|
||||
/// returned, if they are a match, correspond to the starting offset of
|
||||
/// `needle` in the given `haystack`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// When `haystack.len()` is less than [`Finder::min_haystack_len`].
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Since this is meant to be used with vector functions, callers need to
|
||||
/// specialize this inside of a function with a `target_feature` attribute.
|
||||
/// Therefore, callers must ensure that whatever target feature is being
|
||||
/// used supports the vector functions that this function is specialized
|
||||
/// for. (For the specific vector functions used, see the Vector trait
|
||||
/// implementations.)
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn find_prefilter(
|
||||
&self,
|
||||
haystack: &[u8],
|
||||
) -> Option<usize> {
|
||||
assert!(
|
||||
haystack.len() >= self.min_haystack_len,
|
||||
"haystack too small, should be at least {} but got {}",
|
||||
self.min_haystack_len,
|
||||
haystack.len(),
|
||||
);
|
||||
|
||||
let start = haystack.as_ptr();
|
||||
let end = start.add(haystack.len());
|
||||
let max = end.sub(self.min_haystack_len);
|
||||
let mut cur = start;
|
||||
|
||||
// N.B. I did experiment with unrolling the loop to deal with size(V)
|
||||
// bytes at a time and 2*size(V) bytes at a time. The double unroll
|
||||
// was marginally faster while the quadruple unroll was unambiguously
|
||||
// slower. In the end, I decided the complexity from unrolling wasn't
|
||||
// worth it. I used the memmem/krate/prebuilt/huge-en/ benchmarks to
|
||||
// compare.
|
||||
while cur <= max {
|
||||
if let Some(chunki) = self.find_prefilter_in_chunk(cur) {
|
||||
return Some(matched(start, cur, chunki));
|
||||
}
|
||||
cur = cur.add(V::BYTES);
|
||||
}
|
||||
if cur < end {
|
||||
// This routine immediately quits if a candidate match is found.
|
||||
// That means that if we're here, no candidate matches have been
|
||||
// found at or before 'ptr'. Thus, we don't need to mask anything
|
||||
// out even though we might technically search part of the haystack
|
||||
// that we've already searched (because we know it can't match).
|
||||
cur = max;
|
||||
if let Some(chunki) = self.find_prefilter_in_chunk(cur) {
|
||||
return Some(matched(start, cur, chunki));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Search for an occurrence of our byte pair from the needle in the chunk
|
||||
/// pointed to by cur, with the end of the haystack pointed to by end.
|
||||
/// When an occurrence is found, memcmp is run to check if a match occurs
|
||||
/// at the corresponding position.
|
||||
///
|
||||
/// `mask` should have bits set corresponding the positions in the chunk
|
||||
/// in which matches are considered. This is only used for the last vector
|
||||
/// load where the beginning of the vector might have overlapped with the
|
||||
/// last load in the main loop. The mask lets us avoid visiting positions
|
||||
/// that have already been discarded as matches.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// It must be safe to do an unaligned read of size(V) bytes starting at
|
||||
/// both (cur + self.index1) and (cur + self.index2). It must also be safe
|
||||
/// to do unaligned loads on cur up to (end - needle.len()).
|
||||
#[inline(always)]
|
||||
unsafe fn find_in_chunk(
|
||||
&self,
|
||||
needle: &[u8],
|
||||
cur: *const u8,
|
||||
end: *const u8,
|
||||
mask: V::Mask,
|
||||
) -> Option<usize> {
|
||||
let index1 = usize::from(self.pair.index1());
|
||||
let index2 = usize::from(self.pair.index2());
|
||||
let chunk1 = V::load_unaligned(cur.add(index1));
|
||||
let chunk2 = V::load_unaligned(cur.add(index2));
|
||||
let eq1 = chunk1.cmpeq(self.v1);
|
||||
let eq2 = chunk2.cmpeq(self.v2);
|
||||
|
||||
let mut offsets = eq1.and(eq2).movemask().and(mask);
|
||||
while offsets.has_non_zero() {
|
||||
let offset = offsets.first_offset();
|
||||
let cur = cur.add(offset);
|
||||
if end.distance(cur) < needle.len() {
|
||||
return None;
|
||||
}
|
||||
if is_equal_raw(needle.as_ptr(), cur, needle.len()) {
|
||||
return Some(offset);
|
||||
}
|
||||
offsets = offsets.clear_least_significant_bit();
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Search for an occurrence of our byte pair from the needle in the chunk
|
||||
/// pointed to by cur, with the end of the haystack pointed to by end.
|
||||
/// When an occurrence is found, memcmp is run to check if a match occurs
|
||||
/// at the corresponding position.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// It must be safe to do an unaligned read of size(V) bytes starting at
|
||||
/// both (cur + self.index1) and (cur + self.index2). It must also be safe
|
||||
/// to do unaligned reads on cur up to (end - needle.len()).
|
||||
#[inline(always)]
|
||||
unsafe fn find_prefilter_in_chunk(&self, cur: *const u8) -> Option<usize> {
|
||||
let index1 = usize::from(self.pair.index1());
|
||||
let index2 = usize::from(self.pair.index2());
|
||||
let chunk1 = V::load_unaligned(cur.add(index1));
|
||||
let chunk2 = V::load_unaligned(cur.add(index2));
|
||||
let eq1 = chunk1.cmpeq(self.v1);
|
||||
let eq2 = chunk2.cmpeq(self.v2);
|
||||
|
||||
let offsets = eq1.and(eq2).movemask();
|
||||
if !offsets.has_non_zero() {
|
||||
return None;
|
||||
}
|
||||
Some(offsets.first_offset())
|
||||
}
|
||||
|
||||
/// Returns the pair of offsets (into the needle) used to check as a
|
||||
/// predicate before confirming whether a needle exists at a particular
|
||||
/// position.
|
||||
#[inline]
|
||||
pub(crate) fn pair(&self) -> &Pair {
|
||||
&self.pair
|
||||
}
|
||||
|
||||
/// Returns the minimum haystack length that this `Finder` can search.
|
||||
///
|
||||
/// Providing a haystack to this `Finder` shorter than this length is
|
||||
/// guaranteed to result in a panic.
|
||||
#[inline(always)]
|
||||
pub(crate) fn min_haystack_len(&self) -> usize {
|
||||
self.min_haystack_len
|
||||
}
|
||||
}
|
||||
|
||||
/// Accepts a chunk-relative offset and returns a haystack relative offset.
|
||||
///
|
||||
/// This used to be marked `#[cold]` and `#[inline(never)]`, but I couldn't
|
||||
/// observe a consistent measureable difference between that and just inlining
|
||||
/// it. So we go with inlining it.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Same at `ptr::offset_from` in addition to `cur >= start`.
|
||||
#[inline(always)]
|
||||
unsafe fn matched(start: *const u8, cur: *const u8, chunki: usize) -> usize {
|
||||
cur.distance(start) + chunki
|
||||
}
|
||||
|
||||
// If you're looking for tests, those are run for each instantiation of the
|
||||
// above code. So for example, see arch::x86_64::sse2::packedpair.
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
/*!
|
||||
A module with low-level architecture dependent routines.
|
||||
|
||||
These routines are useful as primitives for tasks not covered by the higher
|
||||
level crate API.
|
||||
*/
|
||||
|
||||
pub mod all;
|
||||
pub(crate) mod generic;
|
||||
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
pub mod aarch64;
|
||||
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
|
||||
pub mod wasm32;
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
pub mod x86_64;
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
/*!
|
||||
Wrapper routines for `memchr` and friends.
|
||||
|
||||
These routines choose the best implementation at compile time. (This is
|
||||
different from `x86_64` because it is expected that `simd128` is almost always
|
||||
available for `wasm32` targets.)
|
||||
*/
|
||||
|
||||
macro_rules! defraw {
|
||||
($ty:ident, $find:ident, $start:ident, $end:ident, $($needles:ident),+) => {{
|
||||
use crate::arch::wasm32::simd128::memchr::$ty;
|
||||
|
||||
debug!("chose simd128 for {}", stringify!($ty));
|
||||
debug_assert!($ty::is_available());
|
||||
// SAFETY: We know that wasm memchr is always available whenever
|
||||
// code is compiled for `wasm32` with the `simd128` target feature
|
||||
// enabled.
|
||||
$ty::new_unchecked($($needles),+).$find($start, $end)
|
||||
}}
|
||||
}
|
||||
|
||||
/// memchr, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `One::find_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memchr_raw(
|
||||
n1: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(One, find_raw, start, end, n1)
|
||||
}
|
||||
|
||||
/// memrchr, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `One::rfind_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memrchr_raw(
|
||||
n1: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(One, rfind_raw, start, end, n1)
|
||||
}
|
||||
|
||||
/// memchr2, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Two::find_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memchr2_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Two, find_raw, start, end, n1, n2)
|
||||
}
|
||||
|
||||
/// memrchr2, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Two::rfind_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memrchr2_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Two, rfind_raw, start, end, n1, n2)
|
||||
}
|
||||
|
||||
/// memchr3, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Three::find_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memchr3_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
n3: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Three, find_raw, start, end, n1, n2, n3)
|
||||
}
|
||||
|
||||
/// memrchr3, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `Three::rfind_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn memrchr3_raw(
|
||||
n1: u8,
|
||||
n2: u8,
|
||||
n3: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> Option<*const u8> {
|
||||
defraw!(Three, rfind_raw, start, end, n1, n2, n3)
|
||||
}
|
||||
|
||||
/// Count all matching bytes, but using raw pointers to represent the haystack.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Pointers must be valid. See `One::count_raw`.
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn count_raw(
|
||||
n1: u8,
|
||||
start: *const u8,
|
||||
end: *const u8,
|
||||
) -> usize {
|
||||
defraw!(One, count_raw, start, end, n1)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user