The Guide to Blockchain: How Does It Work?

Gilbert Komin

blockchain basics

With the rise of Web3, blockchain has undoubtedly become one of the most important words in the current financial domain. In this article, I will try to introduce you to the basics and answer any questions you may have.

Let's dive in

What is Blockchain and How Does It Work?

The definition from Wikipedia says that blockchain is a growing list of records, called blocks that are securely linked together using cryptography, but that’s definitely more to this topic and I’d like to explore it below.

Is this rocket science? The answer is NO. Is this real magic?! I think so!

So, let’s grab a cup of coffee and start our adventure!

Cta image

Blockchain - a Simple Definition

To put it simply, blockchain is just a database. It’s not complicated. You can create a simple model of blockchain even in Excel. The main principle of this database is that you can’t change the record you wrote, though. You can only add a new one. Each new record has a fingerprint (called a hash) about the previous one. So, you have a chain connected together by hashes. 

If you don't follow the rules and you don’t respect that blockchain is immutable and you want to edit an existing record - this action changes the fingerprint of this record. Since that fingerprint is included in the next block, the next record fingerprint is changed too, the next also… and so on.

It’s a domino effect where any change becomes obvious. You can't edit any record without everyone noticing. The nodes - guardians of the blockchain - verify every record. They’ve got a copy of the current state of the database. If you edit anything, the nodes will find your edited record easily, and they will reject your wily changes.

If you want to add a new record - the nodes have to reach a consensus. The consensus is a set of rules (algorithm) on how to add a new record and how consensus should work itself. The most popular algorithms of consensus are PoW - Proof-of-Work, i.e. Bitcoin/Ethereum (before merge) network, PoS - Proof- of-Stake, i.e. Solana/Cardano/Ethereum (after merge). I will dive deeper into it in the next articles in this series.

Blockchain in More Detail

chain of blocksThe blockchain is what the name tells us - a chain of blocks.

A common misconception is that a blockchain is a single chain of blocks when actually, it's more like a tree of chains of blocks. The blocks are connected together by hashes. A hash is a string generated by a one-way cryptography hashing function. It’s calculated based on the data inside the block (i.e., parent hash, timestamp, transactions data) as the process of generating a fixed size output from a variable size input. 

Thanks to the hash function, blockchain has its most important attribute, namely immutability. If someone wants to change their block, the nodes will be notified of these changes, because the hash function will return another hash - your changes won’t be accepted. Changing a block in the blockchain is not fully impossible, but almost impossible. Potentially it can be possible during the so-called 51% attack which is one of the biggest threats to blockchain - I’ll explain it in more detail below.

What is a 51% Attack on Blockchain?

A 51% attack is an attack on a blockchain network in which an entity is able to control most of the computing power present in the entire network, which may lead to its use for its own purposes without verifying it with the remaining 49% of the nodes. This is why the nodes should be distributed and decentralized. They should be fully independent of each other and must regularly reach a consensus on the correctness of all transactions and on the mining process (adding new blocks to the blockchain).

The networks of bitcoin and major altcoins are so large that an attack by 51% is realistically impossible in theory. It is also unprofitable. Malicious nodes never get a block mining reward and are excluded from circulation.

To sum up, a blockchain is a tree of blocks. There are multiple chains that a new block can join at any time. The new block has to have a parent.

Pointing to the parent can be done by using hashes that are computed from the data inside the block (i.e. parent hash, transaction data, and timestamp).

By indicating the parent with hashes, we can enforce a specific block order. Then the peer-to-peer aspect of the network and the consensus algorithm (e.g. PoW/ PoS) come into play. When a node decides to add a new block, it distributes that block to the other nodes. Then these remaining nodes verify the newly created block and decide whether to add it to their tree.

If the hash of the new block is correct, the block is accepted and added to the tree. The whole process of adding a new block is very energy-consuming. You have to do some demanding calculations to be able to guess the correct hash and add a new block to the network according to the consensus algorithm. This procedure is called mining.

From this definition, a blockchain is the longest currently available chain in the tree. If new blocks are added to another chain, it may be longer than the current longest one and this chain will be called a blockchain.

How to Make a Blockchain

Let’s create a simple model of blockchain in JS and prepare some code snippets to better understand how it works. As we already know, blockchain is more like a tree than a chain, but for our simple model, let's treat a chain as a simple array. 


class Blockchain {
  constructor() {
    this.chain = [new Block(Date.now().toString())]; // genesis block
  }

  getLastBlock() {
    return this.chain[this.chain.length - 1];
  }

  addBlock(block) {
    block.parentHash = this.getLastBlock().hash || "root";
    block.hash = block.getHash();
    this.chain.push(block);
  }
}

Now I will present a simple model of the block. So each block has information about data transactions, timestamp, hash, and parent hash. We can imagine it like that: 


class Block {
  constructor(timestamp = "", data = []) {
    this.data = data; // data of transactions 
    this.timestamp = timestamp; // current timestamp 
    this.parentHash =  ""; // hash of previous block
    this.hash = this.getHash(); // cryptography hashing function
  }

  // simple example of hashing function
  getHash() {
    return generateHash(this.parentHash + this.timestamp + this.data);
  }
}

So, we created a chain with a genesis block. Next, we created two functions: getLastBlock for getting the last block in the chain, and an addBlock function for adding a new block to the chain. Later we defined the Block class with all needed fields and a getHash function for creating a hash. 

We have to create a generateHash function. We will use a crypto package for that. 


const crypto = require("crypto");

const SALT = 'hard$alt';

function generateHash(pass) {
  return crypto.createHmac('sha256', SALT)
    .update(pass)
    .digest('hex')
}

