How to write Tendermint Applications using Python
Tags: tendermint, blockchain, decentralization
Published: 2020-08-15 23:07:55

---
title: How to write Tendermint Applications using Python
published: true
date: 2019-02-08 09:35:56 UTC
tags: tendermint,blockchain,decentralization
canonical_url: https://medium.com/coinmonks/how-to-write-tendermint-applications-using-python-d8dde304e339
---

### How to write Tendermint Applications using Python, or in any other language.

![](https://cdn-images-1.medium.com/max/1024/1*phm1pnPQNE8fRcPBIw2wCw.png)<figcaption>source tendermint.com</figcaption>

> Byzantine-fault tolerant state machine replication. Or blockchain, for short.

This is the first things you see when you open the Tendermint website. Odds are you already know what a blockchain is, It’s the technology which powers Bitcoin, Ethereum and other decentralized coins you heard of. The basic premise is that people can make applications in a decentralized way, and you can write in any programming language you prefer. Maybe you are confused so let’s back up a bit.

_Tendermint is epic fast. Unlike others, you can send thousands of transactions in a second. Also, you can use it to set up private blockchains as well._

Byzantine-fault tolerant state replication machine — a really large word which makes no sense, unless you are good in decentralized systems. Let’s break up this word.

**Byzantine-fault tolerant**

It’s better explained using[the Byzantine Generals Problem](https://web.archive.org/web/20170205142845/http://lamport.azurewebsites.net/pubs/byz.pdf).

![](https://cdn-images-1.medium.com/max/960/1*VeOnqynnx5o_fkOrG29aKQ.jpeg)<figcaption>Need to remove traitor generals.</figcaption>

> This situation can be expressed abstractly in terms of a group of generals of the Byzantine army camped with their troops around an enemy city. Communicating only by messenger, the generals must **agree upon a common battle plan**. However, one or more of them may be traitors who will try to confuse the others. The problem is to find an algorithm to ensure that the loyal generals will reach agreement. It is shown that, using only oral messages, this problem is solvable if and only if **more than two-thirds** of the generals are loyal; so a single traitor can confound two loyal generals. With unforgeable written messages, the problem is solvable for any number of generals and possible traitors.

A system is said to be BFT if it can continue to operate even if certain participants drop out or are adversarial. With respect to consensus algorithms, Byzantine fault tolerance normally means the algorithm is guaranteed to converge, or capable of reaching consensus, even if there are adversarial nodes or if nodes drop from the network, etc. In this sense, BFT is a property of a consensus algorithm rather than an algorithm itself.  
The idea is that for a system of parties who are decentralized all over the world, for them to have a common agreement or consensus among each other. And for this, there is a need for 2/3rd’s of the people, to be honest.

Hard parts over, now let’s look at the second part.

[**State machine replication**](https://en.wikipedia.org/wiki/State_machine_replication)

In computer science, a _state machine_ refers to something that will read a series of inputs and, based on those inputs, will transition to a new state.

![](https://cdn-images-1.medium.com/max/800/1*hJ8eDA-YXM62paxFfvNK_A.png)

With Tendermint’s state machine, we begin with a “genesis state.” This is analogous to a blank slate before any transactions have happened on the network. When transactions are executed, this genesis state transitions into some final state. At any point in time, this final state represents the current state of Tendermint.

![](https://cdn-images-1.medium.com/max/800/1*u2bo41-QYZwctRaJR7uEUA.png)

The programmer defines the initial state that should tolerate failures and the set of allowable transitions on this state, and the system ensures that every replica starts in the same state, and every replica makes the same state transitions in the same order. This is state machine replication.

_Or we can just call it a blockchain_. Now let’s get into the details of things.

### Writing Applications using Tendermint

> Tendermint can replicate deterministic state machines written in any programming language.

![](https://cdn-images-1.medium.com/max/700/1*dkhrybKc6E-EZi-KrUS8yA.png)<figcaption>TSMP is called ABCI now</figcaption>

Which is pretty good, considering if you wish to make an application for Ethereum you end up learning solidity. Bare with me, it’s a bit tricky.

#### Start off by installing Tendermint

[Quick Start | Tendermint Documentation](https://tendermint.com/docs/introduction/quick-start.html#install)

Let’s first run a pre-built application and then understand how to make it our-self. We will use an application called kvstore. It’s a simple application which will take a “key” and a “value” and stores it. So that later you can get the value when you search for the “key”. A very simple database application.

![](https://cdn-images-1.medium.com/max/320/1*lxMQB2bMlK8QYFTqcdlytw.png)

#### Running pre-built kvstore

The Tendermint team has already build kvstore example into the application.   
Better you type these parts as we go, that’s why I didn’t type the output.

{% gist https://gist.github.com/gauthamzz/89fbbcc8ba09a161bde6e2c7d43d7554 %}

- [untitled](https://asciinema.org/a/ddaa7WWd3lrV7l1NP3ZYU3AD3)
- [untitled](https://asciinema.org/a/U6SkGnw40EveOGgP2MX1oiGxB)

Let’s breakdown the process we started an application using the command ‘tendermint node’. The tendermint application runs on port 26657 on your local machine. Go to that port to see the application running smoothly. You have so many methods which you can use.

#### KVStore using abci-cli

Why don’t we do the same task in a different way, where you don’t have you type curl command always. Tendermint has this tool called abci-console which allows you to send requests easily to the Tendermint application.

ABCI is short for application blockchain interface, which connects the application with the blockchain.

The abci-console allows people to send Transactions easily. Now let us see the methods we used in abci-console to send transactions to Tendermint.

{% gist https://gist.github.com/gauthamzz/2cd0213d995aacbded84cb9d4f674237 %}

We used deliver\_tx to send a transaction, this is the part where the state is updated in our machine. we committed that transaction to store the transaction, this is where the state increases the height of the chain stores the block. After which we query it from the Tendermint node, which is basically looking up the state for the value for the given key.

![](https://cdn-images-1.medium.com/max/1024/1*6fTKb38pjlhOkUg50rbLtg.png)

Let us summarize.

> You deliver a transaction, then you commit the transaction and then you can query the transaction.

Now, let us look at how to make the KV Store application as you want.   
If you notice I used the home flag, It’s to make sure the genesis token is in the folder named node.

### Let’s code our application

I will be writing an application in Python, which is a very simple language I think you can follow it even if you don’t understand it. [The full code is here](https://github.com/tendermint/tendermint/blob/e6a6c6523eae02ce62e08c107dbd27f33b79a107/abci/example/python3_kvstore/kvstore.py) if you wish to see it.

I will be using [the py-abci](https://github.com/davebryson/py-abci) module, which is a wrapper for abci in python. I am not going to reinvent the wheel.

We will start off by creating a state of the Tendermint node. I will add a DB which is MemoryDB, a size to define the size of the state, a height to define the height of the block and an apphash for the application.

{% gist https://gist.github.com/gauthamzz/8e69c41f4e3edfa6964eab717a2e3f2d %}

#### Deliver\_tx

If you remember the deliver\_tx in the above example we send the key and value pair to the Tendermint node. And this key and value are stored in the DB and state is updated.

The data input is in bytes. So we split the string using ‘=’ and save the key and value pair. The size variable we had set will be increased.

{% gist https://gist.github.com/gauthamzz/c71cd51fd460481e468f4c2172cd7fa6 %}

Now that a transaction is delivered we need to save the state, for that we use commit.

#### Commit

During commit, we need to return an apphash, representing the change of state in the chain. I am creating a big-endian notation of the size as the apphash. The height of the chain is increased and we are saving the state.

{% gist https://gist.github.com/gauthamzz/30651effc5293b76929200a162e5b160 %}

Now that we have updated the state, we need to ensure that the application works. So let us make a query.

#### Query

The query seems straight forward. You check if the value is in the DB and return it.

{% gist https://gist.github.com/gauthamzz/c2f786595baeca996b71ee6f5ec44de1 %}

**That’s it**. HURRRRRRAAAAAAAAAAAAYYYYYYYY.

We took care of delivering the transaction, then we made the commit and query.

One thing we can ensure is that if the transaction is bad, we can neglect it from entering the mempool of the blockchain. We can use check\_tx method for that, but that’s unnecessary for this use case.

Now do check out the whole code or write the rest yourself, in your favourite programming language.

Now there are other methods like broadcast\_tx\_sync or broadcast\_tx\_asyc which are more faster than commit. You can use it depending on the use case. Please read the docs for more info.

[There is another example in Go if you wish to take a look.](https://github.com/tendermint/tendermint/tree/e6a6c6523eae02ce62e08c107dbd27f33b79a107/abci/example/kvstore)

#### Notes

After you start the tendermint node then run your python code to run the application. Then you should be able to send requests just like the kvstore example above.

![](https://cdn-images-1.medium.com/max/400/1*irtYZac2qbxaWGmJ43xqSQ.jpeg)

The tendermint node runs in port 26657 and abci in 26658.

### Conclusion

…Phew! You made it to the end. I hope?

There’s a lot to digest in this post, I know. If it takes you multiple reads to fully understand what’s going on, that’s totally fine. I personally read the various parts of the codebase many times and coded the application before grokking what was going on.

Nonetheless, I hope you found this overview helpful. If you find any errors or mistakes, I’d love for you to write a private note or post it directly in the comments. I look at all of ’em, I promise.

And remember, I’m human (yep, it’s true) and I make mistakes. I took the time to write this post for the benefit of the community, for free. So please be constructive in your feedback, without unnecessary bashing. ☺️

_Thank you for reading. If you like the Article give it a clap 👏._

_Do consider Buying me a Coffee_ [_https://www.buymeacoffee.com/gautham_](https://www.buymeacoffee.com/gautham)_ , If you loved the article._

_If you wish to have a chat,_ [_DM me at https://twitter.com/gauthamzzz_](https://twitter.com/gauthamzzz)_._

_I am a Masters student at the Indian Institute of Information Technology, Allahabad currently a research intern at the University of Messina. My Website_ [_http://gauthamzz.com_](http://gauthamzz.com/)_._

#### Resources

[1][https://www.reddit.com/r/CryptoTechnology/comments/8782mb/what\_is\_the\_difference\_between\_normal\_byzantine/](https://www.reddit.com/r/CryptoTechnology/comments/8782mb/what_is_the_difference_between_normal_byzantine/)

[2][https://web.archive.org/web/20170205142845/http://lamport.azurewebsites.net/pubs/byz.pdf](https://web.archive.org/web/20170205142845/http://lamport.azurewebsites.net/pubs/byz.pdf)

[3][http://hackingdistributed.com/2013/12/26/introducing-replicant/](http://hackingdistributed.com/2013/12/26/introducing-replicant/)

[How does Ethereum work, anyway?](https://medium.com/@preethikasireddy/how-does-ethereum-work-anyway-22d1df506369)

* * *