How does blockchain really work? I built an app to show you

According to Wikipedia, blockchain is:

A distributed database for maintaining an ever-growing list of records, we call it a blockchain.

That sounds great, but how does it work?

To illustrate Blockchain, we will use an open source command-line tool called Blockchain CLI.

I also built a browser-based version

Install the command line tool

Install Node.js before doing so

Then run the following command from your command line:

npm install blockchain-cli -g
blockchainCopy the code

You should see 👋 Welcome to Blockchain CLI! And a blockchain → tip. That means it’s ready.

What does the block look like?

To view the current blockchain, you need to type Blockchain or BC at the command prompt. You should see a block like the image below.

A block on a blockchain

  • Index: which block is it (the Index of the Genesis block is 0)?
  • Hash: Is the block valid?
  • Previous Hash: Is the Previous block valid?
  • Timestamp: When was the block added?
  • Data: What information is stored on the block?
  • Nonce: How many iterations did we go through before we found a valid block?

Creation piece

Each blockchain starts with 🏆 Genesis Block. As you will see later, each block on the blockchain depends on the previous block. So, we need the Genesis block to dig out our first block.

What happens when a new block is mined?



Mime freeCodeCamp has ︎

Blockchain looks at the latest block on the chain to get the Index and previous hash. The Genesis block is the newest block in this case.

  • Index: 0 + 1 = 1
  • The Previous Hash: 0000018035 a828da0…
  • Timestamp: time when the block was added
  • Data: freeCodeCamp ❤
  • Hash:??
  • Nonce:??

How does the Hash work?

A hash is a fixed length value that uniquely identifies data.

Hash is computed by taking Index, Previous Hash, Timestamp, Data, and Nonce as input values.

CryptoJS.SHA256(index + previousHash + timestamp + data + nonce)Copy the code

The SHA256 algorithm will calculate a unique Hash value based on these inputs. The same input always returns the same result.

Did you notice the four leading zeros in the block Hash?

Four leading zeros is the minimum required for a valid Hash. The number of leading zeros required is called difficulty

function isValidHashDifficulty(hash, difficulty) {
  for (var i = 0, b = hash.length; i < b; i ++) {
      if (hash[i] ! = ='0') {
          break; }}return i >= difficulty;
}Copy the code

This is also known as a proof of work system

What is a Nonce?

The Nonce is the number of times a valid Hash is searched.

let nonce = 0;
let hash;
let input;
while(! isValidHashDifficulty(hash)) {     
  nonce = nonce + 1;
  input = index + previousHash + timestamp + data + nonce;
  hash = CryptoJS.SHA256(input)
}Copy the code

Nonce iterates until the Hash is valid. In our case, a valid Hash must have at least four leading zeros. The process of finding the Nonce corresponding to a valid Hash is mining.

As the difficulty increases, the number of possible valid hashes decreases. With fewer valid hashes, we need more computational power to find valid hashes.

Why is it so important?

These mechanisms are important because they make the blockchain immutable.

If we have A blockchain “A->B->C” and A person wants to change the data on block A. So what happens?

  1. The data on block A has changed.
  2. The hash of block A changes because the data is used to compute the hash.
  3. Block A is invalidated because its hash no longer has four leading zeros.
  4. The hash of block B changes because the hash of block A is used to compute the hash of block B.
  5. Block B is invalidated because its hash no longer has four leading zeros.
  6. The hash of block B changes because the hash of block C is used to compute the hash of block B.
  7. Block C is invalidated because its hash no longer has four leading zeros.

The only way to change a block is to dig it up again, and then all the blocks. Since new blocks are always being added, changing blocks is almost impossible.

I hope you found this tutorial helpful!

If you want to see the web version of the demo, go out and right to BlockchainDemo.io