fix: bound Sapling proving concurrency and resource reservations

FE
1d ago
+2788-194

Concurrent Sapling proving currently creates a CPU-sized pool per proof and extra FFT threads, without an aggregate admission budget. Add process-wide count/working-set admission and a shared bounded worker for the two Sapling proving exports. Saturation returns false before circuit synthesis or caller-state mutation.

  • Defaults: at most 2 operations, 256 MiB reserved working set, 2 shared pool threads. Spend reserves 256 MiB and Output 128 MiB, admitting one Spend or two Outputs by default. There is no native admission queue.
  • Startup environment supports bounded tuning: TRON_SAPLING_MAX_PROOFS (1..8), TRON_SAPLING_MEMORY_BUDGET_MIB (128..4096), and TRON_SAPLING_WORKER_THREADS (1..8). Invalid configuration refuses proving and sets FFI status 3 for JNI to report.
  • CPU tasks retain the operation reservation until their captured work is disposed, including after caller failure/cancellation. On normal completion, cleanup and reservation release precede notification of the waiting caller. Scoped workers join before the enclosing reservation is released.
  • Round FFT chunks up to avoid extra threads on uneven sizes. Existing/Sprout Bellman callers keep their worker policy. The Sapling worker bound is W + P*W, plus P calling threads (default: at most 6 worker threads).
  • Verification and the Sprout implementations are unchanged. Already staged contexts/outputs permit same-context retry after rejection.

Memory figures are conservative admission reservations, not allocation counters or JVM RSS limits; shared parameters and other JVM/node memory are excluded. This reduces concurrent proving resource amplification, not all OOM risk. No forced cancellation or consensus-validation timeout is added.

This branch depends on Rust #16 (b7a153d) for atomic initialization and panic-safe state publication; its prerequisite commits remain visible until merged. Review the final resource-budget commit separately. The existing PR is not modified. Companion SDK PR https://github.com/tronprotocol/zksnark-java-sdk/pull/24 updates the Rust pin, maps configuration errors, and adds JNI regressions.

Validation on macOS x86_64 / JDK 8:

  • 26 Rust library tests passed, one fixture-dependent initializer test ignored by default; admission count/weight, concurrency, scoped panic cleanup, and caller unwind with a still-running task are covered.
  • All 11 Bellman tests passed in release mode, including bounded chunking, FFT equivalence, fixed-randomness proof equality, and cleanup-before-completion notification.
  • 42 Java tests passed with instrumented release JNI. New cases use valid Spend/Output inputs, unchanged sentinel outputs, nonempty contexts, successful retry and binding signatures, verification during saturation, eight concurrent rejected callers, and malformed configuration.
  • All 4 resource scenarios passed with java-tron's production parameters; ordinary release JNI passed 35 tests.
  • Production Rust/JNI contain no reservation/injection test symbols.

Release integration still requires combining the independent fixes, rebuilding all four packaged JNI binaries, validating the final artifacts, and updating java-tron's SDK dependency. See librustzcash/PROVING_RESOURCES.md for semantics and reproduction.