I'm not sure if you already listened about Blockchain, but how about Bitcoin? Probably yes, right? The Blockchain is the technology behind the cryptocurrency. However, this technology is used to much more scenarios: smart contracts (facilitate, verify, or negotiate a contract agreement), financial services, video games, security, Healthcare (store their patients’ medical records), Automobiles, Insurance, etc. It is a really safe technology. Most parts of the problems happen because of the users and not because of the technology.

Here, let’s understand the basic concepts about how the Blockchain works.

The characteristics: distributed, secure, transparent, consensus-based and flexible.

What is this?

The Blockchain is a list of blocks (really? 🤔) connected to each other by a cryptographic hash. It is used to identify the previous block.

Each block contains many messages (or transactions), timestamps (when the block was created) and hash codes. To get better about the hash you can try here.


In fact, you will identify different types of hash codes. One of them is to identify the previous block, the other one is the root hash[1], [2] that represents a list of hash inside a binary tree that regarding all the transactions. An important value inside the block is the Nonce, which will be used to generate the final hash that represents this block.


When a new block is created, the hash used to identify this block is the combining of the previous hash, the root hash and the Nonce value. The Nonce is a calculated value used to identify the block. Below is a simplistic example of how to create the hash that will represent the block.

//calculateBlockHash
String dataToHash = previousHash + Long.toString(timeStamp)
    + Integer.toString(nonce)  + data;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] bytes = digest.digest(dataToHash.getBytes());

The hash code is the key to verify if the block was modified. If a new process runs and generated a different hash code then it is known that this block was modified. A complete example of the Block class you can see here.

The process to create the new block is mining.

Mining

The key point of the Blockchain is to be decentralized, based on peer-to-peer network. Each node is a machine with duplicate data that will help to maintain the integrity. It is a kind of distributed database with all records of transactions. Always when a computer connects to the network, a synchronization is started and a copy of the data is downloaded.

The miners are nodes on the network marked as available to process the messages. Any node inside the network is electable to be responsible to create the blocks, just need to have a good processing power.  The miners are responsible by process all the messages, create the new block and add the hash code from the last block inside the new block. That is the signature of the block. The miners can dispute the creation of the block. Will win who has a better performance.

The mining begins when a transaction is broadcasted to all the nodes and a miner selects the transaction to starts the process. This process finishes when the signature attends the requirements. This requirement can be the size of the hash, the prefix is started by zero, etc.  Below, another example of how to mine the block.

public String mineBlock(int prefix) {
    String prefixString = new String(new char[prefix]).replace('\0', '0');
    while (!hash.substring(0, prefix).equals(prefixString)) {
        nonce++;
        hash = calculateBlockHash();
    }
    return hash;
}

Proof of work

It is the process to guarantee that this block is correct. To do this, the Nonce part of the block is used by miners. This process starts when a miner creates a new block and broadcast this block to the other miners to confirm this new block before to be added inside the blockchain.

If more then one miner in the network solves the Proof-of-Work and all of then connect the new block to the last block of the list, the list can have more then one branch. The next block has to choose which branch to use. It can be done by the newer criterion.

How it works

The blockchain uses public-key cryptography to represent the address.  In a general view, some sender sends a transaction by broadcast and the owner uses the private key to access the messages. The miners validate the transactions, create the block and send it to all the nodes.

The steps cited in the bitcoin article are:

  1. New transactions are broadcast to all nodes.
  2. Each node collects new transactions into a block.
  3. Each node works on finding a difficult proof-of-work for its block.
  4. When a node finds a proof-of-work, it broadcasts the block to all nodes.
  5. Nodes accept the block only if all transactions in it are valid and not already spent.
  6. Nodes express their acceptance of the block by working on creating the next block in the chain, using the hash of the accepted block as the previous hash.

In blockchain only insert new block is permitted. No deletions or updates are permitted.

Videos

What is Blockchain What are miners
Application 19 Industry

References