Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Install script] Support multiple networks #941

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
136 changes: 117 additions & 19 deletions tools/installer/full-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# DEV_NOTE: For testing purposes, you can change the branch name before merging to master.
GENESIS_BRANCH="master"
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# DEV_NOTE: For testing purposes, you can change the branch name before merging to master.
GENESIS_BRANCH="master"
# DEV_NOTE: For testing purposes, you can change the branch name before merging to master.
POCKET_NETWORK_GENESIS_BRANCH="master"


# Function to print colored output
print_color() {
COLOR=$1
Expand All @@ -23,19 +26,72 @@
fi
}

# Function to install jq if not installed
install_jq() {
if ! command -v jq &> /dev/null; then
print_color $YELLOW "Installing jq..."
if [ -f /etc/debian_version ]; then
apt-get update
apt-get install -y jq
elif [ -f /etc/redhat-release ]; then
yum update -y
yum install -y jq
else
print_color $RED "Unsupported distribution. Please install jq manually."
exit 1
fi
print_color $GREEN "jq installed successfully."
else
print_color $YELLOW "jq is already installed."
fi
}

# Function to get user input
get_user_input() {
# Ask user which network to install
echo "Which network would you like to install?"
echo "1) testnet-alpha"
echo "2) testnet-beta"
echo "3) mainnet"
read -p "Enter your choice (1-3): " network_choice
okdas marked this conversation as resolved.
Show resolved Hide resolved

case $network_choice in
1) NETWORK="testnet-alpha" ;;
2) NETWORK="testnet-beta" ;;
3) NETWORK="mainnet" ;;
*) print_color $RED "Invalid choice. Exiting."; exit 1 ;;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*) print_color $RED "Invalid choice. Exiting."; exit 1 ;;
*) print_color $RED "Invalid network choice. Exiting."; exit 1 ;;

esac

print_color $GREEN "You have chosen to install the $NETWORK network."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print_color $GREEN "You have chosen to install the $NETWORK network."
print_color $GREEN "Installing the $NETWORK network."


read -p "Enter the desired username to run poktrolld (default: poktroll): " POKTROLL_USER
POKTROLL_USER=${POKTROLL_USER:-poktroll}

read -p "Enter the node moniker (default: $(hostname)): " NODE_MONIKER
NODE_MONIKER=${NODE_MONIKER:-$(hostname)}

read -p "Enter the chain-id (default: poktroll): " CHAIN_ID
CHAIN_ID=${CHAIN_ID:-"poktroll"}
# Update URLs to use the branch constant
BASE_URL="https://raw.githubusercontent.com/pokt-network/pocket-network-genesis/${GENESIS_BRANCH}/shannon/$NETWORK"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
BASE_URL="https://raw.githubusercontent.com/pokt-network/pocket-network-genesis/${GENESIS_BRANCH}/shannon/$NETWORK"
BASE_URL="https://raw.githubusercontent.com/pokt-network/pocket-network-genesis/${POCKET_NETWORK_GENESIS_BRANCH}/shannon/$NETWORK"

SEEDS_URL="$BASE_URL/seeds"
GENESIS_URL="$BASE_URL/genesis.json"

# Download genesis.json and store it
GENESIS_FILE="/tmp/genesis.json"
curl -s -o "$GENESIS_FILE" "$GENESIS_URL"
if [ $? -ne 0 ]; then
print_color $RED "Failed to download genesis file. Please check your internet connection and try again."
exit 1
fi

# Extract chain_id from genesis.json
CHAIN_ID=$(jq -r '.chain_id' < "$GENESIS_FILE")
if [ -z "$CHAIN_ID" ]; then
print_color $RED "Failed to extract chain_id from genesis file."
exit 1
fi
print_color $GREEN "Using chain_id: $CHAIN_ID from genesis file"

# Fetch seeds from the provided URL
SEEDS_URL="https://raw.githubusercontent.com/pokt-network/pocket-network-genesis/master/shannon/alpha/testnet-validated.seeds"
SEEDS=$(curl -s "$SEEDS_URL")
if [ -z "$SEEDS" ]; then
print_color $RED "Failed to fetch seeds from $SEEDS_URL. Please check your internet connection and try again."
Expand Down Expand Up @@ -125,6 +181,7 @@
print_color $GREEN "Cosmovisor set up successfully."
}


