#!/bin/bash
set -euo pipefail

# ── V31 Trinity Miner Wrapper for SMOS ──────────────────────────────────────
# Triple-stream: ZION (GPU) + ZANO (GPU AuxPoW) + VRSC (CPU AuxPoW)
# Multi-GPU PARALLEL: BOTH GPUs mine BOTH ZION Deeksha AND ZANO ProgPoW.
# Time-slicing via yield_now (GAP_MS=0) — GPU driver serializes kernels.
# VRSC on CPU (Stream 3). Max performance: 2x ZION + 2x ZANO + 1x VRSC.
# V3 Trinity architecture: single V3 protocol connection to ZION pool.
# Pool embeds external_stream jobs and forwards AuxPoW shares to external pools.
# All revenue flows through the pool's AuxPoW bridge and revenue system.
# Date: 2026-08-10 (V3.2 Trinity multi-GPU PARALLEL, max performance)

# Pool wallet — miner uses this for ZION coinbase. Pool handles ZANO/VRSC wallets.
WALLET_ADDR="zion1s6m204400290l660k622r3r0c6u040g5j6cu2x5"
WORKER_NAME="vega-smos"
export ZION_MINER_ID="vega-smos"

# ── Core miner config ─────────────────────────────────────────────────────
export ZION_GPU_BACKEND="${ZION_GPU_BACKEND:-opencl}"
export ZION_PROFILE="${ZION_PROFILE:-pool}"
export ZION_VERBOSE=1
export ZION_INTERACTIVE=1
export ZION_NO_STICKY=1
export ZION_METRICS_REPORT_SECS=15
export ZION_STATS_FILE="/tmp/zion-miner-stats.json"

# ── V3 Trinity mode ────────────────────────────────────────────────────────
# All 3 streams through a single V3 protocol connection to the pool.
# The pool distributes ZANO (GPU) and VRSC (CPU) jobs via external_stream fields.
export ZION_V3_TRINITY=1

# ── GPU / autotune ────────────────────────────────────────────────────────
export ZION_AUTOTUNE=1
export ZION_AUTOTUNE_SECS=3
export ZION_IGNORE_GPU_SELF_TEST_FAIL=1
# NOTE: Do NOT set ZION_NO_GCN_S4_MODE — s4_mode is the default and REQUIRED
# for Vega 64 (GCN/gfx900).  Forcing full GPU pipeline on GCN produces 0%
# accepted shares due to compiler bugs in NPU/fusion stages (stages 5-6).
# See docs/3.0.1Genesis/VEGA64_S4_MEMHARD_DEBUG_GUIDE.md
# RX 5600 XT (RDNA1/gfx1010) uses full GPU pipeline by default (not GCN).
export ZION_OCL_BUILD_OPTS="${ZION_OCL_BUILD_OPTS:--cl-std=CL1.2 -cl-mad-enable}"

# Deeksha (ZION) GPU stream — work_size=8192 (RDNA1 cap, GCN auto-caps to 4096).
# Auto-tune per-device: Vega 64 (GCN) → 4096, RX 5600 XT (RDNA1) → 8192.
# LOCAL_SIZE: GCN needs 64 (wave64), RDNA1 uses 256 (wave32). Auto-tune handles
# this per-device, but ZION_OCL_LOCAL_SIZE override can break GCN if set wrong.
# See docs/3.0.1Genesis/VEGA64_S4_MEMHARD_DEBUG_GUIDE.md (work size table)
# See docs/3.0.6/VEGA_SMOS_DUAL_GPU_REPORT.md (v90 config)
export ZION_MINER_ALGORITHM=deeksha_lite_v1
export ZION_GPU_WORK_SIZE=8192
export ZION_NONCE_AUTOTUNE=1
export ZION_NONCE_COUNT=65536
export ZION_NONCE_COUNT_MIN=32768
export ZION_NONCE_COUNT_MAX=262144
export ZION_GPU_MAX_BATCH=65536
export ZION_GPU_EARLY_BREAK=0
export ZION_GPU_NO_STREAM_BYPRODUCT=1

