Skip to content

Instantly share code, notes, and snippets.

@jhelison
Last active February 4, 2025 17:49
Show Gist options
  • Save jhelison/b218034ad61f46f308aeb517357dff10 to your computer and use it in GitHub Desktop.
Save jhelison/b218034ad61f46f308aeb517357dff10 to your computer and use it in GitHub Desktop.
#!/bin/bash
sudo systemctl stop unicornd
NODE_HOME=~/.unicornd
# Backup the old files
cp $NODE_HOME/config/genesis.json ~
cp $NODE_HOME/config/node_key.json ~
# Clean the current chain state
rm -r $NODE_HOME/wasm
rm -r $NODE_HOME/data
# Endpoints
PRIMARY_ENDPOINT=10.0.2.205:26657
SECONDARY_ENDPOINT=10.0.2.230:26657
PERSISTENT_PEERS="95574f14542640287f1df51a442e153707437050@10.0.2.205:26656,377870a3896a3ef06f89fc48ae53deea790383e0@10.0.2.230:26656,d4915becd47553e1fffe0c8c66dc89ee79fae99b@10.0.2.156:26656"
# Configure state-sync
TRUST_HEIGHT_DELTA=500
LATEST_HEIGHT=$(curl -s "$PRIMARY_ENDPOINT"/block | jq -r ".block.header.height")
if [[ "$LATEST_HEIGHT" -gt "$TRUST_HEIGHT_DELTA" ]]; then
SYNC_BLOCK_HEIGHT=$(($LATEST_HEIGHT - $TRUST_HEIGHT_DELTA))
else
SYNC_BLOCK_HEIGHT=$LATEST_HEIGHT
fi
# Get the sync block hash
SYNC_BLOCK_HASH=$(curl -s "$PRIMARY_ENDPOINT/block?height=$SYNC_BLOCK_HEIGHT" | jq -r ".block_id.hash")
# Enable state sync
sed -i.bak -e "s|^enable *=.*|enable = true|" $NODE_HOME/config/config.toml
sed -i.bak -e "s|^rpc-servers *=.*|rpc-servers = \"$PRIMARY_ENDPOINT,$SECONDARY_ENDPOINT\"|" $NODE_HOME/config/config.toml
sed -i.bak -e "s|^db-sync-enable *=.*|db-sync-enable = false|" $NODE_HOME/config/config.toml
sed -i.bak -e "s|^trust-height *=.*|trust-height = $SYNC_BLOCK_HEIGHT|" $NODE_HOME/config/config.toml
sed -i.bak -e "s|^trust-hash *=.*|trust-hash = \"$SYNC_BLOCK_HASH\"|" $NODE_HOME/config/config.toml
# Set the PERSISTENT_PEERS
sed -i -e "/persistent-peers =/ s^= .*^= \"$PERSISTENT_PEERS\"^" $NODE_HOME/config/config.toml
# Recreate the validator state
mkdir -p $NODE_HOME/data
echo '{
"height": "0",
"round": 0,
"step": 0
}' > $NODE_HOME/data/priv_validator_state.json
# Restore the old genesis
cp ~/genesis.json $NODE_HOME/config/genesis.json
cp ~/node_key.json $NODE_HOME/config/node_key.json
# Start the node
sudo systemctl start unicornd
journalctl -fu unicornd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment