Solutions
No Results

Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Solution

Use Quick-Algo to Start Running an Algorand Node in Under 1 Minute

Overview

Table of Contents

  1. Install a Node: Covers the basic steps needed to install an Algorand Node. Additionally, this section introduces the shell script I wrote that automates the process of running an Algorand Node.
  2. Debugging and Interacting with your Node: Discusses common errors you may run into and also introduces some useful Algod API endpoints that you can use for debugging and interacting with the Algorand node.
  3. Catching up to the Network: Introduces the concept of “Fast Catch-ups” and discusses several ways to keep your node up to date.
  4. Using the quick-algo shellscript: Discusses how quick-algo can be used to begin running a fully-customizable Algorand Node in under one minute.

Install a Node

  1. Change into your $HOME directory. cd $HOME , cd ~ , or even just cd should do the trick.
  2. Create a new directory called node and change to that directory. If you are using Zsh, you can run take node to both create and change to the node directory.
  3. Create an environment variable called ALGORAND_DATA. Set the environment variable equal to $(pwd)/data. Here is the syntax for creating this environment variable: export ALGORAND_DATA=$(pwd)/data
  4. Install the updater script from Algorand’s GitHub page. This script is used to pull down the latest package update from AWS S3 and install it. curl https://raw.githubusercontent.com/algorand/go-algorand-doc/master/downloads/installers/update.sh -O
  5. Ensure that the updater script is executable: chmod 544 update.sh
  6. Execute the update.sh script to install your node. When executing for the first time, this may take a little while: ./update.sh -i -c stable -p ~/node -d $ALGORAND_DATA -n
  7. Add goal to your PATH. Use export PATH=$PATH:(pwd)
  8. Start your node by running goal node start
  9. Check the status of your node by running goal node status

A script that automates this process is available on GitHub:

#! /bin/bash

main () {
    echo "Executing script..."
    run
    echo "Finished."
}

