Skip to content

Commit

Permalink
CHIA-193: Rust bindings to chiapos (#439)
Browse files Browse the repository at this point in the history
Adds a Rust crate with a memory safe (at least, according to fuzz tests
and unit tests I've written) binding to `validate_proof`. This is part
of an effort to port block prevalidation to Rust.
  • Loading branch information
Rigidity authored Aug 15, 2024
2 parents e5eeb6e + e81f6f5 commit 84a28b7
Show file tree
Hide file tree
Showing 18 changed files with 1,034 additions and 4 deletions.
89 changes: 89 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Rust bindings

on:
push:
branches:
- main
release:
types: [published]
pull_request:
branches:
- "**"

permissions:
id-token: write
contents: read

jobs:
fuzz_targets:
name: Run fuzzers
runs-on: ubuntu-latest
env:
CARGO_PROFILE_RELEASE_LTO: false
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly

- name: Install cargo-fuzz
run: cargo +nightly install cargo-fuzz

- name: Cargo fuzz
run: |
cd rust-bindings
cargo fuzz list | xargs -I "%" sh -c "cargo +nightly fuzz run % -- -max_total_time=600 || exit 255"
build_crate:
name: Build crate
runs-on: ubuntu-latest
strategy:
fail-fast: false

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy

- name: Rustfmt
run: cargo fmt -- --files-with-diff --check

- name: Clippy
run: cargo clippy

- name: Tests
run: cargo test && cargo test --release

- name: Build
run: cargo build --release

- name: Prepare for publish
run: |
mkdir rust-bindings/cpp
cp -r src lib tests uint128_t python-bindings c-bindings CMakeLists.txt rust-bindings/cpp
- name: Publish to crates.io (dry run)
# We use `--allow-dirty` because the `cpp` folder is copied into the working directory.
# This is necessary because the `cpp` folder is not part of the crate otherwise.
run: cargo publish --dry-run -p chiapos --allow-dirty

- name: Upload crate artifacts
uses: actions/upload-artifact@v4
with:
name: crate
path: ./target/package/*-*.crate

- name: Set Env
uses: Chia-Network/actions/setjobenv@main
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to crates.io
if: env.RELEASE == 'true'
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.cargo_registry_token }}
# See comment above for why `--allow-dirty` is used.
run: cargo publish -p chiapos --allow-dirty
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ build-*
cmake-build*
*.zip
*.tar.gz
libs/
libs/
target/
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,15 @@ include_directories(
option(BUILD_PROOF_OF_SPACE_STATICALLY "Build ProofOfSpace target statically" OFF)
IF (BUILD_PROOF_OF_SPACE_STATICALLY)
message("Statically build ProofOfSpace")
target_link_libraries(ProofOfSpace -static -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
target_link_libraries(ProofOfSpace PUBLIC -static -Wl,--whole-archive -lrt -lpthread -Wl,--no-whole-archive)
ENDIF()

option(BUILD_STATIC_CHIAPOS_LIBRARY "Build chiapos static library (verify-only)" OFF)
IF (BUILD_STATIC_CHIAPOS_LIBRARY)
message("Build chiapos static library (verify-only)")
add_library(chiapos_static STATIC src/chacha8.c c-bindings/wrapper.cpp)
target_link_libraries(chiapos_static PUBLIC blake3)
target_include_directories(chiapos_static PUBLIC lib/include)
ENDIF()

FetchContent_Declare(
Expand Down
Loading

0 comments on commit 84a28b7

Please sign in to comment.