January 10, 2023

Starting the OKP4 node using StateSync/SnapShot !!!

State sync allows a new node joining a network to simply fetch a recent snapshot of the application state instead of fetching and applying all historical blocks. This can reduce the time needed to join the network by several orders of magnitude (e.g. weeks to minutes), but the node will not contain historical data from previous heights.

To run OKP4 using StateSync, be sure to include snapshots in app.toml!!!

Stop OKP4 and clear all uploaded blockchain data ( !!!!! make a backup of the priv_validator_key.json file !!!!!):

sudo systemctl stop okp4d && okp4d tendermint unsafe-reset-all --home $HOME/.okp4d --keep-addr-book

Specify OKP4 RPC node as a variable:

SNAP_RPC=https://okp4-rpc.42cabi.net:443

Specify variables LATEST_HEIGHT, BLOCK_HEIGHT, TRUST_HASH

LATEST_HEIGHT=$(curl -s $SNAP_RPC/block | jq -r .result.block.header.height); \
BLOCK_HEIGHT=$((LATEST_HEIGHT - 2000)); \
TRUST_HASH=$(curl -s "$SNAP_RPC/block?height=$BLOCK_HEIGHT" | jq -r .result.block_id.hash); \
echo $LATEST_HEIGHT $BLOCK_HEIGHT $TRUST_HASH

Specify peer variable, add peer to persistent_peers:

peers="[email protected]:28656"
sed -i -e "s/^persistent_peers *=.*/persistent_peers = \"$peers\"/" ~/.okp4d/config/config.toml

Add SNAP_RPC, BLOCK_HEIGHT and TRUST_HASH in config.toml:

sed -i.bak -E "s|^(enable[[:space:]]+=[[:space:]]+).*$|\1true| ; \
s|^(rpc_servers[[:space:]]+=[[:space:]]+).*$|\1\"$SNAP_RPC,$SNAP_RPC\"| ; \
s|^(trust_height[[:space:]]+=[[:space:]]+).*$|\1$BLOCK_HEIGHT| ; \
s|^(trust_hash[[:space:]]+=[[:space:]]+).*$|\1\"$TRUST_HASH\"| ; \
s|^(seeds[[:space:]]+=[[:space:]]+).*$|\1\"\"|" $HOME/.okp4d/config/config.toml

Restart okp4d and see the logs:

sudo systemctl restart okp4d && sudo journalctl -u okp4d -f -o cat

We will also consider the possibility of starting a node using a copy of the database (SnapShot):

The update (snapshot) occurs every 24 hours

Installing the lz4 utility :

cd ~
sudo apt update
sudo apt install lz4 -y

Stop OKP4 :

sudo systemctl stop okp4d 

Copy the priv_validator_state file.json and and clear all uploaded blockchain data (!!!!! make a backup of the priv_validator_key.json file !!!!!):

cp $HOME/.okp4d/data/priv_validator_state.json $HOME/.okp4d/priv_validator_state.json.backup
okp4d tendermint unsafe-reset-all --home $HOME/.okp4d --keep-addr-book

Download and install Snapshot :

SNAP_NAME=$(curl -s https://snapshots.42cabi.net/ | egrep -o ">okp4_snap.*\.tar.lz4" | tr -d ">")
curl https://snapshots.42cabi.net/${SNAP_NAME} | lz4 -dcm - | tar -xf - -C $HOME/.okp4d

Moving the priv_validator_state.json file:

mv $HOME/.okp4d/priv_validator_state.json.backup $HOME/.okp4d/data/priv_validator_state.json

Restart okp4d and see the logs:

sudo systemctl restart okp4d && sudo journalctl -u okp4d -f -o cat