Mempool.space

Time: 20 minutes

Mempool.space allows you to keep track of bitcoin's mempool yourself. This is useful for determining how much fee to pay or to see if your transaction has already gone through. By hosting it yourself you are not handing over any data to third parties.

Preparation

  1. Electrs
  2. NodeJS

Installation

Get the source code:

git clone https://github.com/mempool/mempool

Enter the directory with cd mempool. Then put the latest release in a temporary variable called latestrelease in your terminal.

latestrelease=$(curl -s https://api.github.com/repos/mempool/mempool/releases/latest|grep tag_name|head -1|cut -d ''' -f4)

Then grab the latest version with git checkout $latestrelease.

Maria DB

Mempool.space needs a database to store some data in. That's why it uses Maria DB.

sudo apt install mariadb-server mariadb-client -y

Log in to Maria DB.

sudo mariadb -u root

Once in Maria DB, you can execute SQL code. Mempool.space needs a database so create it. Execute all of the command, including the ;.

create database mempool;

Then create a user who will have access to that particular database.

grant all privileges on mempool.* to 'mempool'@'%' identified by 'mempool';

Finally, you can close the database connection with quit;.

Backend

The tool consists of two parts which run separately: a backend and a frontend. The frontend is shown in your browser and the backend requested the data for the frontend. Enter the backend directory with cd backend. Then install the backend dependencies with npm install --prod after which you make a build npm run build.

The backend needs a configuration and for that we have a JSON file. Copy the sample configuration to a usable one.

cp mempool-config.sample.json mempool-config.json

Open the file nano mempool-config.json. If all goes well, the file looks like the one below. It is quite large and take great care to change the right things:

  • FROM_BITCOIND_A and FROM_BITCOIND_C: The user that you created.
  • FROM_BITCOIND_B and FROM_BITCOIND_D: The password that you were given.
{
"MEMPOOL": {
"NETWORK": "mainnet",
"BACKEND": "electrum",
"HTTP_PORT": 8999,
"SPAWN_CLUSTER_PROCS": 0,
"API_URL_PREFIX":"/api/v1/",
"POLL_RATE_MS": 2000,
"CACHE_DIR": "./cache",
"CLEAR_PROTECTION_MINUTES": 20,
"RECOMMENDED_FEE_PERCENTILE": 50,
"BLOCK_WEIGHT_UNITS": 4000000,
"INITIAL_BLOCKS_AMOUNT": 8,
"MEMPOOL_BLOCKS_AMOUNT": 8,
"PRICE_FEED_UPDATE_INTERVAL": 3600,
"USE_SECOND_NODE_FOR_MINFEE": false,
"EXTERNAL_ASSETS": []
},
"CORE_RPC": {
"HOST": "127.0.0.1",
"PORT": 8332,
"USERNAME": "THIS_KNOW_YOU_A",
"PASSWORD": "THIS_KNOW_YOU_B"
},
"ELECTRUM": {
"HOST": "127.0.0.1",
"PORT": 50001,
"TLS_ENABLED": true
},
"ESPLORA": {
"REST_API_URL": "http://127.0.0.1:3000"
},
"SECOND_CORE_RPC": {
"HOST": "127.0.0.1",
"PORT": 8332,
"USERNAME": "THIS_KNOW_YOU_C",
"PASSWORD": "THIS_KNOW_YOU_D"
},
"DATABASE": {
"ENABLED": true,
"HOST": "127.0.0.1",
"PORT": 3306,
"DATABASE": "mempool",
"USERNAME": "mempool",
"PASSWORD": "mempool"
},
"SYSLOG": {
"ENABLED": true,
"HOST": "127.0.0.1",
"PORT": 514,
"MIN_PRIORITY": "info",
"FACILITY": "local7"
},
"STATISTICS": {
"ENABLED": true,
"TX_PER_SECOND_SAMPLE_PERIOD": 150
},
"BISQ": {
"ENABLED": false,
"DATA_PATH": "/bisq/statsnode-data/btc_mainnet/db"
}
}

Frontend

Now that the backend is taken care of we can focus on the frontend. This starts with navigating to the right directory.

cd ../frontend

Again you need to install the dependencies with npm install --prod. Then you can copy the sample configuration, but you don't need to modify it.

cp mempool-frontend-config.sample.json mempool-frontend-config.json

It may happen that the build below fails. If so, please try again later. Unfortunately there is no better solution for this.

You can build the frontend with npm run build (may take quite some time) and finally move the output of the build to another directory. Nginx will redirect traffic to that directory.

sudo rsync -av --delete dist/ /var/www/

Server

The website is only available through Nginx.

sudo apt install -y nginx

After installing Nginx, go back to the root of the project with cd ~/mempool. From there, install the Nginx configuration from mempool.

sudo cp nginx.conf nginx-mempool.conf /etc/nginx/

Then modify two lines of the configuration with sudo nano /etc/nginx/nginx.conf. A screen will open where you will see in the top line user nobody;. Change it to user ubuntu;. Scroll down to the bottom where you will see listen 127.0.0.1:80;. That should become listen 0.0.0.0:80;. Save it with Ctrl + X and confirm with Y. You can check the configuration with sudo nginx -t whose output should look like:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To finish the Nginx setup, restart Nginx with sudo systemctl restart nginx.

Firewall

Reaching mempool.space is done through normal internet addresses (clearnet). On port 80. Nginx further handles where the traffic goes.

sudo ufw allow 80 comment "Port for normal internet traffic"

Automation

The frontend of mempool.space is now in a folder you don't need worry about. However, it is important that the backend runs in the background. We create a service for this.

sudo nano /etc/systemd/system/mempool.service

Put the following text in there.

[Unit]
Description=Mempool
Requires=bitcoind.service
Requires=electrs.service
After=bitcoind.service
After=electrs.service
[Service]
WorkingDirectory=/home/ubuntu/mempool/backend
ExecStart=npm run start
User=ubuntu
Group=ubuntu
Type=simple
Restart=on-failure
TimeoutSec=120
RestartSec=30
[Install]
WantedBy=multi-user.target

Save it with Ctrl + X and confirm with Y.

Enable and start the service with these two commands:

sudo systemctl enable mempool
sudo systemctl start mempool

With systemctl status mempool you can see if everything is running properly and get an output similar to the one below.

● mempool.service - Mempool
Loaded: loaded (/etc/systemd/system/mempool.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-01-31 09:59:52 UTC; 25s ago
Main PID: 209191 (npm run start)
Tasks: 27 (limit: 4203)
CGroup: /system.slice/mempool.service
├─209191 npm run start
├─209202 sh -c node --max-old-space-size=2048 dist/index.js
└─209203 node --max-old-space-size=2048 dist/index.js
Jan 31 10:00:15 ubuntu npm[209203]: Jan 31 10:00:15 [209203] DEBUG: Fetched transaction 14 / 19
Jan 31 10:00:15 ubuntu npm[209203]: Jan 31 10:00:15 [209203] DEBUG: Fetched transaction 15 / 19
Jan 31 10:00:15 ubuntu npm[209203]: Jan 31 10:00:15 [209203] DEBUG: Fetched transaction 16 / 19

Updating

Go to the application directory.

cd ~/mempool

Update the repository with the latest changes via Git.

git fetch --all

Then put the latest release in a temporary variable called latestrelease in your terminal.

latestrelease=$(curl -s https://api.github.com/repos/mempool/mempool/releases/latest|grep tag_name|head -1|cut -d ''' -f4)

Get the latest version with git checkout $latestrelease.

Update backend

Enter the backend directory with cd backend. Then install the backend dependencies with npm install --prod after which you make a build npm run build.

Restart the mempool service.

sudo systemctl restart mempool

Update frontend

First, go to the directory containing the frontend code.

cd ../frontend

Again, you need to install the dependencies with npm install --prod.

It may happen that the build below fails. Please try again later. Unfortunately there is no better solution for this.

You can build the frontend with npm run build (can take quite a long time). The Nginx directory with the production version should be updated automatically.