# Automatically generated by fuzz/generate-files.sh
name: Fuzz
on:
  schedule:
    # 5am every day UTC, this correlates to:
    # - 10pm PDT
    # - 6am CET
    # - 4pm AEDT
    - cron: '00 05 * * *'

jobs:
  fuzz:
    if: ${{ !github.event.act }}
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        # We only get 20 jobs at a time, we probably don't want to go
        # over that limit with fuzzing because of the hour run time.
        fuzz_target: [
          bitcoin_deserialize_address,
          bitcoin_deserialize_block,
          bitcoin_deserialize_prefilled_transaction,
          bitcoin_deserialize_psbt,
          bitcoin_deserialize_script,
          bitcoin_deserialize_transaction,
          bitcoin_deserialize_witness,
          bitcoin_deser_net_msg,
          bitcoin_outpoint_string,
          bitcoin_script_bytes_to_asm_fmt,
          hashes_json,
          hashes_ripemd160,
          hashes_sha1,
          hashes_sha256,
          hashes_sha512_256,
          hashes_sha512,
          units_deserialize_amount,
        ]
    steps:
      - name: Install test dependencies
        run: sudo apt-get update -y && sudo apt-get install -y binutils-dev libunwind8-dev libcurl4-openssl-dev libelf-dev libdw-dev cmake gcc libiberty-dev
      - uses: actions/checkout@v4
      - uses: actions/cache@v4
        id: cache-fuzz
        with:
          path: |
            ~/.cargo/bin
            fuzz/target
            target
          key: cache-${{ matrix.target }}-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: '1.65.0'
      - name: fuzz
        run: |
          if [[ "${{ matrix.fuzz_target }}" =~ ^bitcoin ]]; then
              export RUSTFLAGS='--cfg=hashes_fuzz --cfg=secp256k1_fuzz'
          fi
          echo "Using RUSTFLAGS $RUSTFLAGS"
          cd fuzz && ./fuzz.sh "${{ matrix.fuzz_target }}"
      - run: echo "${{ matrix.fuzz_target }}" >executed_${{ matrix.fuzz_target }}
      - uses: actions/upload-artifact@v4
        with:
          name: executed_${{ matrix.fuzz_target }}
          path: executed_${{ matrix.fuzz_target }}

  verify-execution:
    if: ${{ !github.event.act }}
    needs: fuzz
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
      - name: Display structure of downloaded files
        run: ls -R
      - run: find executed_* -type f -exec cat {} + | sort > executed
      - run: source ./fuzz/fuzz-util.sh && listTargetNames | sort | diff - executed