run () {

    read -p 'Directory to Install Node [any valid directory : $HOME]: ' installation_loc # variable of where to install algorand node
    read -p 'Algorand Node Type [mainnet/testnet : mainnet]: ' node_type # type of algorand node to run
    read -p 'Run Fast Catchup? Read about Fast Catchup Here (https://developer.algorand.org/docs/run-a-node/setup/install/#sync-node-network-using-fast-catchup) [yes/no : no]: ' fast_catchup # run fast catchup during installation
    read -p 'Automate Fast Catchups [yes/no : yes]: ' automate_catchups # automate catchups?


    if [ -z "$installation_loc" ]; then # if $installation_loc is empty or not set
        installation_loc=$HOME
    fi;

    if [ -z "$node_type" ]; then # if $node_type is empty or not set
        node_type='mainnet'
    fi;

    if [ -z "$fast_catchup" ]; then # if $fast_catchup is empty or not set
        fast_catchup='no'
    fi;

    if [ -z "$automate_catchups" ]; then # if $automate_catchups is empty or not set
        automate_catchups='yes'
    fi;


    echo "Installing in:" $installation_loc
    change_dir $installation_loc # change to $HOME dir.
    FOLDER=$installation_loc/node

    if [ -d "$FOLDER" ]; then
        # if the node dir already exists, do not mkdir. Just cd into it.
        cd $installation_loc/node
    else
        # if the node dir does not exist, mkdir and then cd into it.
        mk_node_dir
    fi

    # create ALGORAND_DATA env var in installation dir
    create_env_var $installation_loc

    # install the update.sh script
    install https://raw.githubusercontent.com/algorand/go-algorand-doc/master/downloads/installers/update.sh

    # change the permissions of the update.sh file
    make_executable $installation_loc/node/update.sh
    # run the update.sh script
    run_update_script
    # start the node

    run_node $node_type

    # check the status of the node
    check_status

    if [[ $fast_catchup == "yes" ]]; then
        # do fast catchup

        # quietly download last catchpoint, extract the plaintext from the page w/ sed
        checkpoint=$(wget -qO- https://algorand-catchpoints.s3.us-east-2.amazonaws.com/channel/$node_type/latest.catchpoint | sed -e 's/<[^>]*>//g')

        cd $installation_loc/node
        ./goal node catchup $checkpoint -d $ALGORAND_DATA

        echo "Running fast catchup... This may take a few minutes."
    fi;

    if [[ $automate_catchups == "yes" ]]; then
        crontab -l >> $installation_loc/node/catchup_cron_job
        echo "00 * * * * $installation_loc/node/update.sh -i -c stable -d $ALGORAND_DATA > $installation_loc/node/sync.log 2>&1" >> $installation_loc/node/catchup_cron_job
        crontab $installation_loc/node/catchup_cron_job
    fi;
}

change_dir () {
    # change directory to $1
    # $1 must be a valid directory that is a subfolder of $(pwd)
    cd $1
}

mk_node_dir() {
    # make the node directory and cd into it

    mkdir node && cd node
    return 1
}

create_env_var() {
    # create ALGORAND_DATA env var
    # This tells the Algorand Node where to store data related to our node.

    if [ "$node_type" == 'mainnet' ]; then
        export ALGORAND_DATA=$1/node/data
    elif [ "$node_type" == 'testnet' ]; then
        export ALGORAND_DATA=$1/node/testnetdata
    fi;
    echo "Algorand Data Directory:" $ALGORAND_DATA
    return 1
}

install() {
    # Install $1, where $1 is a valid link to a downloadable file/folder.

    curl $1 -O
}

make_executable() {
    # make the passed file ($1) executable

    chmod 544 $1
}

run_update_script() {
    # updates the node to the latest version
    # -n ensures the node does not automatically start

    echo "RUNNING UPDATE.SH:"
    sh -c "yes | $installation_loc/node/update.sh -i -c stable -p $installation_loc/node -d $ALGORAND_DATA -n"
    echo ''
}

run_node () {
    cd $installation_loc/node
    echo "STARTING NODE:"
    if [ "$1" == "mainnet" ]; then
        # begins running the Algorand node
        ./goal node start -d $ALGORAND_DATA
        echo ''
    elif [ "$1" == "testnet" ]; then
        if [ -d "testnetdata" ]; then
            cp genesisfiles/testnet/genesis.json $installation_loc/node/testnetdata
            ./goal node start -d testnetdata
        else 
            mkdir testnetdata
            cp genesisfiles/testnet/genesis.json $installation_loc/node/testnetdata
            ./goal node start -d testnetdata
        fi;
    else
        echo "$1 is an invalid or currently unsupported Algorand Node Type. Try: mainnet, testnet"
        exit 1;
    fi;
}

check_status () {
    # Check the status of the Algorand Node.
    # If running, will return node information.
    # If error, will return "Cannot contact Algorand node..."

    echo "CHECKING NODE STATUS:"
    cd $installation_loc/node
    ./goal node status -d $ALGORAND_DATA
    echo ''
}

main

Debugging and Interacting with your Node

Common Errors

If, when checking the status of your node with goal node status, you get an error that says:
Cannot contact Algorand node...
This means that your node is not running. This can be solved by running the following command:
goal node start
Another great way to ensure your node is running correctly is the Algorand algod REST API. The documentation goes into great detail, but here are a few that I find useful:

  • GET /v2/accounts/:address/
    curl -X GET http://$(cat $ALGORAND_DATA/algod.net)/v2/accounts/[YOUR ADDRESS]/ -H “X-Algo-API-Token: $(cat "$ALGORAND_DATA"/algod.token)"
    Given an account public key, this API call will return the account’s status, balance, and spendable amounts.

  • GET /v2/ledger/supply
    curl -X GET http://$(cat $ALGORAND_DATA/algod.net)/v2/ledger/supply -H “X-Algo-API-Token: $(cat "$ALGORAND_DATA"/algod.token)"
    Get the current supply reported by the ledger.

  • POST /v2/shutdown
    curl -X POST http://$(cat "$ALGORAND_DATA"/algod.net)/v2/shutdown -H “X-Algo-API-Token: $(cat "$ALGORAND_DATA"/algod.token)"
    Shutdown the node. Optionally, this endpoint accepts a timeout parameter that tells the node how many seconds to wait until it shuts down.

  • GET /v2/status
    curl -X GET http://$(cat "$ALGORAND_DATA"/algod.net)/v2/status -H “X-Algo-API-Token: $(cat "$ALGORAND_DATA"/algod.token)"
    Get the current status of the node.
    These are only a few of the endpoints supported by the Algorand algod API, and I suggest you check out the rest of them here. Now, let’s learn how to update our node.

Catching up to the Network

When a node first starts, it must sync with the network. This means the node will process all the blocks in the blockchain to verify them and ensure the validity of the chain, which may take some time. For future syncs, you can catch up to the network more quickly by using the Fast Catch-Up functionality.

What is Fast Catch-Up?

Fast Catch-Up is a new feature that rapidly updates a node using catchpoint snapshots. This process can sync an entire node in minutes.

How do I use Fast Catch-Up?

First, you have to retrieve the latest check-up point for the network you are running. There are three networks supported by Algorand:

  • BetaNet: This network is where new protocol-level features will be released for initial testing. Quality and features may not be final, and protocol upgrades and network restarts are common.
  • TestNet: This network mirrors the MainNet in terms of its software version, but it has test “Algos” and a different genesis block. This means that the state of accounts is different. This network is useful for developers who want to test how an application might interact with the Algorand chain.
  • MainNet: This is the default configuration, and it is the desired outcome for most users. This is the primary Algorand Network with real-value assets, including Algorand’s native currency (the “Algo”).
    The links to where you can retrieve the latest check-up point for whichever network you are running are in the Algorand Developer Documentation. After you retrieve the check-up point, run goal node catchup [INSERT CHECK-UP POINT HERE] to sync your node. So, if the check-up point was 1234#ABC, the full command would be:
    goal node catchup 1234#ABC
    Finally, check the status of your node with goal node status. It should return something similar to the following:

Last committed block: XXXX
Sync Time: 10.2s
Catchpoint: 1234#ABC
...

Automating Fast Catch-Ups

With Cron jobs, you can easily automate the process of syncing your node to the network. Here is a simple Cron job that will sync your node with the network every hour:
00 * * * * $HOME/node/update.sh -i -c stable -d $HOME/node/data > $HOME/node/sync.log 2>&1

Using the quick-algo shellscript

  1. Install the quick-algo script from GitHub. To do this, open up a terminal and type the following command: wget https://raw.githubusercontent.com/danmurphy1217/algorand-quickstart/master/quick-algo
  2. Make the quick-algo script executable by running the following command in the terminal: chmod 544 quick-algo
  3. Run the shell script by using the following command: ./quick-algo

For all of these steps, make sure you are in the same directory you downloaded the script to. Once you run the script, you will be prompted with four questions:

  1. Where you want to install the files for your Algorand node. The value passed must be a valid (already existing) directory. Default is $HOME.
  2. The type of the Algorand node you want to run. Valid values are: mainnet, testnet. Default is mainnet.
  3. Whether you want to run Fast Catchup post-installation or not. Valid values are: yes, no. Default is no.
  4. Whether you want to automate Fast Catchup. Valid values are: yes, no. Default is yes.

After answering these four questions, the installation process begins and should complete in 30-45 seconds. If you run into any problems during the installation, have any questions, or want to propose any updates, please open an issue on GitHub.

Review

In this article we learned:

  • How to Install and Run an Algorand node on our computer
  • How to use the Algorand algod REST API to interact with our node and get useful information about our account, node, and ledger.
  • How to sync our node with the network.
  • How to use quick-algo to simplify the customization of your Algorand Node, automate fast catchups, and run main and testnet nodes.

If you have any comments or questions, you can contact me in the comments or at my e-mail ([email protected]). All code is publicly hosted on GitHub. Thanks for reading!++

June 23, 2021