In that function we use crypto.createHmac function. We pass to this function hash algorithm type, and SALT. Next we update this hash with our block data (this.parentHash + this.timestamp + this.data). Then we encode the result to hex. 

If we want to add a new block to the blockchain we have to write: 


const NewBlockchain = new Blockchain();

NewBlockchain.addBlock(new Block(Date.now().toString(), { 
  from: "Tom", 
  to: "Robert", 
  amount: 100, 
  currency:  "NEW",
}));

And that’s it! We already created a really simple model of blockchain. We created a new chain with a genesis block and next, we added a new block with transaction data. We send 100 quantities of new token "NEW" from Tom to Robert.

If you want to see the current state of blockchain you can write:


console.log(NewBlockchain.chain);

[ Block {
    data: [],
    timestamp: '1660643581648',
    parentHash: '',
    hash:
     'c71ff035eefcf504b833a88be8574cef7fad66470e20f762446c035fa808f210' },
  Block {
    data: { from: 'Tom', to: 'Robert', amount: 100, currency: 'NEW' },
    timestamp: '1660643581649',
    parentHash:
     'c71ff035eefcf504b833a88be8574cef7fad66470e20f762446c035fa808f210',
    hash:
     '8eb69a2f46f1506b239fa5cdf55ac99f82c4c7b6701f27e42263b7012554eb0d' } ]

We have to remember that we don’t add validation, the difficulty of mining, block time, and much more. Don’t worry, I will describe it more precisely in the next articles.

For now, we have our first blockchain! Hurray! You can play with this implementation here: http://tpcg.io/_4VGUQ8

History of Blockchain

In 1991 two scientists, Stuart Haber and Wakefield Scott Stornetta created the document entitled „How to Time-Stamp a Digital Document”. It was a solution for marking documents with timestamps that prevented their modification or forgery. The system used cryptographically secured blocks to store time-stamped documents.

In 1992, the hash tree (Merkle) was added to this solution. Its main functionality was to collect many documents in one block.

In 2000, Stefan Konst published the concept of a distributed transaction book. This solution was developed to allow access to financial data in which each user of the book is independent of others.

In 2004, Hal Finney introduced a system called Reusable Proof of Work (RPoW). It was based on getting unchanging hashcashes - a cryptographic algorithm that was validated based on hashing. RPoW solved the double spending problem by preserving the ownership of tokens on a trusted server designed to allow users worldwide to validate and validate their integrity in real-time.

Before bitcoin, there were two attempts to create a virtual currency. In 2005, Nick Szabo created the concept of a cryptocurrency called Bit Gold. In 2006, Wai Dai published a similar currency concept called b-money.

In 2008, a person or a group of people with the pseudonym Satoshi Nakamoto published a Bitcoin white paper outlining all the assumptions of a decentralized peer-to-peer network. The approach proposed by Nakamoto was based on a decentralized protocol to track and verify transactions. It was the first real application of this type of technology. The network was launched on January 3, 2009.

In 2014, the Ethereum network was founded by Vitalik Buterin. The Ethereum blockchain includes functionality for creating and managing smart contracts. Smart contracts are programs that can be used, for example, for a transaction that will be finalized if certain conditions are met.

Blockchain - Use Cases

Blockchain has a lot of applications in the real world. We will describe the most important ones:

  • Cryptocurrencies are built on blockchain. Blockchain allows users to follow the process of sending funds, which increases the transparency of trading, without any middlepeople. You, the recipient, and the network - thanks to blockchain technology, we don't need a centralized bank or anything else. Cryptocurrencies are the most important application of blockchain.
  • NFT (a non-fungible token) - a unique blockchain-based digital data unit that users can trade with each other. An NFT represents a wide range of tangible and intangible items such as virtual real estate or virtual works of art, sports cards, music songs, video clips, and graphics. 
  • Digitization of documents (guarantee of non-repudiation, storage of equivalents of paper documents in the form of smart contracts).
  • Logistics - thanks to blockchain, everyone can check at what stage is the transport of a given product. It can also be used to confirm the authenticity of a particular product. All you need is to scan the QR code and check the record in the blockchain and we know everything.
  • Voting in organizations, companies, and political elections - thanks to blockchain, a new model of organization management (Decentralized Autonomous Organization) has emerged. In practice, it's a program that is transparent, member-controlled, and independent of any central authority. This governance model may be the future of many markets and political choices. There have already been several trials, for example in the canton of Zug in Switzerland and in the state of Ohio for soldiers outside the US.
  • COVID-19 Passports for New York City - a passport is issued through a platform that verifies whether you have been vaccinated for COVID-19 or have tested negative for the virus. The platform was built using blockchain architecture. 
  • New currencies in computer games - a lot of Play2Earn games have their own token, i.e. Axie Infinity. You can play the game, win some tokens, and then sell them.

The Future of Blockchain: What’s Next For This Technology?

Looking at how this technology is advancing, blockchain is poised to expand its existing functionality on a larger scale. In the future, we may hear of nationwide political elections where the possibility of counterfeiting the results will be impossible. Digitalization of documents with certified ownership will also be possible - the notary will no longer be needed because in blockchain it is easy to see who is the owner. You will be able to put all of your personal documents on the blockchain. You only need one QR code and you will have access to all your documents. Metaverse - a virtual reality that is just taking its first steps, but it may change the world. Transferring banking to blockchain may become feasible.

The possibilities are endless.

Blockchain still has a lot of room for expansion. Large companies and governments are following blockchain technology. In a few years, blockchain will probably have several applications we don't know about now.

Summary

In this article, I tried to explain in a simple way what blockchain is. I described the blockchain's construction, history, application, and its future. I wanted to present a general picture of the technology.

In the next articles, I will go a little more into detail - stay tuned!

Cta image
Gilbert Komin avatar
Gilbert Komin