Skip to content

Instantly share code, notes, and snippets.

@mintthemoon
Last active July 3, 2022 21:46
Show Gist options
  • Save mintthemoon/a3d12f8a8b0d6224ef4b9a78accfba02 to your computer and use it in GitHub Desktop.
Save mintthemoon/a3d12f8a8b0d6224ef4b9a78accfba02 to your computer and use it in GitHub Desktop.
cosmos-statesync-bootstrap
#!/bin/bash
# bootstrap a cosmos sdk node with statesync.
# detects an existing home dir or handles initialiation if config is not present.
# configure with env vars, defaults are for kujira mainnet.
#
# required packages: jq, curl, sed
name="${CHAIN_NAME:-kujira}"
id="${CHAIN_ID:-kaiyo-1}"
home="${CHAIN_HOME:-${HOME}/.kujira}"
daemon="${CHAIN_DAEMON:-kujirad}"
rpc="${CHAIN_RPC:-https://rpc-${name}.mintthemoon.xyz:443}"
seeds="${CHAIN_SEEDS:-128cc2a74351ffd089989ddad07b17c4f14d2859@23.175.49.176:20656}"
peers="${CHAIN_PEERS}"
genesis="${CHAIN_GENESIS:-https://raw.githubusercontent.com/Team-Kujira/networks/master/mainnet/kaiyo-1.json}"
set -e
echo "cosmos bootstrap: $name ($id) at $home"
daemon_args="--home $home"
if [[ ! -f "$home/config/config.toml" ]]
then
moniker="${CHAIN_MONIKER:-node-$RANDOM}"
echo "initializing config ($moniker)"
"$daemon" $daemon_args init --chain-id "$id" "$moniker"
curl -fsSL -o "$home/config/genesis.json" "$genesis"
fi
if [[ -d "$home/data/state.db" ]]
then
echo "cannot statesync with existing data!"
echo "backup any private keys you need, reset your data, and try again:"
echo " $daemon $daemon_args tendermint unsafe-reset-all"
exit 1
fi
if [[ ! -z "$seeds" ]]
then
echo "using seeds: $seeds"
daemon_args="$daemon_args --p2p.seeds $seeds"
elif [[ ! -z "$peers" ]]
then
echo "using peers: $peers"
daemon_args="$daemon_args --p2p.persistent_peers $peers"
else
echo "warning: no seeds or peers configured for bootstrap"
fi
echo "modifying config.toml for statesync from $rpc"
latest_height=$(curl -s $rpc/block | jq -r .result.block.header.height)
trust_height=$((latest_height - 2000))
trust_hash=$(curl -s "$rpc/block?height=$trust_height" | jq -r .result.block_id.hash)
sed -i.bak -E "s|^(enable[[:space:]]+=[[:space:]]+).*$|\1true| ; \
s|^(rpc_servers[[:space:]]+=[[:space:]]+).*$|\1\"$rpc,$rpc\"| ; \
s|^(trust_height[[:space:]]+=[[:space:]]+).*$|\1$trust_height| ; \
s|^(trust_hash[[:space:]]+=[[:space:]]+).*$|\1\"$trust_hash\"|" "$home/config/config.toml"
echo "bootstrap will halt at height $latest_height"
daemon_args="$daemon_args --halt-height $latest_height"
echo "beginning statesync"
"$daemon" $daemon_args start
mv "$home/config/config.toml.bak" "$home/config/config.toml"
echo "restored pre-sync config.toml"
echo "statesync complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment