Blockchain from the Command Line

769 VIEWS

To make blockchain even simpler, Sean Han created blockchain-cli. This minimalistic blockchain command line mimics the Bitcoin blockchain and is meant for educational purposes. (Note that this is not the official Bitcoin headless daemon, bitcoind.) blockchain-cli is a node application built with Vorpal, CryptoJS, and Peer Exchange. In this post, we’ll look at this nifty command line tool to help you become familiar with the blockchain.

The blockchain is a revolutionary piece of technology. The release of Bitcoin, which popularized the blockchain, has spawned several innovations that make use of blockchain technology. For more on Blockchain and Blockchain-related innovations, you should check out the following articles: What You Should Know About the Blockchain, Meet Etherscan, Building Your First Blockchain Application, and Picking a Blockchain Technology. (You should regularly check out Sweetcode’s blockchain section for the latest blockchain content.)

Installation

You need to have Node.js installed to install and run the bitcoin-cli. You can also clone the GitHub project.

npm install -g blockchain-cli

Once installed successfully, you should have the command blockchain available to you. Enter blockchain in the terminal to start up the tool. You should be greeted with an interface similar to the one below.

Begin by entering blockchain to show the current state of the blockchain. This command has an alias: bc. You can look at the aliases for the various commands by typing help . You should be presented with a table denoting the Genesis Block.

A little explanation is needed here. The Genesis Block is the first block of the blockchain. It is usually hardcoded into the application. As such, the hash of the block before the Genesis Block is zero (in our case, no block preceded it).

Here is an explanation of each section of the table:

  1. Previous Hash: to create a chain that ensures the blockchain is hack-proof, each block is hashed. A new block that gets added links to the block before it by storing the hash of the previous block in this field.
  2. Timestamp: indicates when a block was added. Bitcoin uses Unix time (different from what we see here) for its timestamp. A timestamp adds additional security and serves as a source of variation for the block hash.
  3. Data: the content of the block. In Bitcoin’s case, it is the transaction data.
  4. Hash: the hash value of the block content
  5. Nonce: number required to find the solution that validates a block

The next command we examine is mine . To add something to the blockchain, we invoke the command and pass the data to it. Say, for example, that you want to add the name Alice to the blockchain:

Notice that it took some time for the block to be generated. This mimics what happens on the Bitcoin blockchain, which is mostly slower.

When you look closely, you see that a new block has been created with Alice as data, and the previous hash field now holds the value of the hash of the Genesis Block. The next added block will have the Alice block’s hash as its previous hash.

Mining is the process of adding transactions—data, in our case—to the blockchain and is a means of introducing new Bitcoins. Miners compete to solve a computationally complex problem that validates a block and adds it to the blockchain for which they also receive some amount of Bitcoin as reward.

We go on to cover the remaining commands below.

The blockchain is decentralized. No one has complete power over it. Anyone can become a miner if they have sufficient computational power and have their own copy of the blockchain.

So far, only you have been using the blockchain. To simulate Blockchain’s decentralized nature, we need to add other people (peers).

For this example, we’ll run two (or more) different terminals and connect them.

# TERMINAL 1


#TERMINAL 2


The state of the blockchain after this should give the same result from either the first or second terminal.

peers and discover are quite straightforward and easy to try on your own.

Final Thoughts

Blockchain-cli makes the concept of blockchain less arcane and rather fun. The tool is very helpful. I highly recommend it if you want to explain blockchain (especially to newbies). I hope you have a better understanding of a blockchain now.


Bruno is a junior at Ashesi University College studying Computer Science. He is interested in leveraging the power of technology to increase productivity. As a big fan of open source technology, he is currently exploring the possibility of using the Bitcoin Blockchain to fight corruption in government.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu