Programmers T-shirts

Understanding the Blockchain

Updated: Sep 10, 2021




A Blockchain is a set of digital blocks, linked together using cryptography signatures.

The data stored in these blocks is verified and processed by the network, which consists of a large number of servers (decentralized). Once a block is “mined”, the data inside that block is immutable (cannot be changed). Hence, the blockchain is considered a “digital decentralized ledger”.



blocks in the blockchain
linked blocks

In order to understand the core magic of the blockchain, we will need to understand the concept of cryptography.

Cryptography is the process of encryption. In computer science, we use Hashing algorithms in order to encrypt data.

Hash - a one-way mathematical function that always produces consistent and unique value.

We are all familiar with standard 2-way functions. eg: let’s take the number 130 and apply an x2 function over it: 130x2 = 260.

Now if we know the function that was used (x2) and the result of that operation (260), then we can always go back to the original number by using the division function (/2). hence 260/2 = 130.

In hashing, this doesn’t work. We can apply a special function to a text, number etc, but we will never be able to go back to the original value.

There are many hash algorithms available, in this tutorial we’ll use the SHA1 algorithm.

Let’s hash the following sentence with SHA1:

Input: the sw developer

Output: 80892310ec2219bedc48dbcd5b6f4b75e86bf3af

Note: I’m using the following SHA1 online tool.



Hash algorithm facts

  1. Hashing the same input will always result in the same output. Hash of “the sw developer” will always result in 80892310ec2219bedc48dbcd5b6f4b75e86bf3af

  2. Hashing is unique. Even a tiny change in the input will result in a total different output. E.g: “the sw developer a” will result in: 16b23dc214a6b6c50bf7ae600467e28ae1e99d9f

  3. Hash cannot be reversed back to the input value.


In blockchain technology, we use hashing in order to sign the current block with the data of the previous block. This action creates a cryptographic link between both blocks.


Let’s understand it

Between the curly brackets is the data of Block A.


data {

sender address: re78df8jkhf

receiver address: 358fsd89we

amount: 0.1BTC

transaction time:1630665223

prev block hash: a45a4cc01085eb15721f9d290984e358b864460e

}


Let’s apply SHA1 to the whole content between the brackets.

Output: dcd1eb4ce228cb4ccdf2fa66050d6f1844e9a70e


Now check out the data of Block B:


data { sender address: 4e7wedre8jh3

receiver address: 788sdh4we

amount: 0.023BTC

transaction time:1630666243

prev block hash: dcd1eb4ce228cb4ccdf2fa66050d6f1844e9a70e

}


As you can see, the “prev block hash” is equal to the SHA1 hash of the data of Block A.

Why is this so important?

Using this hashing technique no one can tamper with the blocks in the chain. If for example, a malicious attacker will try to change the amount of BTC transferred in Block A, the hash of that data will be changed, and since the original hash of that block is already recorded in Block B, then the link will break. Since hashing is a one-way function, the hash value cannot be changed to align the data in the previous block, hence the attacker will need to change all the blocks till the end of the chain. Take into consideration that he needs to change the blocks on all machines that are synced with the network, in less than 10 min (the mining idle interval) - such thing is impossible in a big network.


Mining

In order to operate the network, someone needs to run the hash algorithm every time a block needs to be created - the miners are handling this part.

Miners are computers that are running the mining SW of that Blockchain. Each time a transaction is being executed into the network, the miners are competing on who will be the one to solve a mathematical function that will create a new Bitcoin in the network, then they hash the data of the transaction and populates the block to all other machines connected to the network for verification.

During the mining process, a Bitcoin is created. This is in order to compensate the miners for the energy they spend on operating the network. Such a motivation system is crucial for making the Blockchain network worthwhile.


DAP - Decentralized Applications

Bitcoin is a DAP, it’s an implementation of an application on the blockchain.

It uses the decentralized blockchain as a Peer to Peer financial application.

Since Bitcoin is an implementation and not a framework for developing blockchain applications, Ethereum was introduced to fill that need.

Ethereum provides a blockchain with a set of developing tools. With that, you can mine logic (code), not only currency transactions.

With Ethereum we already see much smarter implementations, such as Games, Gambling, Cloud Storage, Smart Contracts, DEFI applications (Decentralized Financial), and more.


Wrap up

Now that you understand the technology of the blockchain, I encourage you to read more about Ethereum and Smart Contracts - these topics open up a whole new world for SW developers.

In the next tutorial, I'll be explaining how to develop a blockchain in Java.



Practical Programming Tutorials for Developers

Work Desk

The SW Developer

The SW Developer was built in order to provide Practical and Simple Tutorials for programmers.

Created by Dotan Raz, Michael Rodov & Kobi Atiya

  • github
  • LinkedIn

Subscribe

Programmers T-shirts