# ── Triple-stream config ──────────────────────────────────────────────────
# In V3 Trinity mode, the pool decides which coins to send based on its
# auxpow_runtime configuration (ZANO + VRSC). No direct stream URLs needed.
export ZION_STREAM1_ENABLED=1
export ZION_STREAM2_ENABLED=1
export ZION_STREAM3_ENABLED=1

# GPU AuxPoW tuning (ZANO ProgPoWZ)
# PARALLEL mode: BOTH GPUs mine BOTH ZION and ZANO via time-slicing.
# GAP_MS=0 → tokio::yield_now() (no sleep, just cooperative scheduling).
# GPU driver serializes ZION and ZANO kernels on each GPU.
# Vega 64 (GCN): GROUP_SIZE=128, bpermute auto-disabled (GCN-safe)
# RX 5600 XT (RDNA1): GROUP_SIZE=256, bpermute auto-enabled (wave32)
# See docs/3.0.6/parallelgpu.md (RDNA1 bpermute optimization)
# See docs/3.0.6/VEGA64_ZANO_TUNING_REPORT.md (Vega 64 ZANO tuning)
# See docs/3.0.6/VEGA_SMOS_DUAL_GPU_REPORT.md (v90 stable config)
export ZION_STREAM2_BATCH=2097152
export ZION_STREAM2_FORCE_COIN=ZANO
export ZION_AUXPOW_GPU_WORK_SIZE=1048576
export ZION_AUXPOW_GPU_GROUP_SIZE=128
export ZION_AUXPOW_GPU_VRAM_PCT=50
export ZION_AUXPOW_GPU_BYTES_PER_ITEM=64
export ZION_ZANO_STALE_SECS=30

# Multi-GPU PARALLEL mode: ALL GPUs mine ZION (MultiGpuMiner) AND ZANO
# (time-sliced). ZION_ZANO_RESERVE=0 → no GPU reserved, all GPUs to
# MultiGpuMiner for ZION. ZANO stream picks GPU via pick_opencl_device()
# and shares via time-slicing (yield_now when GAP_MS=0).
# DUTY_PCT=100: ZANO gets full GPU burst when active.
export ZION_ZANO_RESERVE=0
export ZION_ZANO_DEVICE_NAME=vega
export ZION_EXT_GPU_TIME_DUTY_PCT=100
export ZION_EXT_GPU_GAP_MS=0
export ZION_EXT_GPU_MAX_GAP_MS=0

# CPU AuxPoW tuning (VRSC VerusHash — pool sends jobs, miner mines)
export ZION_MINER_CPU_COIN=VRSC
export ZION_STREAM3_BATCH=2000000
export ZION_EXT_CPU_NONCE_COUNT=2000000
export ZION_MINER_THREADS=4

# ── Download V31 miner binary ─────────────────────────────────────────────
LOCAL_MINER="/tmp/zion-miner-v31"
rm -f "${LOCAL_MINER}"

EDGE_BASE="http://62.171.141.136/zion-miner"
echo "[smos-wrapper] downloading V31 miner binary ..."
rm -f "${LOCAL_MINER}.tmp"
curl --http1.1 --retry 20 --retry-delay 5 --connect-timeout 30 \
     --speed-time 60 --speed-limit 10000 \
     -fsSL -o "${LOCAL_MINER}.tmp" "${EDGE_BASE}/zion-miner-v31" || {
    echo "[smos-wrapper] FATAL: could not download V31 miner binary"
    exit 1
}
chmod +x "${LOCAL_MINER}.tmp"
mv "${LOCAL_MINER}.tmp" "${LOCAL_MINER}"
echo "[smos-wrapper] V31 miner binary ready ($(stat -c%s "${LOCAL_MINER}") bytes)"

echo "[smos-wrapper] starting V3 TRINITY multi-GPU PARALLEL: 2x ZION + 2x ZANO + VRSC CPU"
exec "${LOCAL_MINER}" \
  --pool "${ZION_POOL_ADDR:-62.171.141.136:8444}" \
  --wallet "${WALLET_ADDR}" \
  --worker "${WORKER_NAME}" \
  --gpu "${ZION_GPU_BACKEND}" \
  --threads "${ZION_MINER_THREADS}" \
  --v3-trinity \
  "$@"
