Cosmovisor

CosmosHub | Cosmovisor

For mainnet, it’s recommended to use Cosmovisor to run your node. If you’ve not used it before, then run it during a testnet to check you can get it set up correctly.

Setting up Cosmovisor is relatively straightforward. However, it does expect certain environment variables and folder structures to be set.

Cosmovisor allows you to download binaries ahead of time for chain upgrades, meaning that you can do zero (or close to zero) downtime chain upgrades. It’s also useful if your local timezone means that a chain upgrade will fall at a bad time.

Rather than having to do stressful ops tasks late at night, it’s always better if you can automate them away, and that’s what Cosmovisor tries to do.


Install

First, go and get cosmovisor (recommended approach):

go install github.com/cosmos/cosmos-sdk/cosmovisor/cmd/cosmovisor@v1.0.0

Your installation can be confirmed with:

which cosmovisor

This will return something like:

/home/<your-user>/go/bin/cosmovisor

Add environment variables to your shell

In the .zshrc file, usually located at ~/.zshrc, add:

export DAEMON_NAME=gaiad
export DAEMON_HOME=$HOME/.gaia

Then source your profile to have access to these variables:

source ~/.zshrc

You can confirm success like so:

echo $DAEMON_NAME

It should return gaiad.


Set up folder structure

Cosmovisor expects a certain folder structure:

.
├── current -> genesis or upgrades/<name>
├── genesis
│   └── bin
│       └── $DAEMON_NAME
└── upgrades
    └── <name>
        └── bin
            └── $DAEMON_NAME

Don’t worry about current - that is simply a symlink used by Cosmovisor. The other folders will need setting up, but this is easy:

mkdir -p ~/.gaia/cosmovisor/genesis/bin
mkdir -p ~/.gaia/cosmovisor/upgrades

Set up genesis binary

Cosmovisor needs to know which binary to use at genesis. We put this in $DAEMON_HOME/cosmovisor/genesis/bin.

First, find the location of the binary you want to use:

which gaiad

Then use the path returned to copy it to the directory Cosmovisor expects. Let’s assume the previous command returned /home/your-user/go/bin/gaiad:

cp $GOPATH/bin/gaiad ~/.gaia/cosmovisor/genesis/bin

Once you’re done, check the folder structure looks correct using a tool like tree.

Set up service

Commands sent to Cosmovisor are sent to the underlying binary. For example, cosmovisor version is the same as typing gaiad version.

Nevertheless, just as we would manage junod using a process manager, we would like to make sure Cosmovisor is automatically restarted if something happens, for example an error or reboot.

First, create the service file:

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

Change the contents of the below to match your setup - cosmovisor is likely at ~/go/bin/cosmovisor regardless of which installation path you took above, but it’s worth checking.

[Unit]
Description=cosmovisor
After=network-online.target
[Service]
User=whispernode
ExecStart=/home/whispernode/go/bin/cosmovisor start
Restart=always
RestartSec=3
LimitNOFILE=4096
Environment="DAEMON_NAME=gaiad"
Environment="DAEMON_HOME=/home/whispernode/.gaia"
Environment="DAEMON_ALLOW_DOWNLOAD_BINARIES=false"
Environment="DAEMON_RESTART_AFTER_UPGRADE=true"
Environment="DAEMON_LOG_BUFFER_SIZE=512"
Environment="UNSAFE_SKIP_BACKUP=true"
[Install]
WantedBy=multi-user.target

Note also that we set buffer size explicitly because of a live bug in Cosmovisor before version v1.0.0. If you are using v1.0.0, you may omit that line.

In addition, the same issue can be fixed by reducing the log via env variable. If you are unsure, ask on Discord.

State Syncing the node

State Sync is an efficient and fast way to bootstrap a new node, and it works by replaying larger chunks of application state directly rather than replaying individual blocks or consensus rounds. For more information, see Tendermint's State Sync docs.

To enable state sync, visit an explorer to get a recent block height and corresponding hash. A node operator can choose any height/hash in the current bonding period, but as the recommended snapshot period is 1000 blocks, it is advised to choose something close to current height - 1000.

With the block height and hash selected, update the configuration in ~/.gaia/config/config.toml to set enable = true, and populate the trust_height and trust_hash. Node operators can configure the rpc servers to a preferred provider, but there must be at least two entries. It is important that these are two rpc servers the node operator trusts to verify component parts of the chain state. While not recommended, uniqueness is not currently enforced, so it is possible to duplicate the same server in the list and still sync successfully.

#######################################################
###         State Sync Configuration Options        ###
#######################################################
[statesync]
# State sync rapidly bootstraps a new node by discovering, fetching, and restoring a state machine
# snapshot from peers instead of fetching and replaying historical blocks. Requires some peers in
# the network to take and serve state machine snapshots. State sync is not attempted if the node
# has any local state (LastBlockHeight > 0). The node will have a truncated block history,
# starting from the height of the snapshot.
enable = true

# RPC servers (comma-separated) for light client verification of the synced state machine and
# retrieval of state data for node bootstrapping. Also needs a trusted height and corresponding
# header hash obtained from a trusted source, and a period during which validators can be trusted.
#
# For Cosmos SDK-based chains, trust_period should usually be about 2/3 of the unbonding time (~2
# weeks) during which they can be financially punished (slashed) for misbehavior.
rpc_servers = "https://cosmos-rpc.polkachu.com:443,https://rpc-cosmoshub-ia.cosmosia.notional.ventures:443,https://rpc.cosmos.network:443"
trust_height = 8959784
trust_hash = "3D8F12EA302AEDA66E80939F7FC785206692F8B6EE6F727F1655F1AFB6A873A5"
trust_period = "168h0m0s"

Start Cosmovisor

Finally, enable the service and start it.

sudo -S systemctl daemon-reload
sudo -S systemctl enable cosmovisor
sudo systemctl start cosmovisor

Check it is running using:

sudo systemctl status cosmovisor

If you need to monitor the service after launch, you can view the logs using:

journalctl -u cosmovisor -f

Last updated

Logo

Made with ❤️ by WhisperNode // © 2024.