Writing Applications using Tendermint

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

TSMP is called ABCI now

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

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.

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.

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.

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.

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 if you wish to see it.

I will be using the 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.

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.

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.

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.

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.

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.

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 , If you loved the article.

If you wish to have a chat, DM me at 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.

Resources

[1]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

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