How does blockchain actually work?

There is a lot of information available online on blockchains, crytocurrencies, etc. But only a few manage to explain what is happening behind the scenes of these technologies on a basic level. This article attempts to answer such queries and explain what actually is blockchain, how it works and what is its use. So lets dive in!

Suppose there are few friends who go out a lot and thus many times they pay for each other. After some time, they settle up the amounts they owe to each other and this goes on. But they can’t solely depend on their memories as it might deceive them. So how do they approach this problem? Simple. They will make a common ledger on a paper where everyone can add the amount others owe them. This ledger might look like this:

communal ledger

Now how will you ensure that no one adds any fake line in the legder, or changes the amount in order to cheat?

We can simply add signature of person who owes the amount at the end of each line. This will ensure no one else can add a fake entry on behalf of the person who owes money.

signed ledger

But people can copy each other’s signatures, right? To prevent that, we can use digital signatures. In this system, everyone will have their public key and private key. The public key of people are known to everyone while private keys of a person are secret, like a password. The digital signature is usually a very long binary number which is generated using a combination of the input statement, and the private key. These two parameters, when passed on to a hash function give us the digital signature.

To understand this we need to know what are hash functions. Hash functions are special functions, which always produce a unique output for a given input.

Hash_function (inputs) = 010010010… (unique binary number)

The property of hash functions is that it will never produce the same output, unless the input is same. The length and type of the number generated (binary, decimal, hexadecimal) depends on the type of hash function used. There are multiple types of hash function like MD5, SHA256 etc. Bitcoin, in particular uses SHA256 hash. Also, just a small change in hash input completely changes the output of the hash function. For example:

Hash_function(“hello world”) = 010010011101001…

Hash_function(“helo world”) = 1011010110010011..

Note that for a given combination of inputs, the hash function will always generate a unique signature. This ensures that signature of a person is unique as the secret key is known to that person only.

For example, in the first line of ledger, Suppose Alice has a private key: 1423 (which only she knows), Alice can add her signatures in the following way:

Hash_function (“Alice owes Bob $10”, 1423) = 011000100111.

Thus 011000100111 is the digital signature of Alice. This signature can be generated by Alice only as she only knows her private key.

To verify if the signature is correct, the hash function has a corresponding verification function as well. The verification function returns True or False if the signature is correct or not. It works as follows:

Verification_function (message, signature, public key) = True/ False

So anyone can check if a particular signature is real or fraudulent by simply putting the message, the signature which has to be checked, and public key of person who signed (which is known to everyone). If the function returns True, then the signature is authentic, else fraudulent.

So currently our ledger looks like this after implementation of digital signatures:

digi ledger.PNGThough now it can be checked if an entry in ledger is legit or not by simply checking its signature, but one thing can still be done to add fake entries in the ledger. What if someone copies one of the previous entry multiple times in the ledger, thus forcing the borrower to pay multiple times. The entry and signature combination would still hold true, thus the borrower would have no way to claim that it is a fake entry.

ledger with copied entry

To solve this problem, we can simply add a unique ID  (eg. serial no.) before each line. Now if someone will try to copy same entry multiple times, the ID will be same in both, thus they will become invalid. Now with this implementation, our ledger looks like this:

id ledger

Also instead of writing “owes” or “pays” every time in ledger, we can standardize our entries. For example if Alice has to give Bob $10, we can simply write:

  1. A—>B $10  01001110

Now we have solved few problems in our ledger and made it more secure. With this ledger, we can do one more thing, i.e stop relying on actual currency to perform transactions. What everyone can do is, bank some initial amount, and then use it for transactions. For example if Alice banks in $80, Bob banks in $70 and so on, then Alice can not overspend this amount, and if any transaction leads to overspending, it won’t be added to ledger as Alice doesn’t have that much money to pay. Now since the deposit money remains in this ledger system, It can be called anything, like ledgercoin (LC), and this ledgercoin can be used for transactions within this system consisting of friends.

Cryptocurrencies, like bitcoin are quite similar to these hypothetical ledgercoins which we invented. But instead of our few friends, they have ecosystem of millions of users, who can exchange these bitcoins with each others for various services like buying pizza, downloading apps, and even buying/ selling illegal items on dark web. Now since initial no. of ledgercoins for each person is limited, they can’t overspend this amount. And if they want to spend more, they will have to add more ledgercoins in their balance.

