Building a Blockchain and Cryptocurrency
This section discusses the detailed steps to build a blockchain and create cryptocurrency.
The Secure Hash Algorithm-256-bit is an algorithm that generates a cryptographic hash value for a text or file or transaction. This is used in blockchain to ensure the immutability of data. This algorithm creates a hash of length 256-bit that is unique
FIGURE 12.5 A 3D Pie Chart Showing Distribution Based on Gender.
FIGURE 12.6 A 3D Pie Chart Representing Analysis of Administration of Insulin.
for a particular content. It is not an encryption; instead, it transforms input data to hash. It is one-way, i.e., information cannot be obtained from its hash. This algorithm generates the same hash for the same data and different hash for even a small change in the data. This hash can further be signed by encryption, using a private key. This digital signature is authenticated by other participants of the blockchain, by decryption of the signature, using their public key. The hash value generated is compared with original hash, a process that ensures that the data has not been altered.
A peer-to-peer network or system is a distributed and decentralized architecture. The workload is shared among peer nodes. Resources such as pow'er, bandwidth, storage space, and access to other peers are equipped w'ithin each peer. A peer is capable of both transmitting and receiving data. This kind of network has gained importance, as it has the capability to share resources and also look up peers with the required amount of resources to perform a specific task.
Byzantine Fault Tolerance (BFT)
It is a challenging task to design a reliable system with a secure consensus, in the presence of faulty nodes and processes in the network. Since blockchain is a distributed system, it is crucial to find malicious nodes or nodes that fail to operate. A byzantine-fault-tolerant system is capable of operating efficiently in the presence of faulty nodes. The Byzantine Generals' Problem can be solved in several ways. One promising solution is the appropriate choice of consensus mechanism. A consensus mechanism should be able to deal with byzantine fault issues. BFT implies that two hubs can convey information securely over a network, understanding that they are showing same information.
Proof of Work (PoW) Consensus
A PoW is a computationally intensive task executed by miners to add new block into the blockchain. Miners receive rewards for generating hash, performing the task of adding valid blocks.the and linking hash of a new block to the previous block. All verified transactions are arranged in blocks. Every new block is validated, and consensus among the majority of nodes is obtained before a new block is added into the blockchain. The previous hash value for the genesis block is zero, and from then on, the hash value of block 1 becomes the previous hash value for the second block. This is indicated in Figure 12.7.
Creating Smart Contracts
A smart contract is code written to contain terms and conditions of exchange, agreed to by the sender and receiver of files in a blockchain network. A smart contract is stored in the blockchain with a unique address. It completely eradicates the need for a middleman and makes the exchange more trustworthy and irreversible. It is an agreement between the nodes, which is a legal framework that is automatically executed. It is implemented using solidity language in an Ethereum virtual machine.
FIGURE 12.7 Details of Block in Blockchain.
This is a virtual Ethereum blockchain, which is used to set up addresses, smart contracts, and use functions to run and test the workings of a blockchain network. This can be used to control the functioning of a planned blockchain.
We create a cryptocurrency for interacting with the blockchain and postman on the three nodes we have in the network. We run each node on a different console, which is equivalent to having the computers connected to different servers. The steps performed are as follows:
- • Creation of chain of blocks: Perform a GET request on node-1 using the get_ chain function. This will check to see if the genesis block was well created. Similarly, perform a get_chain request on node-2. Similar to node-1 and node-2, perform a get_chain request on node-3, to verify that the genesis block is created.
- • Set the requests to POST and call the connect_node function to create a connection between the nodes. Node-2 is connected to node-1 and node-3 is calling the connect_node POST request. Similar to node-2, node-3 is also connected to the other nodes, and therefore, all the nodes are connected to one another in the network.
- • Mine a block in the first node by calling mine_block GET request. This will mine a block in the blockchain. Once the block is mined, we perform get_chain request on the node to obtain all the details of that node. We can see that two blocks are there in the chain. One is the genesis block and the other is the block that was just mined.
- • Perform replace_chain() and GET request on other nodes, to replace the chain with the longest chain in the network, thereby following consensus among all the nodes. The replace_chain request can then be verified by the get_chain request. We observe that the chain is now replaced with the longest chain in the network. The add_transaction POST request is a request that is made to add a particular transaction, i.e., EHR to the chain. Here, we specify the amount to be transacted between the sender and the receiver. Once the transaction is added, perform the get_chain request to view the blocks in the chain. This helps to verify if the transaction made is being added to the chain or not.