tendermint

Introduction to Blockchain and Proof of Stake with Tendermint

2966 VIEWS

Over time, the technology behind Blockchain has evolved, and new consensus algorithms have been proposed to solve the issue of reaching agreement in a group of servers or nodes. One of those algorithms is the Proof of Stake where each next block is chosen via various combinations of random selection and weight factors instead of mining a hard and computationally expensive hash function. In this article, we are going to use Tendermint as the blockchain consensus engine and deploy an example application in a testnet.

First: What is a blockchain?

A blockchain, in layman’s terms, is a database that is distributed across multiple computers at the same time. In order to update this database, new sets of recordings, or “blocks,” are added to it. Each block consists of information like a timestamp and a link to the previous block. That’s why it’s called a blockchain. One particular characteristic of this database is that it’s not managed by any particular body; instead, everyone in the network gets a copy of the whole database. Old blocks are preserved forever and new blocks are added to the chain irreversibly, making it impossible to manipulate by faking documents, transactions and other information.

Tendermint

Tendermint is a byzantine-fault tolerant state machine replication or Blockchain technology. It’s a blockchain consensus engine and a generic application interface. It works with participant nodes called validators, which take turns proposing blocks of transactions and voting on them. New blocks are committed in a chain, with one block at each height. A block may fail to be committed — in which case the protocol moves to the next round, and a new validator gets to propose a block for that height.

To successfully commit a block, a series of voting stages are required to pass, and at least 2/3 of validators need to approve them. There are various reasons why the validators may fail to commit a block. For example, the current proposer may be offline, or the network may be slow. With various rules, Tendermint guarantees the terms are not violated, and it will never commit conflicting blocks at the same height. Below is an illustration of the consensus engine with the voting rounds (a.k.a. polka dance rounds).


source https://tendermint.com/docs

Deploying a Tendermint testnet and an example application

Let’s see how we can install and deploy a Tendermint node and test an example application.

Follow the instructions on how to install Tendermint from source:

https://tendermint.com/docs/introduction/install.html#from-source

Install the dependencies:

make get_tools
make get_tools
make get_vendor_deps
make install
make build

Run the following command inside the build folder to initialize a test network.

tendermint init

This will create the following folder structure:

$ tree -L 3 .                                                                                    
.
├── config
│   ├── config.toml
│   ├── genesis.json
│   ├── node_key.json
│   └── priv_validator.json
├── data
└── tendermint

Next, we need to set up our node environment, and especially the TMHOME env variable. It’s best to assign it to the build folder so we can test inside the project:

export TMHOME=/Users/itspare/go/src/github.com/tendermint/tendermint/build/

We also need to update one config section to not allow empty blocks to be committed, as it would be easier to track the transactions in the console. In config.toml change the following option to false:

# EmptyBlocks mode and possible interval between empty blocks
create_empty_blocks = false

In order to run Tendermint core, we need an active ABCI application where we can test our blockchain. Luckily for us, there are some ready-made examples we can use.

Install the abci-cli tool first

$ make install_abci

Then, start the KVStore application that acts as a key-value database:

$ abci-cli kvstore
I[6116-11-06|16:13:37.901] Starting ABCIServer                          module=abci-server impl=ABCIServer
I[6116-11-06|16:13:37.902] Waiting for new connection...                module=abci-server 

Now we are ready to initiate the consensus engine:

$ ./tendermint node --proxy_app=kvstore  

You will see some initialization logs, but after that, the engine is up and running. Let’s test with some transactions.

See the current status:

$ curl -s localhost:26657/status
{
  "jsonrpc": "2.0",
  "id": "",
  "result": {
    "node_info": {
      "protocol_version": {
        "p2p": "4",
        "block": "7",
        "app": "1"
      },
      "id": "5b883f077d1dc8da6d8268c9d4ece4562ec62f43",
      "listen_addr": "tcp://0.0.0.0:26656",
      "network": "test-chain-PzOgTg",
      "version": "0.26.0",
      "channels": "4020212223303800",
      "moniker": "C02PV2X1FVH6",
      "other": {
        "tx_index": "on",
        "rpc_address": "tcp://0.0.0.0:26657"
      }
    },
    "sync_info": {
      "latest_block_hash": "EAF9820AB7E142779915ACF19B7F9F05B1AF6706636DDCFC79536DDBF8FF8F30",
      "latest_app_hash": "0000000000000000",
      "latest_block_height": "2",
      "latest_block_time": "2018-11-06T16:41:11.52468Z",
      "catching_up": false
    },
    "validator_info": {
      "address": "11AE0110DE4BE1F05A0AC6F087CB0AE4C8089105",
      "pub_key": {
        "type": "tendermint/PubKeyEd25519",
        "value": "ZlYMi3tIjAL6NJaMcd2JXg70hHaoG8pOkfbEPj3cUFQ="
      },
      "voting_power": "10"
    }
  }
}% 

Submit a value to store:

(No spaces in the transaction payload)

$ curl -s 'localhost:26657/broadcast_tx_commit?tx="message=HelloWorld"'
{
  "jsonrpc": "2.0",
  "id": "",
  "result": {
    "check_tx": {
      "gasWanted": "1"
    },
    "deliver_tx": {
      "tags": [
        {
          "key": "YXBwLmNyZWF0b3I=",
          "value": "Q29zbW9zaGkgTmV0b3dva28="
        },
        {
          "key": "YXBwLmtleQ==",
          "value": "bWVzc2FnZQ=="
        }
      ]
    },
    "hash": "79D32A1CD0BB698298B49E03019CFC62000F34EDAA2DA170335BCE411BBD6870",
    "height": "5"
  }
}%   

Query a value from store:

$ curl -s 'localhost:26657/abci_query?data="message"'                  
{
  "jsonrpc": "2.0",
  "id": "",
  "result": {
    "response": {
      "log": "exists",
      "key": "bWVzc2FnZQ==",
      "value": "SGVsbG9Xb3JsZA=="
    }
  }
}%

The key and values are base64 encoded.

Overview

In this article, we’ve learned about Blockchain and the Tendermint Consensus engine. We have deployed an example blockchain application and tested how it behaves. The topic of Blockchain and consensus algorithms have created tons of new opportunities for decentralized computing and solving old problems with a new approach. Stay tuned for more exciting articles discussing those technologies.


Theo Despoudis is a Senior Software Engineer, a consultant and an experienced mentor. He has a keen interest in Open Source Architectures, Cloud Computing, best practices and functional programming. He occasionally blogs on several publishing platforms and enjoys creating projects from inspiration. Follow him on Twitter @nerdokto. Theo is a regular contributor at Fixate IO.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu
Skip to toolbar