The buzz surrounding the cryptocurrency is increasing every day, ever since Bitcoin, the first cryptocurrency was launched in the market. Everyone is interested in knowing crypto-related news, investments, blogs; but only a few think out of the box and consider to develop/ build their own cryptocurrency.
If you are thinking to build a new cryptocurrency, we guide you through the step by step instructions on building a digital currency with blockchain. Let’s get started with a basic concept of the blockchain.
The concept of blockchain is quite simple which has a distributed database and maintains a continuously increasing list of records. In this article, we are implementing a basic version of such blockchain.
STEP 1: Define a Block Structure
Start by defining the block structure. The essential properties have to be included at the block at this point. The basic properties of the block are described in the following:
- Index – It is the height of the block in the blockchain
- Data – It is the information which is included in the block.
- Hash – The hash taken from the content of the block, for example, SHA256.
- Previous Hash – It is the reference to the hash of the previous block, and the value will explicitly define the previous block.
General code for the block structure appears like the following:
STEP 2: Block Hash
The block hash is one of the essential property of the block, which is calculated using the overall data of block. If anything in the block changes, the primary hash is no longer valid. For example, blocks with the identical index can appear, but each block has unique hashes.
The following code is the example to calculate the hash of the block:
The block hashes are used to save the integrity of the block and to refer the previous block explicitly.
The important outcome of the properties and the previous hash is that a block cannot be altered without modifying the hash of each consecutive block.
The example is demonstrated below, if the data in block 44 is modified from ‘DESERT’ to ‘STREET,’ all the hashes of the consecutive blocks will be changed; since the previousHash value is dependent on the hash of the block.
STEP 3: Genesis Block
The very first and the only block that has no previousHash in the blockchain is called Genesis block. The following is the hard code of the genesis block which is coded to the source code:
STEP 4: Generating a Block
To generate a block, one should learn the hash of the previous block and formulate the rest of the necessary content [= index, hash, data and timestamp]. Block data is provided by the end-user, but the rest of the parameters will be created using the following code:
STEP 5: Store the Blockchain
STEP 6: Validate the Integrity of the Blocks
At any time, you must be able to validate a block or chain of blocks accurately in terms of integrity. It can be possible, especially when we accept new blocks from other nodes and must decide whether to allow them or not.
For a block to be valid, you need to make sure the following points are applied:
- The index of the block must be a number higher than the previous block.
- The previousHash of the block must resemble the hash of the previous block
- The hash of the block itself should be a valid one.
The code is demonstrated below:
You must validate the structure of the block so that distorted content sent by a peer will not crash your node.
Now you have the means to validate a single block; you can now validate a full chain of blocks. First, check whether the first block in the chain matches with the genesis block, and later you can validate each consecutive block with the help of the above-described methods. The code is demonstrated as:
STEP 7: Always Choose the Longest Chain
A chain should always have only a single set of blocks at a given time. In case of conflicts (for ex: two nodes generate block number 72), you must prefer the chain which has the longest number of blocks. In the below illustration, the data introduced in block 72 is a350235b00 and it will not be entered in the blockchain since it will override the longer chain.
The above logic is implemented in the following code:
STEP 8: Node to Node Communication
The fundamental part of a node is to share and sync the blockchain with other nodes. The following rules have to be followed to maintain the sync.
- When a node produces a new block, it will broadcast it to the network
- When a node connects to a new match, it queries for the latest block
- When a node confronts a block that has an index greater than the current known block, it either adds the block that is a current chain or queries for the full blockchain.
You must use WebSockets for the peer-to-peer communication. The active sockets for each node are filed in the const sockets: WebSocket variable. You cannot use automatic peer discovery. The locations [= WebSocket URLs] of the peers need to be added manually.
STEP 9: Control the Node
By setting up an HTTP server, the user can control the node.
The user can interact with the node in the following ways:
- List all the blocks
- Create a new block with a content provided by the user
- Add or List peers
The most sincere way to handle the node is with Curl:
STEP 10: Architecture
It can be seen that the node exposes two web servers i.e. one for the user to handle the node (HTTP server) and other for the peer-to-peer communication between the nodes. (WebSocket HTTP server)
The article explains a basic version of blockchain and demonstrates how the basic principles of blockchain that can be implemented easily to create a cryptocurrency.