The lazy programmer scripts
scripts that work

Deploy Polygon (Matic) private RPC GitHub

Polygon (former Matic) network is a Layer 2 scaling solution for Ethereum, with high throughput and security based on periodic snapshots saved on Ethereum. The documentation to have a private RPC node is awfully bad (@0xpolygon, please fix it).

Requirements for deploying a private RPC:

* 4 core or more VM/baremetal

* 16 GB or more of RAM

* 1 TB of SSD or NVME. An SSD is *REQUIRED*, do not try this on an HDD, you will just waste your time

This script has been tested on Ubuntu Server 20.04, deployed on a Xeon 4 core / 8 threads, 32 GB RAM DDR3, 940 GB ENTERPRISE SSD.

# update the repos
sudo apt update

# install required packages
sudo apt install -y python3 python3-pip git
sudo pip3 install ansible

# let's check what your speed of the SSD is
# if you get less than 20MB/s, I suggest you do not go further
# and get a faster SSD. My setup had 40MB/S and could catch up
# around 1h of blocks in 6 minutes.

dd if=/dev/zero of=/tmp/output.img bs=8k count=50 oflag=dsync

# now, we can get the heimdall and bor (pruned, is the smallest one) latest snapshots from 
# https://snapshots.matic.today/
# Verify on https://snapshots.matic.today/ if newer snapshots are available
# One day in addition of catching up means hours more

wget https://matic-blockchain-snapshots.s3-accelerate.amazonaws.com/matic-mainnet/heimdall-snapshot-2021-08-25-tar.gz
wget https://matic-blockchain-snapshots.s3-accelerate.amazonaws.com/matic-mainnet/bor-pruned-snapshot-2021-08-30.tar.gz

# let's prepare the heimdall/bor installation
git clone https://github.com/maticnetwork/node-ansible
cd node-ansible

# Edit the inventory.yml file and insert your IP(s) in the sentry->hosts section.
# should look like this, where 10.0.0.1 is your public ip,
# because we will run heimdall and bor on the same machine.
# localhost works too.
all:

  hosts:
  children:
    sentry:
      hosts:
       10.0.0.1:
    validator:
      hosts:
        10.0.0.1:

# now, let's configure ansible for ssh key authentication
# create a ssh key for the user, running ssh-keygen (if you do not have one already)
# do not run this command if you already have a file ~/.ssh/id_rsa.pub
cat /dev/zero | ssh-keygen -q -N ""

# set authorized_keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

# if running under another user than ubuntu,
# edit group_vars/all and replace ubuntu with your user
# ansible_user:YOUR_USER

# if you do not have passwordless sudo authentication, in the same file,
# you need to add below the ansible_user:YOUR_USER line,
# ansible_sudo_pass:YOUR_PASSWORD

# check ansible
ansible sentry -m ping

# check ansible runs on the correct hosts
ansible-playbook -l sentry playbooks/network.yml --extra-var="bor_branch=master heimdall_branch=master  network_version=mainnet-v1 node_type=sentry/sentry heimdall_network=mainnet" --list-hosts

# install heimdall and bor
ansible-playbook -l sentry playbooks/network.yml --extra-var="bor_branch=master heimdall_branch=master  network_version=mainnet-v1 node_type=sentry/sentry heimdall_network=mainnet"

# configure heimdall

# Edit ~/.heimdalld/config/config.toml
# Set seeds:
# seeds="f4f605d60b8ffaaf15240564e58a81103510631c@159.203.9.164:26656,4fb1bc820088764a564d4f66bba1963d47d82329@44.232.55.71:26656,2eadba4be3ce47ac8db0a3538cb923b57b41c927@35.199.4.13:26656,3b23b20017a6f348d329c102ddc0088f0a10a444@35.221.13.28:26656,25f5f65a09c56e9f1d2d90618aa70cd358aa68da@35.230.116.151:26656"

# Edit ~/.heimdalld/config/heimdall-config.toml
# Set ETHEREUM RPC URL (I use one from infura)
# eth_rpc_url = insert_here_url

# use the snapshot for heimdall
# this command takes a while, grab some coffee
tar -xzvf heimdall-snapshot-2021-08-25-tar.gz -C ~/.heimdalld/data/

# start heimdall
sudo service heimdalld start
sudo service heimdalld-rest-server start

# check heimdall logs
sudo journalctl -u heimdalld.service -f

# check heimdall status, catching_up should be false
curl localhost:26657/status

# DO not try to run bor before heimdalld is synced up
# after heimdall status catching_up = true, run the following

# Configure bor
# Edit ~/node/bor/start.sh and add at the end

--bootnodes "enode://0cb82b395094ee4a2915e9714894627de9ed8498fb881cec6db7c65e8b9a5bd7f2f25cc84e71e89d0947e51c76e85d0847de848c7782b13c0255247a6758178c@44.232.55.71:30303,enode://88116f4295f5a31538ae409e4d44ad40d22e44ee9342869e7d68bdec55b0f83c1530355ce8b41fbec0928a7d75a5745d528450d30aec92066ab6ba1ee351d710@159.203.9.164:30303,enode://3178257cd1e1ab8f95eeb7cc45e28b6047a0432b2f9412cff1db9bb31426eac30edeb81fedc30b7cd3059f0902b5350f75d1b376d2c632e1b375af0553813e6f@35.221.13.28:30303,enode://16d9a28eadbd247a09ff53b7b1f22231f6deaf10b86d4b23924023aea49bfdd51465b36d79d29be46a5497a96151a1a1ea448f8a8666266284e004306b2afb6e@35.199.4.13:30303,enode://ef271e1c28382daa6ac2d1006dd1924356cfd843dbe88a7397d53396e0741ca1a8da0a113913dee52d9071f0ad8d39e3ce87aa81ebc190776432ee7ddc9d9470@35.230.116.151:30303"

# Other features that can be enabled / set in ~/node/bor/start.sh

# To enable WSS, add:

--ws --ws.addr "0.0.0.0" --ws.origins="*" --ws.port 8546 \

# To enable GraphQl, add:

--graphql --graphql.corsdomain '*' --graphql.vhosts '*' \

# To set more cache - 16GB, add:

--cache 16384 \

# To enable txs with more than 1 MATIC gas fee, add:

--rpc.txfeecap 0 \


# Extract the bor pruned snapshot
# Grab a cup of coffee, it should take at least an hour to extract 300+GB
tar -xzvf bor-pruned-snapshot-2021-08-30.tar.gz -C ~/.bor/data/bor/chaindata

# Start bor
sudo service bor start

# Check if bor works and if the catch up time decreases
# Every 20 seconds, 5k txs should be catched up on a 40MB/s speed
# You can approximate yourself looking at the time
sudo journalctl -u bor.service -f

# Check bor last block
# The block number should increase by at least 50 per minute
curl -X POST -s --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest", false],"id":1}' http://localhost:8545/ -H "Content-Type: application/json"| jq '.result.number' | xargs printf "%d\n"

# Check GraphQL

curl -X POST 'http://localhost:8545/graphql' -H 'Content-Type: application/json' --data-raw '{"query":"{pending{transactionCount transactions{hash}}}"}'

That's all, folks!

Written by kami on Tuesday September 7, 2021
Permalink -

Leave a comment

comments powered by Disqus

« Lazy speed up apt install on baremetal - Leader election (PowerShell) »