Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Algorand Consensus Protocol

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

Algorand is a public blockchain and protocol that aims to deliver decentralization, scale and security for

all participants. It provides various unique features like ASAs, Atomic Transfers, Stateless & Stateful
Smart Contracts and much more.

Algorand Consensus Protocol: Pure Proof of Stake (PPoS)

How it works:

Algorand’s consensus protocol works by selecting a block proposer and a set of voting committees at
each block round, to propose a block and validate the proposal, respectively. The proposer and
committees are randomly chosen from the pool of all token holders (the accounts that hold algos), and
the likelihood of being chosen is proportional to the account’s stake in the network (i.e. how many algos
it has relative to the whole).
Detailed Explanation of Consensus
Functionalities in Algorand on Layer 1
Algorand provides a standalone daemon algorand-indexer that reads committed blocks from the
Algorand blockchain and maintains a local database of transactions and accounts that are searchable
and indexed.
Algorand provides three command-line utilities packaged with Algorand node software: goal,
kmd, and algokey.

goal is the primary tool for operating a node and it also contains functionality to manage keys,
sign and send transactions, create assets, and perform many of the same or similar functions that
are available in the SDKs. Although not required to build an application, developers who run
nodes may find it useful to achieve some level of fluency in goal as a complementary tool
during testing and validation. goal is required to setup more advanced testing environments
using private networks.
kmd is the CLI for the Algorand Key Management daemon and algokey is a standalone utility
for generating Algorand accounts and for signing transactions. It is often used as a lightweight
offline client for secure key signing.

Algorand Networks
Mainnet

Testnet

Faucet link: https://bank.testnet.algorand.network/


Algorand Smart Contracts
Setting up development environment

There are 3 ways:

1. Sandbox

2. Third-party API services

3. Run your Algorand node

Sandbox:

The sandbox allows developers to create local, private networks. Moreover, we can quickly remove a
network, reset its state, or spin up a new network. The only requirement for using the Algorand sandbox
is installing Docker and Docker-Compose.
Ubuntu and macOS

Make sure the docker daemon is running and docker-compose is installed.

Open a terminal and run:

git clone https://github.com/algorand/sandbox.git

In whatever local directory the sandbox should reside. Then:

cd sandbox
./sandbox up

This will run the sandbox shell script with the default configuration.

Run the test command for examples of how to interact with the environment:

./sandbox test

A successful node installation will print a list of prefunded accounts. Here's


an example of the sandbox output.

# Available accounts
./sandbox goal account list

# [offline] HCNMMIL3MKYILOLUO74NF6OPCJU4RU7IE5PX6JYBIT5YHAMQIVO5YADHMU
HCNMMIL3MKYILOLUO74NF6OPCJU4RU7IE5PX6JYBIT5YHAMQIVO5YADHMU 1000000000000000
microAlgos

To connect to the sandbox using an SDK, you can use the below connection object.

const algosdk = require('algosdk');

// create client object to connect to sandbox's algod client


const algodToken =
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
const algodServer = 'http://localhost';
const algodPort = 4001;
let algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);

Summary:

git clone https://github.com/algorand/sandbox.git


cd sandbox
./sandbox up testnet

Windows

Note: Be sure to use the latest version of Windows 10. Older versions may not work properly.

1. Install Git for Windows


2. Install and launch Docker for Windows: https://docs.docker.com/get-docker
3. Open "Git Bash" and follow the instruction for Ubuntu and macOS above, in the "Git Bash"
terminal.

Sandbox supports two primary modes of operation. By default, a private network will be created, which
is only available from the local environment. There are also configurations available for the public
networks which will attempt to connect to one of the long running Algorand networks and allow
interaction with it.
To specify which configuration to run:

./sandbox up $CONFIG

Where $CONFIG is specified as one of the configurations in the sandbox directory.

For example to run a dev mode network, run:

./sandbox up dev

To switch the configuration:

./sandbox down
./sandbox clean
./sandbox up $NEW_CONFIG

Third-party API services

Using third-party API services to access native Algorand REST APIs for the mainnet, testnet, and betanet
like service providers are Purestake and Rand Labs. Developer can access these services via an API key
and Algorand node address.

https://developer.purestake.io/

https://randlabs.io/

Run own Algorand node

Linux / OSX

Initial environment setup:

git clone https://github.com/algorand/go-algorand


cd go-algorand
./scripts/configure_dev.sh
./scripts/buildtools/install_buildtools.sh

