version: '3'
services:
  CN:
    hostname: CN
    image: ${DOCKER_IMAGE:-klaytn/klaytn:latest}
    restart: "no"
    ports:
      - ${DOCKER_LISTEN:-8545}:8545
    entrypoint:
      - /bin/bash
      - -c
      - |
        set -e
        mkdir -p /klaytn/log
        cp -r /input/* /klaytn
        if [ ${DOCKER_DEBUG:-0} -eq 0 ]; then
          kcn init --datadir /klaytn /input/genesis.json 2>>/klaytn/log/kcnd.out
          VERBOSITY=2 # WARN
        else
          kcn init --datadir /klaytn /input/genesis.json
          VERBOSITY=4 # DEBUG
        fi

        nohup -- kcn \
          --datadir /klaytn \
          --verbosity $$VERBOSITY \
          --gcmode archive --state.block-interval 1 \
          --netrestrict "127.0.0.1/32" --nodiscover \
          --ipcpath /klaytn/klay.ipc \
          --rpc --rpcvhosts "*" --rpccorsdomain "*" --rpcaddr "0.0.0.0" --rpcport 8545 \
          --rpcapi "admin,debug,eth,governance,istanbul,klay,net,personal,rpc,txpool,web3" \
          --unlock `cat /klaytn/account_addrs` --password /klaytn/password \
          2>>/klaytn/log/kcnd.out &

        set +e
        echo "waiting for node to start.."
        while [ ! -e /klaytn/klay.ipc ]; do
          sleep 0.1
        done
        echo "node started."

        if [ ${DOCKER_DEBUG:-0} -eq 0 ]; then
          tail -n 0 -F /klaytn/log/kcnd.out # do not print previous logs
        else
          tail -F /klaytn/log/kcnd.out
        fi

    volumes:
      - ./input:/input

