name: Smoking test suit

on:
  workflow_dispatch:
    inputs:
      CKB_linux_release_package:
        description: "Linux release package to somking test"
        required: false
  repository_dispatch:
    types: [smoking-test]
env:
  RUSTFLAGS: "-D warnings"
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: full
  TESTNET_SNAPSHOT_URL: ${{secrets.TESTNET_SNAPSHOT_URL}}
  CARGO_TARGET_DIR: ${{ github.workspace }}/../target
  PGHOST: ${{ secrets.PGHOST }}
  PGPORT: ${{ secrets.PGPORT }}
  PGUSER: ${{ secrets.PGUSER }}
  PGPASSWORD: ${{ secrets.PGPASSWORD }}
  PGDATABASE: "ckbtest"

jobs:
  Start_and_sync:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      - name: Download&Unzip v0.35.0
        run: |
          echo "start_time=$(date +%Y-%m-%d' '%H:%M:%S.%6N)" >> $GITHUB_ENV
          if [ ! -f "/tmp/ckb_v0.35.0_linux.tar.gz" ]; then
             curl -L https://github.com/nervosnetwork/ckb/releases/download/v0.35.0/ckb_v0.35.0_x86_64-unknown-linux-gnu.tar.gz -o /tmp/ckb_v0.35.0_linux.tar.gz
          fi
          tar -zxf /tmp/ckb_v0.35.0_linux.tar.gz -C /tmp
          cp /tmp/ckb_*/ckb ${{ github.workspace }}/ckb
          rm -rf /tmp/ckb_*
      - name: Update ExecStart&StandardOutput
        run: |
          sed -i  "s#User=.*#User=$USER#g" ${{ github.workspace }}/devtools/smoking_test/ckb.service
          sed -i  "s#ExecStart=.*#ExecStart=${{github.workspace}}/ckb run -C ${{github.workspace}}#g" ${{ github.workspace }}/devtools/smoking_test/ckb.service
          sed -i  "s#StandardOutput=.*#StandardOutput=file:${{github.workspace}}/data/logs/run.log#g" ${{ github.workspace }}/devtools/smoking_test/ckb.service
      - name: Init&Start ckb testnet with v0.35.0
        run: |
          ${{ github.workspace }}/ckb init -c testnet -C ${{ github.workspace }} --force
          sudo cp ${{ github.workspace }}/devtools/smoking_test/ckb.service /etc/systemd/system/ckb.service
          sudo systemctl daemon-reload
          sudo systemctl enable ckb
          sudo service ckb start
          sleep 600
          sudo service ckb stop
          sudo systemctl disable ckb
          sudo rm /etc/systemd/system/ckb.service
          sudo systemctl daemon-reload
          sudo systemctl reset-failed
          rm ${{ github.workspace }}/ckb
      - run: sudo apt-get update && sudo apt-get install libssl-dev pkg-config libclang-dev -y && sudo apt-get install -y gcc-multilib build-essential
      - run: sudo apt-get install -y wget ca-certificates && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - && sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' && sudo apt-get update && sudo apt-get install -y postgresql postgresql-contrib
      - name: Download target ckb release pkg and copy binary to github workspace
        if: |
          (github.event_name == 'workflow_dispatch' && github.event.inputs.CKB_linux_release_package != '')
          || contains(github.event_name, 'repository_dispatch')
        run: |
          if [ -z ${{ github.event.inputs.CKB_linux_release_package }} ]; then
          curl -L ${{ github.event.client_payload.CKB_linux_release_package }} -o /tmp/ckb.7z
          7za x "/tmp/ckb.7z" -r -o/tmp
          fi
          if [ -z ${{ github.event.client_payload.CKB_linux_release_package }} ]; then
          curl -L ${{ github.event.inputs.CKB_linux_release_package }} -o /tmp/ckb.tar.gz
          tar -zxf /tmp/ckb.tar.gz -C /tmp
          fi
          cp /tmp/ckb_*/ckb ${{ github.workspace }}/ckb
      - name: Build CKB binary
        if: |
          (github.event_name == 'workflow_dispatch' && github.event.inputs.CKB_linux_release_package == '')
          && ! contains(github.event_name, 'repository_dispatch')
        run: |
          make build
          cp ${CARGO_TARGET_DIR}/release/ckb ${{ github.workspace }}/ckb
      - name: Update ExecStart&StandardOutput
        run: |
          sed -i  "s#User=.*#User=$USER#g" ${{ github.workspace }}/devtools/smoking_test/ckb.service
          sed -i  "s#ExecStart=.*#ExecStart=${{github.workspace}}/ckb run -C ${{github.workspace}}#g" ${{ github.workspace }}/devtools/smoking_test/ckb.service
          sed -i  "s#StandardOutput=.*#StandardOutput=file:${{github.workspace}}/data/logs/run.log#g" ${{ github.workspace }}/devtools/smoking_test/ckb.service
      - name: Regenerate testnet configuration
        run: |
          ${{ github.workspace }}/ckb init -c testnet -C ${{ github.workspace }} --force
      - name: Migration check
        run: |
          ${{ github.workspace }}/devtools/smoking_test/check-migrate.sh
      - name: Start ckb service.
        if: ${{ success() }}
        run: |
          sudo cp ${{ github.workspace }}/devtools/smoking_test/ckb.service /etc/systemd/system/ckb.service
          sudo systemctl daemon-reload
          sudo systemctl enable ckb
          sudo service ckb start
          sudo service ckb status
      - name: Ensure the ckb service is health.
        run: ${{ github.workspace }}/devtools/smoking_test/tip_block_growth_check.sh
      - name: Stop & clean ckb service.
        if: ${{ success() }}
        run: |
          rm -rf /tmp/data
          rm -rf /tmp/ckb*
          sudo service ckb stop
          sudo systemctl disable ckb
          sudo systemctl daemon-reload
          sudo systemctl reset-failed
          sudo rm /etc/systemd/system/ckb.service
          echo "GITHUB_RUN_STATE=0" >> $GITHUB_ENV
      - name: Set test state when test failed.
        if: ${{ failure() }}
        run: |
          echo "GITHUB_RUN_STATE=1" >> $GITHUB_ENV
      - name: insert data to DB
        if: ${{ always() && github.repository_owner == 'nervosnetwork'}}
        env:
          PGHOST: ${{ secrets.PGHOST }}
          PGPORT: ${{ secrets.PGPORT }}
          PGUSER: ${{ secrets.PGUSER }}
          PGPASSWORD: ${{ secrets.PGPASSWORD }}
          PGDATABASE: ckbtest
        run: |
          if [ -z "${PGHOST}" ];then
            end_time=$(date +%Y-%m-%d' '%H:%M:%S.%6N)
            psql -c "INSERT INTO smoking_test (github_run_id,github_run_state,start_time,end_time,github_branch,github_trigger_event,github_run_link)  \
               VALUES ('$GITHUB_RUN_ID','${{ env.GITHUB_RUN_STATE }}','${{ env.start_time }}','$end_time','${{ github.ref_name }}','${{ github.event_name }}','https://github.com/${{ github.repository }}actions/runs/${{ github.run_id }}');"
          fi
    env:
      CKB_DIR: ${{ github.workspace }}