build
make install

test
# unit tests
make test

# integration tests
make integration

style and checks


make fmt
make lint
make fix
make vet
make sanity

Running a node

Once the software is built you'll find binaries in ${GOPATH}/bin, and a data directory will be
initialized at ~/.algorand. Start your node with ${GOPATH}/bin/goal node start -d
~/.algorand, use ${GOPATH}/bin/carpenter -d ~/.algorand

Providing your own data directory

You can run a node out of other directories than ~/.algorand and join networks other than
mainnet. Just make a new directory and copy into it the genesis.json file for the network. For
example:

mkdir ~/testnet_data
cp installer/genesis/testnet/genesis.json ~/testnet_data/genesis.json
${GOPATH}/bin/goal node start -d ~/testnet_data

Genesis files for mainnet, testnet, and betanet can be found in installer/genesis/.

Detailed Explanation for Node Setup - Workspace

Set PATH:
There are two methods for installation:

The package manager method uses fixed directories and automatically updates. It has been
validated on Debian, Ubuntu, Fedora, and CentOS.

The updater script method allows manually setting data directories and requires manual updates.
It has been tested on the same Linux distributions from above, as well as on Raspbian (Raspberry
Pi 3), openSUSE Leap, Manjaro, Mageia, Alpine, and Solus.

1.) Installation with a package manager

Debian based distributions (Debian, Ubuntu, Linux Mint, ...)


sudo apt-get update
sudo apt-get install -y gnupg2 curl software-properties-common
curl -O https://releases.algorand.com/key.pub
sudo apt-key add key.pub
sudo add-apt-repository "deb [arch=amd64] https://releases.algorand.com/deb/
stable main"
sudo apt-get update

# To get both algorand and the devtools:


sudo apt-get install -y algorand-devtools

# Or, to only install algorand:


sudo apt-get install -y algorand

algod –v
sudo -u algorand -E goal account listpartkeys

Red Hat based distributions (Fedora, CentOS, ...)

Installing on Fedora and CentOS are described below.

 To install to CentOS 7, open a terminal and run the following commands.

curl -O https://releases.algorand.com/rpm/rpm_algorand.pub
sudo rpmkeys --import rpm_algorand.pub
sudo yum install yum-utils
sudo yum-config-manager --add-repo
https://releases.algorand.com/rpm/stable/algorand.repo

# To get both algorand and the devtools:


sudo yum install algorand-devtools

# Or, to only install algorand:


sudo yum install algorand

 To install to Fedora or CentOS 8 Stream, open a terminal and run the following commands.

curl -O https://releases.algorand.com/rpm/rpm_algorand.pub
sudo rpmkeys --import rpm_algorand.pub
dnf install -y 'dnf-command(config-manager)'
dnf config-manager
--add-repo=https://releases.algorand.com/rpm/stable/algorand.repo
dnf install algorand

Start Node

Installs by a package manager automatically start the node. Starting and stopping a node should
be done using systemctl commands:

sudo systemctl start algorand


sudo systemctl stop algorand

The status of the node can be checked by running:

goal node status -d /var/lib/algorand

2.) Installation with the updater script

Installing on a Mac

Verified on OSX v10.13.4 (High Sierra) and 10.15.7 (Catalina).

 Create a folder to hold the install package and files.

mkdir ~/node
cd ~/node

Download the updater script.

 curl https://raw.githubusercontent.com/algorand/go-algorand-doc/master/
downloads/installers/update.sh -O
 Ensure that your system knows it's an executable file.

chmod 544 update.sh

 Run the installer from within your node directory.

./update.sh -i -c stable -p ~/node -d ~/node/data -n

Installing on Linux

Nodes have been verified on Ubuntu, CentOS, Fedora, Raspbian (Raspberry Pi), openSUSE Leap,
Manjaro, Mageia, Alpine, and Solus. Other modern distros should work as well.

 Create a temporary folder to hold the install package and files.


mkdir ~/node
cd ~/node

Download the updater script.


wget https://raw.githubusercontent.com/algorand/go-algorand-
doc/master/downloads/installers/update.sh

 Ensure that your system knows it's an executable file.

chmod 544 update.sh

 Run the installer from within your node directory.

./update.sh -i -c stable -p ~/node -d ~/node/data –n

When the installer runs, it will pull down the latest update package from S3 and install it.

Start Node

Installs by the updater script require that the node be started manually. This can be done with the
following command:

