name: Post-merge APC tests
on:
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - '**.rs'
      - '**.toml'

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-C target-cpu=native"
  RUST_BACKTRACE: 1
  JEMALLOC_SYS_WITH_MALLOC_CONF: "retain:true,background_thread:true,metadata_thp:always,thp:always,dirty_decay_ms:-1,muzzy_decay_ms:-1,abort_conf:true"
  POWDR_OPENVM_SEGMENT_DELTA: 10000

jobs:
  test_guests_apc:
    runs-on: self-hosted

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: ⚡ Cache rust
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-release-apc-${{ hashFiles('**/Cargo.toml') }}

      - name: Build
        run: cargo build --release -p powdr-openvm

      - name: Install cargo openvm
        # Rust 1.88 is needed by fresher versions of dependencies of cargo-openvm.
        # For now we point to the powdr-labs/openvm commit that switches to Rust 1.88.
        run: |
          rustup toolchain install 1.88
          cargo +1.88 install --git 'http://github.com/powdr-labs/openvm.git' --rev fbd69da cargo-openvm

      - name: Run keccak with 100 APCs
        run: /usr/bin/time -v cargo run --bin powdr_openvm -r prove guest-keccak --input 10000 --autoprecompiles 100 --recursion

      - name: Run ECC with 100 APCs
        run: /usr/bin/time -v cargo run --bin powdr_openvm -r prove guest-ecc-powdr-affine-hint --input 20 --autoprecompiles 100 --recursion

      - name: Run ecrecover with 100 APCs
        run: /usr/bin/time -v cargo run --bin powdr_openvm -r prove guest-ecrecover --input 20 --autoprecompiles 100 --recursion

      - name: Checkout openvm-reth-benchmark
        uses: actions/checkout@v4
        with:
          repository: powdr-labs/openvm-reth-benchmark
          ref: main
          path: openvm-reth-benchmark

      - name: Patch openvm-reth-benchmark to use local powdr
        run: |
          cd openvm-reth-benchmark
          mkdir -p .cargo
          echo '[patch."https://github.com/powdr-labs/powdr.git"]' > .cargo/config.toml
          echo 'powdr-openvm = { path = "../openvm" }' >> .cargo/config.toml
          echo 'powdr-riscv-elf = { path = "../riscv-elf" }' >> .cargo/config.toml
          echo 'powdr-number = { path = "../number" }' >> .cargo/config.toml
          echo 'powdr-autoprecompiles = { path = "../autoprecompiles" }' >> .cargo/config.toml

      - name: Run reth benchmark
        run: |
          cd openvm-reth-benchmark
          RES_DIR=reth
          mkdir -p $RES_DIR
          echo "export RPC_1=${{ secrets.RPC_1 }}" >> .env

          # prove with 100 APCs
          MODE="prove-stark" APC=100 /usr/bin/time -v ./run.sh || exit 1
          echo "Finished proving with 100 APCs"

      - name: Save revisions and run info
        run: |
          echo "openvm-reth-benchmark: $(git -C openvm-reth-benchmark rev-parse HEAD)" > run.txt
          echo "powdr: $(git rev-parse HEAD)" >> run.txt
          echo "run: https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" >> run.txt

      - name: upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: bench-results
          path: |
            run.txt