# Function to download and set up Poktrolld
setup_poktrolld() {
print_color $YELLOW "Setting up Poktrolld..."
Expand All @@ -138,37 +195,62 @@
exit 1
fi

# Get the version genesis started from. We can't just use `latest` as the new binary won't sync from genesis.
# We need to start syncing from scratch using the version that was used when the network started.
POKTROLLD_VERSION=$(curl -s https://raw.githubusercontent.com/pokt-network/pocket-network-genesis/master/shannon/alpha/testnet-validated.init-version)
# Extract the version from genesis.json using jq
POKTROLLD_VERSION=$(jq -r '.app_version' < "$GENESIS_FILE")
print_color $YELLOW "Detected version from genesis: $POKTROLLD_VERSION"

if [ -z "$POKTROLLD_VERSION" ]; then
print_color $RED "Failed to extract version information from genesis file."
exit 1
fi

# Use the direct download link for the correct release
RELEASE_URL="https://github.com/pokt-network/poktroll/releases/download/${POKTROLLD_VERSION}/poktroll_linux_${ARCH}.tar.gz"
# Construct the release URL with proper version format
RELEASE_URL="https://github.com/pokt-network/poktroll/releases/download/v${POKTROLLD_VERSION}/poktroll_linux_${ARCH}.tar.gz"
print_color $YELLOW "Attempting to download from: $RELEASE_URL"

# Download and extract directly as the POKTROLL_USER
sudo -u "$POKTROLL_USER" bash << EOF
mkdir -p \$HOME/.poktroll/cosmovisor/genesis/bin
curl -L "$RELEASE_URL" | tar -zxvf - -C \$HOME/.poktroll/cosmovisor/genesis/bin
if [ \$? -ne 0 ]; then
echo "Failed to download or extract binary"
exit 1
fi
chmod +x \$HOME/.poktroll/cosmovisor/genesis/bin/poktrolld
ln -sf \$HOME/.poktroll/cosmovisor/genesis/bin/poktrolld \$HOME/bin/poktrolld
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to these changes and likely out of scope but as this is the first time I'm reading this file, forgive my curiosity. 😅

Suggested change
ln -sf \$HOME/.poktroll/cosmovisor/genesis/bin/poktrolld \$HOME/bin/poktrolld
ln -sf \$HOME/.poktroll/cosmovisor/genesis/bin/poktrolld \$HOME/.local/bin/poktrolld

My understanding is that this aligns with the XDG user directory specification, which has wide adoption [1, 2].

I'm noticing that we seem to be creating this $HOME/bin directory. I'm wondering if it would be worth using an existing & conventional directory which is already included in the $PATH on most modern distros (except alpine, according to chatGPT). We could then remove the append to the $HOME/.profile:

-    echo 'export PATH=\$HOME/bin:\$PATH' >> \$HOME/.profile

[1] https://unix.stackexchange.com/questions/316765/which-distributions-have-home-local-bin-in-path
[2] https://askubuntu.com/questions/14535/whats-the-local-folder-for-in-my-home-directory

source \$HOME/.profile
EOF

if [ $? -ne 0 ]; then
print_color $RED "Failed to set up Poktrolld"
exit 1
fi

print_color $GREEN "Poktrolld set up successfully."
}

# Function to configure Poktrolld
configure_poktrolld() {
print_color $YELLOW "Configuring Poktrolld..."

# Ask for confirmation to download the genesis file
GENESIS_URL="https://raw.githubusercontent.com/pokt-network/pocket-network-genesis/master/shannon/alpha/testnet-validated.json"
print_color $YELLOW "The script will download the genesis file from:"
# Ask for confirmation to use the downloaded genesis file
print_color $YELLOW "The script has downloaded the genesis file from:"
print_color $YELLOW "$GENESIS_URL"
read -p "Are you OK with downloading and using this genesis file? (y/N): " confirm_genesis
read -p "Are you OK with using this genesis file? (y/N): " confirm_genesis
if [[ ! $confirm_genesis =~ ^[Yy] ]]; then
print_color $RED "Genesis file download cancelled. Exiting."
print_color $RED "Genesis file usage cancelled. Exiting."

Check warning on line 241 in tools/installer/full-node.sh

View workflow job for this annotation

GitHub Actions / misspell

[misspell] tools/installer/full-node.sh#L241

"cancelled" is a misspelling of "canceled"
Raw output
./tools/installer/full-node.sh:241:45: "cancelled" is a misspelling of "canceled"
exit 1
fi

# Detect external IP address
EXTERNAL_IP=$(curl -s https://api.ipify.org)
print_color $YELLOW "Detected external IP address: $EXTERNAL_IP"
read -p "Is this your correct external IP address? (Y/n): " confirm_ip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is anyone going to say no? Considering just commenting this out altogether

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect there might be cases when the discovered IP address is not correct and requires adjustment.

if [[ $confirm_ip =~ ^[Nn] ]]; then
read -p "Please enter your external IP address: " custom_ip
EXTERNAL_IP=${custom_ip:-$EXTERNAL_IP}
fi

sudo -u "$POKTROLL_USER" bash << EOF
source \$HOME/.profile

Expand All @@ -177,12 +259,9 @@
echo "Poktrolld version: \$POKTROLLD_VERSION"

poktrolld init "$NODE_MONIKER" --chain-id="$CHAIN_ID" --home=\$HOME/.poktroll
curl -o \$HOME/.poktroll/config/genesis.json $GENESIS_URL
if [ \$? -ne 0 ]; then
echo "Failed to download genesis file. Please check your internet connection and try again."
exit 1
fi
cp "$GENESIS_FILE" \$HOME/.poktroll/config/genesis.json
sed -i -e "s|^seeds *=.*|seeds = \"$SEEDS\"|" \$HOME/.poktroll/config/config.toml
sed -i -e "s|^external_address *=.*|external_address = \"$EXTERNAL_IP:26656\"|" \$HOME/.poktroll/config/config.toml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
sed -i -e "s|^external_address =.|external_address = "$EXTERNAL_IP:26656"|" $HOME/.poktroll/config/config.toml

EOF
if [ $? -eq 0 ]; then
print_color $GREEN "Poktrolld configured successfully."
Expand Down Expand Up @@ -223,10 +302,28 @@
print_color $GREEN "Systemd service set up and started successfully."
}

# Function to check if ufw is installed and open port 26656. We need to open the port to keep the network healthy.
# By default, at least on Debian vultr, this port is not open to the internet.
configure_ufw() {
if command -v ufw &> /dev/null; then
print_color $YELLOW "ufw is installed."
read -p "Do you want to open port 26656 for p2p communication? (Y/n): " open_port
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdyt about printing a line about how to undo this in the future?

if [[ $open_port =~ ^[Yy] ]]; then
ufw allow 26656
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to check the existing rules and only add one if it's not already present; otherwise, it seems that duplicate rules accumulate. See this chatGPT convo.

print_color $GREEN "Port 26656 opened successfully."
else
print_color $YELLOW "Port 26656 not opened."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does landing in this conditional branch really guarantee that the port is not open? If not, I would suggest we rephrase the message to be more precise (e.g. "Firewall rules not modified").

fi
else
print_color $YELLOW "ufw is not installed. Skipping port configuration."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print_color $YELLOW "ufw is not installed. Skipping port configuration."
print_color $YELLOW "ufw is not installed. Skipping firewall configuration."

fi
}

# Main function
main() {
print_color $GREEN "Welcome to the Poktroll Full Node Install Script!"
check_root
install_jq
get_user_input
create_user
install_dependencies
Expand All @@ -235,7 +332,8 @@
setup_poktrolld
configure_poktrolld
setup_systemd
print_color $GREEN "Poktroll Full Node installation completed successfully!"
configure_ufw
print_color $GREEN "Poktroll Full Node installation for $NETWORK completed successfully!"
print_color $YELLOW "You can check the status of your node with: sudo systemctl status cosmovisor.service"
print_color $YELLOW "View logs with: sudo journalctl -u cosmovisor.service -f"
}
Expand Down
Loading