goal node start

This will start the node and it can be verified by running:

pgrep algod

The node can be manually stopped by running:

goal node stop

Sync Node with Network

goal node status

Last committed block: 125064


Time since last block: 3.1s
Sync Time: 0.0s
Last consensus protocol:
https://github.com/algorandfoundation/specs/tree/5615adc36bad610c7f165fa2967f4
ecfa75125f0
Next consensus protocol:
https://github.com/algorandfoundation/specs/tree/5615adc36bad610c7f165fa2967f4
ecfa75125f0
Round for next consensus protocol: 125065
Next consensus protocol supported: true
Genesis ID: testnet-v1.0
Genesis hash: SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=
Via PyTeal:
Algorand provides the tealdbg command-line tool to launch an interactive session to debug smart
contracts.

To launch the debugger locally, for use with the CDT, execute the following command from a
terminal.

$ tealdbg debug program.teal


.
.
2020/08/25 14:05:38 CDT debugger listening on:
ws://localhost:9392/091d04a69152223ae84c8b40271c3d62f8490ea9b795aae95868932163
f89735
2020/08/25 14:05:38 Or open in Chrome:
2020/08/25 14:05:38 devtools://devtools/bundled/js_app.html?
experiments=true&v8only=false&ws=localhost:9392/091d04a69152223ae84c8b40271c3d
62f8490ea9b795aae95868932163f89735
SDKs Setup:

1.) Python:

Algorand provides an SDK for Python which is available as a pip package. To install the Python SDK, open
a terminal and run the following command:

pip3 install py-algorand-sdk

Create an Account on Algorand

Fund the Account


Connect Your Client

Check Your Balance

Build First Transaction

Sign First Transaction

Before the transaction is considered valid, it must be signed by a private key. Use the following code to
sign the transaction.
Submit the Transaction

The signed transaction can now be submitted to the network. wait_for_confirmation is called after
the transaction is submitted to wait until the transaction is broadcast to the Algorand blockchain and is
confirmed.

View the Transaction

To view the transaction, open AlgoExplorer or Goal Seeker and paste the transaction ID into the search
bar.

https://testnet.algoexplorer.io/

https://goalseeker.purestake.io/algorand/testnet
Set Up Your Editor/Framework

1. Using VS Code with JavaScript

Steps

 1. Download VS Code
 2. Install Extensions
 3. Debugging with VS Code
 4. Debugging JavaScript with VS Code sample

Download VS Code from this site https://code.visualstudio.com/Download

Open VS Code. Visual Studio Code includes built-in JavaScript IntelliSense, debugging, formatting, code
navigation, refactorings, and many other advanced language features.

Debugging JavaScript with VS Code samples

In this step, setting up a debug session for a JavaScript code is covered for both a standalone
code file as well as through a browser. The steps are similar for all languages.

Task: Standalone

A) Select the Debug icon on the left

B) Select from the drop-down menu to Add Config (for the current folder)…
Select Debug and Add Config

Select the desired debug method. Here the section is set to Node.js: Launch Program.

VS Code stores debug configurations in a file called launch.json inside of the .vscode folder in your code
folder. The editor should pop up with the launch.json file. Change the name and program to match your
code file to debug.
Now the debugging can start. Select your new launch configuration form the list.

Before pressing the run button, place a breakpoint in the code.

To set a breakpoint, simply click on the left margin, sometimes referred to as the gutter, on the
line to break.

Set breakpoint.

Select the run icon in the debugger pane.


Start Run and Debug.

Your breakpoint should be hit, and the program execution paused on the line of your breakpoint.
It is highlighted.

Debugging.

We are now debugging!

A) Breakpoint

B) Hover over a variable with the cursor

C) See the Data Tip with the contents

D) Navigate by selecting run (to next breakpoint), step over, step into, step out of or stop
2. Algorand Studio

Algorand Studio is a desktop IDE (Integrated Development Environment) for Algorand,


supporting both Mac and Linux systems. It can help you complete the entire process of Algorand
Smart Contract development. Using Algorand Studio you can

 Develop Algorand smart contracts in either TEAL or PyTeal with language support and integrated
compiler.
 Install and Run Algorand nodes.
 Construct transactions with support for most Algorand features, including atomic transfers,
multi-sig signing, asset creation and transfer operations, and contract executions.
 Manage keypairs and sign transactions.
 Query account and transaction information using the integrated explorer.
