How does blockchain truly work? I built an app to display you

How does blockchain truly work? I built an app to display you.

According to Wikipedia, a blockchain is:

A distributed database that is used to maintain a continuously growing list of records, called blocks.

That sounds nice, but how does it work?

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

Installing the Command-Line Interface version

If you haven’t already, install Knot.js.

Then run the following in your terminal:

You should see �� Welcome to Blockchain CLI! and a blockchain → prompt ready to take guidelines.

What does a block look like?

To see your current blockchain, inject blockchain or bc into the guideline prompt. You should see a block like the photo below.

Genesis Block

Every blockchain will embark with the �� Genesis Block . As you will see later, each block on the blockchain is dependent on the previous block. So, the Genesis block is needed to mine our very first block.

What happens when a fresh block is mined?

Let’s mine our very first block. Inject mine freeCodeCamp♥︎ into the prompt.

The blockchain looks at the latest block on the blockchain for the index and previous hash. In this case Genesis block is the latest block.

How is the hash calculated?

A hash value is a numeric value of a immobilized length that uniquely identifies data.

The hash is calculated by taking the index, previous block hash, timestamp, block data, and nonce as input.

The SHA256 algorithm will calculate a unique hash, given those inputs. The same inputs will always come back the same hash.

Did you notice the four leading 0’s in the block hash?

The four leading 0’s is a minimum requirement for a valid hash. The number of leading 0’s required is called difficulty.

This is also known as the Proof-of-Work system.

What’s a nonce?

A nonce is a number used to find a valid hash.

The nonce iterates until the hash is valid. In our case, a valid hash has at least four leading 0’s. The process of finding a nonce that corresponds to a valid hash is mining.

As the difficulty increases, the number of possible valid hashes decreases. With less possible valid hashes, it takes more processing power to find a valid hash.

Why does this matter?

It matters because it keeps the blockchain immutable.

If we have the following blockchain A → B → C, and someone wants to switch data on Block A. This is what happens:

  1. Data switches on Block A.
  2. Block A’s hash switches because data is used to calculate the hash.
  3. Block A becomes invalid because its hash no longer has four leading 0’s.
  4. Block B’s hash switches because Block A’s hash was used to calculate Block B’s hash.
  5. Block B becomes invalid because its hash no longer has four leading 0's.
  6. Block C’s hash switches because Block B’s hash was used to calculate Block C’s hash.
  7. Block C becomes invalid because its hash no longer has four leading 0's.

The only way to mutate a block would be to mine the block again, and all the blocks after. Since fresh blocks are always being added, it’s almost unlikely to mutate the blockchain.

How does blockchain truly work? I built an app to display you

How does blockchain truly work? I built an app to demonstrate you.

According to Wikipedia, a blockchain is:

A distributed database that is used to maintain a continuously growing list of records, called blocks.

That sounds nice, but how does it work?

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

Installing the Command-Line Interface version

If you haven’t already, install Knot.js.

Then run the following in your terminal:

You should see �� Welcome to Blockchain CLI! and a blockchain → prompt ready to take directions.

What does a block look like?

To see your current blockchain, come in blockchain or bc into the directive prompt. You should see a block like the pic below.

Genesis Block

Every blockchain will embark with the �� Genesis Block . As you will see later, each block on the blockchain is dependent on the previous block. So, the Genesis block is needed to mine our very first block.

What happens when a fresh block is mined?

Let’s mine our very first block. Come in mine freeCodeCamp♥︎ into the prompt.

The blockchain looks at the latest block on the blockchain for the index and previous hash. In this case Genesis block is the latest block.

How is the hash calculated?

A hash value is a numeric value of a immobilized length that uniquely identifies data.

The hash is calculated by taking the index, previous block hash, timestamp, block data, and nonce as input.

The SHA256 algorithm will calculate a unique hash, given those inputs. The same inputs will always come back the same hash.

Did you notice the four leading 0’s in the block hash?

The four leading 0’s is a minimum requirement for a valid hash. The number of leading 0’s required is called difficulty.

This is also known as the Proof-of-Work system.

What’s a nonce?

A nonce is a number used to find a valid hash.

The nonce iterates until the hash is valid. In our case, a valid hash has at least four leading 0’s. The process of finding a nonce that corresponds to a valid hash is mining.

As the difficulty increases, the number of possible valid hashes decreases. With less possible valid hashes, it takes more processing power to find a valid hash.

Why does this matter?

It matters because it keeps the blockchain immutable.

If we have the following blockchain A → B → C, and someone wants to switch data on Block A. This is what happens:

  1. Data switches on Block A.
  2. Block A’s hash switches because data is used to calculate the hash.
  3. Block A becomes invalid because its hash no longer has four leading 0’s.
  4. Block B’s hash switches because Block A’s hash was used to calculate Block B’s hash.
  5. Block B becomes invalid because its hash no longer has four leading 0's.
  6. Block C’s hash switches because Block B’s hash was used to calculate Block C’s hash.
  7. Block C becomes invalid because its hash no longer has four leading 0's.

The only way to mutate a block would be to mine the block again, and all the blocks after. Since fresh blocks are always being added, it’s almost unlikely to mutate the blockchain.

Related video:

Leave a Reply