overspend.PNG

Now our ledger works fine, but we need to still find some common place like a bulletin board, or a website to post our ledger so that people can add their entries. In a way we are still dependent on a 3rd party to host our ledger, this 3rd party like a website might become down, or get hacked, thus preventing us from transacting. In order to finish our dependence on a 3rd party, we can create multiple copies of the ledger and distribute it to all our friends. But now  we have to find a way of maintaining consistency between these copies. That’s where the things get interesting!

The way we can ensure consistency between everyone’s ledger is by broadcasting the transactions whenever they happen. For example, if Alice->Bob LC 10 is signed and updated in Alice’s ledger, then it should get automatically broadcast to everyone else in the system, including Bob, so that they all can add this entry in their ledgers.

broad2

After the broadcast reaches others, their respective ledgers will be updated. Now Bob will update his ledger quickly as he is receiving money, but how will he ensure others have updated their ledgers as well? After all, Bob might  need to use these 10 LC he just received to further pay others, and if others are not aware of Bob owning these 10 LC, then Bob won’t be able to perform transactions using these 10 LC.

To solve this problem, Satoshi Nakamoto, the inventor of Bitcoin proposed an algorithm called “Proof of work”. This algorithm requires people to compete while solving a cryptographic puzzle whenever some new transactions are added. But before learning proof of work, we need to get ourselves familiar with blocks.

What is the ‘block’ in blockchain?

A block consists of a set of transaction entries along with some additional parameters. In a way, it is simply a piece of ledger with some additional entries. The number of transaction entries inside a block if usually fixed. Below is the basic structure of block:

block struc

Let us understand the constituents of a block one by one:

  1. Serial No: pretty self explanatory, it denotes the serial in which a particular block comes in the blockchain (we will know what blockchain is in a minute).
  2. Previous Hash: It denotes the hash value of the block preceding a particular block.
  3. Data: It contains the transaction information like sender, reciever, amount, digital signatures and other details.
  4. Nonce: It is a special number which we have to find out in order to mine a block. We will learn about mining in a minute.
  5. Hash: It is the value generated by the hash function when all the above constituents of the block are passed through it. hash = hash_function(s.no, prev hash, data, nonce)

As you might have noticed above, each block also contains the hash of the previous block, so in a way. these blocks are connected as the ‘hash’ value of a block points to the ‘previous hash’ value of  next block. This chain of connected blocks is called a blockchain.

blockchain

Notice that hash of a block is same as the previous hash of the next block.

Proof of work

Whenever a user broadcasts a block to everyone, then we already have the S.no, previous hash and data. In order to find block hash, we need to find the nonce.

Nonce is such a number, so that when inserted in the hash function along with data, S.no, prev. hash, It produces a hash which is less than a given number.

For example, if we need the hash to be less than 999999 then we have to find a nonce such that it satisfies:

Hash_function (nonce, S.no, Data, Prev. hash) < 999999

There is no fixed way to find nonce as the hash produced are truly random in nature. So basically all the users try out many numbers one by one until one of them finds the nonce. The person who finds the nonce first then broadcasts it to other nodes and everyone updates this value in their blockchain. Note that this process of finding the correct nonce is called mining.

Once a block gets mined, only then it gets added in the blockchain. So the above process can be summarized with the illustrations below:

step1

step2

step3

step4.PNG

But how does this process make blockchain secure? The answer is simple. If someone will try to change the transaction entries in a mined block, then the data of the block will change. This in turn, will change the hash of this block and the hash won’t remain lesser than the required value. Also, in the next block, the ‘previous hash’ value will change, this will in turn change hash of this next block as well as the hash depends on all the constituent values in a block. This will keep on happening until all the blocks following the tampered block will become un-mined again, thus the tampering will be caught and the changes will be reverted.

security

There are other mechanisms which prevent fraud transactions from appearing in blockchain like forking, key management etc. We will cover these topics later as this blog post has become too long! Hope you understood the basics of blockchain, please feel free to ask doubts in the comments section! Also please do check out my blog codeharvest.

Leave a Reply

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