Building a Cryptocurrency with Kotlin : Part 1

File:Kotlin-logo.svgKotlin has been becoming more and more mature with every release. We arguably have the most concise modern programming language that currently exists. With language idioms like data classes, extensions, kotlin objects, delegated properties, lambdas and of course, the latest version of Kotlin adds coroutine support. Lets talk about Node.js for a second. Node.js helped us to create many amazing websites and services, and provides numerous modules to make our lives as developers easier. But just because a tool is good enough does not mean we should stop innovation there. Kotlin is now Node.js' competitor because Kotlin's powerful language idioms enable us to create a powerful and fast framework for building asynchronous servers and clients, called Ktor.


Related imageLets go back in time for a moment. Back in 2008 a small C++ side project was started by Satoshi Nakamoto. This project would change the world for ever. 10 years later Bitcoin managed to inspire dozens of crypto currency projects, in languages such as Rust and even Java. With features ranging from proof-of-stake, smart contracts and atomic-swaps.

For this series of tutorials we will learn how cryptocurrencies are written by creating our own, by using Kotlin and nothing else. This currency will have most common features like a blockchain, peer-to-peer consensus, transaction validation, proof-of-work, wallets, smart contracts, and more. Don't worry its actually easier than you think. Also for simplicity we will skip some security features, so final result will not be fit for production use. The reason for these tutorial series is to learn about Kotlin and cryptocurrencies by writing one from scratch. So lets get started!

It All Starts With a Chain of Blocks

Blockchain is a very simple data structure, similar to a linked list. The main difference is that each block holds the hash of the previous block. Think of a hash as a unique signature of that block, given all the data it currently contains, and even if a single bit of that data is different the hash would change. Just like a linked list, each item in the block chain has data. The base minimum data that a block needs to hold are:
  • index: the number of the block in the chain.
  • previous hash: calculated hash of the previous block at the moment of creation of the current block.
  • data: this could be any data like transaction inputs and outputs, smart contract states and inputs, or any other data you wish to store. For this lesson we will keep a string value but in the next lessons we will store transaction data in this field.
  • hash: unlike other fields this is a function that is called on the block to calculate its hash.

There is a built in security feature in such a data structure. In order for a malicious actor to change a single block's data, the actor would have to modify the hash of that block and all the block following it in order to maintain the validity of the data structure.

We will create a class to help us manage our collection of Blocks. Because we only need a single instance of the BlockChain we will make it a Kotlin singleton. For this lesson we will keep the BlockChain object simple. It will have two main functions addNewBlock and isValid. Lets see how our singleton object would look like in Kotlin.

To add a new block to our list of blocks we simply create a new instance on the Block data class with the index of the current block, the hash of the previous block, the time stamp and the data string.

To validate the chain we will iterate through every Block until the genesis block and make sure the previousHash matches the actual hash of the block, and the first block matches the hardcoded genesis block, that way we know none of the individual block has been modified. In the next lesson we will see how to pick the longest chain in the network to meet consensus but for now lets see how to implements the validation function:

And to add some final touches to our node implementation lets create couple endpoints to call the functions on or BlockChain object. But first lets add the following Ktor dependencies to our gradle build file:
    compile "io.ktor:ktor-server-netty:$ktor_version"
    compile "io.ktor:ktor-html-builder:$ktor_version"

Next we will implement a simple Ktor Application module and create 3 endpoints, root that will display a list of all blocks, /add that will generate a new block on our chain, and finally a /isValid endpoint to check the validity of the current chain.


And as you can see the basic underlying structure of most blockchains is very simple. Hopefully this lesson helped you understand the basics of blockchain, the fundamental technology of most crypto currencies.

course image
If you guys want to learn more about Kotlin and all the powerful features that the language offers please check out my Fundamentals of Programming with Kotlin Udemy course.

Comments

  1. I am all that much satisfied with the substance you have specified. I needed to thank you for this awesome article.  Coins Libra

    ReplyDelete
  2. You may find that cryptocurrency is for you or you may not but rather at any rate you'll have the option to talk with a level of sureness and learning that others won't have.
    TradingPlatform

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Data Binding : Dynamic RecyclerView Adapter

Android Architecture Components : LiveData

Android Data Binding : Tricks and Magic