Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Restructure BSQ explorer as system service for easy installation #15

Merged
151 changes: 44 additions & 107 deletions README.md
@@ -1,137 +1,74 @@
# Bisq BSQ Explorer
# BSQ Block Explorer

## Overview
The BSQ explorer provides basic information about BSQ transactions.
It is based on static html pages which are rendered from json files generated by a Bisq application running on the
server. Beside http server and Bisq (we run a seednode instance) we require bitcoind as the Bisq node is running in
fullnode mode.
Follow these instructions to setup a [BSQ Block Explorer](https://explorer.bisq.network) using data from your [Bisq Seednode](https://github.com/bisq-network/bisq/tree/master/seednode). Keep in mind you need a dedicated seednode for the BSQ explorer, as the JSON data dump puts too much load on it to seed the network properly.

## Bisq Seednode

### Installation
Requires Bisq seednode with `--dumpBlockchainData=true`
First, [setup your Bisq Seednode](https://github.com/bisq-network/bisq/tree/master/seednode#bisq-seed-node) so you have Tor, Bitcoin, and bisq-seednode running and fully synced. Then, enable BSQ data output on your Bisq Seednode with the following command

#### Web Server
```bash
sudo sed -i -e 's!BISQ_DUMP_BLOCKCHAIN=false!BISQ_DUMP_BLOCKCHAIN=true!' /etc/default/bisq-seednode.env
sudo service bisq-seednode restart
```
sudo apt-get update
sudo apt-get install nginx
```

Check that it's running

```
systemctl status nginx
```
Copy nginx.config to /etc/nginx/sites-available and symlink from /etc/nginx/sites-enabled. Remove sym link to
default site. Replace `hostname` with site name.
It will take 10+ minutes before the seednode starts saving BSQ transaction data.

Install certificate
### Firewall

```
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
sudo certbot --nginx
Open ports 80 and 443 on your firewall for HTTP and HTTPS
```bash
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
```

Set automatic certificate renewal in crontab. Try to renew every month. Edit crontab...
### Let's Encrypt

```
sudo crontab -e
```
...and paste this line2
```
0 0 1 * * certbot renew
```
Next, request an SSL certificate for your server from Let's Encrypt using certbot-nginx

#### Explorer
Some needed pyhton packages
```
sudo pip install python-bitcoinrpc
sudo pip install simplejson
sudo pip install gitpython
```bash
sudo apt-get update -q
sudo apt-get install -q -y nginx-core python-certbot-nginx
sudo certbot --nginx --agree-tos --non-interactive -m ssl@example.com -d explorer.example.com
wiz marked this conversation as resolved.
Show resolved Hide resolved
```

Setup variables for other commands below to use:
```
export EXPLORER_HOME=<your full path to explorer>
export DATADIR=<path to bisq db directory>
```
After obtaining your SSL certificate, you should be able to see the default page at https://explorer.example.com/

examples:
```
#we assume you cloned this repo to: /home/user/bisq-explorer
#we assume bisq-seednode was run with --appName=seed_2002 and testnet is used as network
export EXPLORER_HOME=/home/user/bisq-explorer
export DATADIR=/home/user/.local/share/seed_2002/btc_testnet/db
```
### BSQ Indexer

HTML need to point to explorer/www
```
cd /var/www
sudo ln -s ${EXPLORER_HOME}/www html
Clone this repo to bisq user's homedir
```bash
curl -s https://raw.githubusercontent.com/bisq-network/bisq-explorer/master/install_bsq_explorer_debian.sh | sudo bash
```

Copy Bisq seednode db directory contents to `$EXPLORER_HOME`
```
mkdir ${EXPLORER_HOME}/data
cp -r ${DATADIR}/* ${EXPLORER_HOME}/data
```
### Nginx

For the update script `inotifywait` is needed, it's part of inotify-tools
```
sudo apt install inotify-tools
Install the nginx.conf from this repository, substituting explorer.example.com for your server hostname
```bash
sudo wget -O /etc/nginx/nginx.conf https://raw.githubusercontent.com/bisq-network/bisq-explorer/master/nginx.conf
sudo sed -i -e 's!__HOSTNAME__!explorer.example.com!g' /etc/nginx/nginx.conf
sudo service nginx restart
```

#### Bitcoin
Bitcoin blocknotify script needs `nc`
```
sudo apt install netcat-openbsd
```
You should now be able to access your BSQ explorer at https://explorer.example.com/

Example `bitcoin.conf`
```
# Uncomment to run on testnet
#testnet=1
lang=en

whitelist=127.0.0.1
rpcallowip=127.0.0.1
rpcport=18332

server=1
txindex=1
rpcuser=bisquser
rpcpassword=bisqpasswd
blocknotify=bash $HOME/.bitcoin/blocknotify %s
```
Example `blocknotify`
### Tor onion (optional)

Add these lines to the bottom of /etc/tor/torrc
```
#!/bin/bash
echo $1 | nc -w 1 127.0.0.1 5110

HiddenServiceDir /var/lib/tor/bsqexplorer/
HiddenServicePort 80 127.0.0.1:80
HiddenServiceVersion 2
```
### Run

Copy update_monitor.sh to a location outside of the explorer directory where the
script will be run from. Then edit it to configure the DATADIR and EXPLORER_HOME derictories.

You can use these commands to edit the files:
```
sed -Ei 's|DATADIR=.*|DATADIR='"${DATADIR}"'|' update_monitor.sh
sed -Ei 's|EXPLORER_HOME=.*|EXPLORER_HOME='"${EXPLORER_HOME}"'|' update_monitor.sh
Then restart Tor with the following command
```bash
sudo service tor restart
```

To start the update monitor
```
./update_monitor.sh
```
After Tor restarts, it will generate your onion hostname, get it by doing:

To update the explorer state, using the data from bisq seednode it's possible
to run the update manually.
```
cd ${EXPLORER_HOME}
./update_data.sh ${DATADIR}
```bash
sudo cat /var/lib/tor/bsqexplorer/hostname
```

Then you can also access your BSQ explorer over Tor at http://foo.onion/
32 changes: 32 additions & 0 deletions bsq-explorer
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

# auto-export all variables
set -a

# load environment from bisq-seednode configuration
source /etc/default/bisq-seednode.env

# variables used by this script and bsq-index script
BISQ_DATA_DIR="${BISQ_HOME}/${BISQ_APP_NAME}/${BISQ_BASE_CURRENCY}"
BISQ_JSON_DIR="${BISQ_DATA_DIR}/db/json"
BSQ_EXPLORER_ROOT="${BISQ_HOME}/bisq-explorer/www"
BSQ_EXPLORER_DATA="${BSQ_EXPLORER_ROOT}/data"

while true
do
# sync
echo "Syncing BSQ transaction data JSON from ${BISQ_JSON_DIR} to ${BSQ_EXPLORER_DATA}/"
rsync -hrt --stats --exclude '*.tmp' "${BISQ_JSON_DIR}" "${BSQ_EXPLORER_DATA}/"

# index
echo "Indexing BSQ transaction data JSON and updating explorer HTML in ${BSQ_EXPLORER_ROOT}"
/usr/local/bin/bsq-index

# wait
echo "Waiting for BSQ transaction data update from Bisq seednode"
inotifywait -qq -r -e modify,move,create,delete "${BISQ_JSON_DIR}"
echo "Detected BSQ transaction data update from Bisq seednode"

# allow time for seednode to complete JSON data dump
sleep 10
done
20 changes: 20 additions & 0 deletions bsq-explorer.service
@@ -0,0 +1,20 @@
[Unit]
Description=BSQ Explorer
After=bisq-seednode.service

[Service]
ExecStart=/usr/local/bin/bsq-explorer
ExecStop=/bin/kill -TERM ${MAINPID}
Restart=on-failure

User=bisq
Group=bisq

PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true
PrivateDevices=true
MemoryDenyWriteExecute=false

[Install]
WantedBy=multi-user.target