Make transfers
VS Code Extension for Algorand

Prerequisite

 VS Code
 Docker for Desktop (this extension uses docker to run Algorand node and Algorand compiler)

Installation

Download the built extension in the releases. Open the Extensions page in VS Code and select Install from VSIX...

TEAL & PyTeal compiler integration

1. Open a *.teal or *.pyteal file
2. Press Cmd+Shift+B to call out the compiler task
3. For the first time running the compiler, you will probably see the No build task to fun found. Configure Build
Task... notification. Click it and you will see an option Algorand Compiler: TEAL or PyTeal. Click it again and VS Code
will configure a tasks.json file for you. Switch to your .teal or .pyteal file and click Cmd+Shift+B to
compile

Algorand Panel

Algorand Panel is a dedicated interface for Algorand-related operations, accessible through a button at the left of the
bottom bar. Please make sure you have installed and started Docker before using the Algorand Panel.

 Install Algorand node (algorand/stable) - This is the official docker image for Algorand node. Click the New
Instance button and click the Algorand version to open the dropdown. You can install and manage Algorand nodes
in the Algorand Version Manager.
 Manage Algorand node instances - Click the New Instance button, enter the instance name and select a version
for algorand node to create a new instance. The extension will download the network snapshot to expedite the
sync process.
 Start a local node - Just click the Start button
 Display node logs - Shown in VS Code terminal
3. Algorand blockchain using IntelliJ IDE.

AlgoDEA IntelliJ plugin provides end to end development life-cycle on the Algorand blockchain
using IntelliJ IDE.
Using this plugin, developers can create and test both stateless and stateful smart contracts directly
from their IntelliJ IDE. Some of the other key features supported by this plugin are editor support for
TEAL & PyTeal files, ASA management, Atomic Transfers, Test account management.

The goal of this plugin is to increase developer productivity by simplifying various interactions with the
Algorand blockchain. Due to simple consistent development workflow in the IDE, developers can iterate
faster during the development stage

Account Management

Using this feature, developers can create & manage both standard and multi-signature accounts. These
accounts are easily accessible from different Algorand specific functionalities in the IDE .
Node Configuration

Developers can add or configure one or more Algorand Nodes inside their IDE. The supported node
types are custom Algorand Node, Purestake.io node and Sandbox.

TEAL Support

The plugin supports various editor level features for TEAL files. Below are the few key features
supported in the editor for the TEAL file type.

 Context specific code completion


 Syntax Highlighter and Error Highlighting
 Op-Code usage help
PyTeal Support

Developers can also create PyTeal files and use them in stateless or stateful smart contracts. With the
“PyTeal Compile” option, a PyTeal file can be easily compiled. All editor level support is enabled by the
default Python plugin in IntelliJ IDE.

Stateless Smart Contract

Developers can easily create, and test stateless smart contracts directly inside their IDE. The plugin
comes with many out-of-box stateless smart contract templates.

A dedicated user interface simplifies the creation of logic Signatures as well as sending a transaction
signed with a logic signature.
Stateful Smart Contract

The plugin supports the full lifecycle of stateful smart contracts. All types of stateful smart contract
transactions are supported through a dedicated user interface.
Payment Transaction

Using this feature, developers can easily transfer Algo or any ASA from one account address to another.
It simplifies the complexity of creating, submitting and monitoring a payment transaction through a
simplified user interface.

Atomic Transfer

The creation of an Atomic Transfer transaction is a multi-step process. With an intuitive user interface,
the plugin simplifies this process. Users can quickly create and submit Atomic Transfer transactions.
Asset Management

The plugin supports full lifecycle of the Algorand Standard Asset inside the IntelliJ IDE. A new ASA can be
easily created, managed through the “Asset Management” UI. The standard “Transfer” UI supports both
Algo and Algorand Standard Assets.

Export Transaction

Using “Export Transaction” and “Export Signed Transaction”, developers can serialize a transaction to a
JSON file which can be used for different purposes later. These options are available through every
transaction submission UI in IntelliJ IDE.

4. Algorand Builder Framework

Installation from source

git clone https://github.com/scale-it/algorand-builder.git


cd algorand-builder
yarn install
yarn build
cd packages/algob
yarn link

Finally, make sure your yarn global bin directory is in your $PATH.
Installation from NPM

yarn global add @algorand-builder/algob

Make sure your yarn bin directory is in PATH

cd ~
yarn bin

# test if the command works


algob --version

You might also like