How to create a Blockchain ecosystem?

 

    The blockchain technology is permanently evolving. It allows users to create an immutable and reliable system for recording any kinds of transactions or information. There are some key pillars to sustain the value of this technology. Those are,
   * Cryptographic Hash
   * Blockchain Validation or Mining
   * Immutable Ledger
   * Consensus Protocol
   * P2P Network
      Before we start creating the blockchain, you need to install the Flask which is the web framework to build the web application that contains the blockchain using the server. So, type the command 'pip install Flask==0.12.2" in the Anaconda prompt. The anaconda prompt is equivalent to the command-line interface in windows. Also, you need to install Postman which is the HTTP client of a user-friendly interface to interact with the blockchain. Most importantly, we will use the Get request in Postman.
      Building the blockchain has two parts. First, we need to build the architecture of the blockchain. You can display the blockchain using the Postman user-friendly functions. Then, we need to add new blocks in the blockchain. So, you need to import the libraries like DateTime which is used for the exact date of the block created and mined. The hashlib used to hash the block for the hash function. and the JSON function will be used to encode the block before hashed up. Finally, from the flask library, we will import the flask class. The jsonify will return the messages in postman when we interact with the blockchain. When we make the request to blockchain to display the whole blockchain, the jsonify will be used to display the responsive request of an index of a new block, proof of the new block, previous hash and the message that the block has been mined.
Creating the Blockchain:
         Start to build a blockchain with the class and name of the class. Inside the class, define all the components of the blockchain that includes the genesis block, chain, init function, create_block function that adds a new block which will be used to mine later on. Then, we need to add the tools to make the blockchain is solid that can not be hacked or broken. So, It is to start with init method that contains self variable and initializes the blockchain. The first block of the blockchain is the genesis block. The create block function in init function has two arguments such as proof (=1) and the link to the previous block(previous_hash = '0'). The SHA256 function in the hashlib library accepts only the string so put the previous_hash value in string format. This will also be used to append the mined block in the blockchain.


Block Structure:
                  The create_block() function will create the block and add to the blockchain. So, it has arguments of self, proof, and previous_hash.  What it has to do is to define the new block to be mined. So, create the variable with a dictionary that will define each block in the blockchain with its 4 essential keys. Those are the index of the block, timestamp, the proof of the block and previous hash. Once, you have the block, you need to append to the chain, So, simply take the list and use the append function this block. The get_previous_block() function will be used to get the last block of the current chain and returns at any time. So, it just returns self.chain[-1]. The chain-1is the last block of the chain.
Proof Of Work: 
          Next function is proof_of_work() which the miners will execute and find the proof. Proof of work is the number or piece of data the miners have to find in order to find a new block. When the miners solve the problem and find potential proof_of_work, then other miners need to verify indeed the first miner solved the problem. This needs to be implemented in this function. The miners need to consider previous proof to find the new proof once we defined the problem. Here, we make the blockchain with a simple proof of work with 4 leading zeros.
       
     First, the hash operation will generate the 64 characters using the SHA256 in hashlib function. The second part of the problem is to check the first 4 characters of the hash operations are 4 zeros. If the first 4 characters are 4 leading zeros, the miner wins and the proof will be set to true and the new_proof will be returned. If the miner loses, the new_proof will be incremented by 1 for the next check. Finally, The new_proof will return the cryptographic hash to mine new block. 
     The hash() function will hash each block so that we have more structured code. So, it will take the block as input and return the SHA256 cryptographic hash of the block which is exactly the hash_operation.

Validating the Block:
              At any time, we must able to validate the block or a chain of a block is valid in terms of integrity. The blocks need to be validated when we receive a new block from other nodes and must decide whether to accept or not. So, we must check the blocks in the blockchain has the correct proof of work that means the cryptographic hash starts with 4 leading zeros in our example. The second thing is the previous hash of each block is equal to the hash of the previous_block. By checking these 2 things, we have a valid blockchain.  
         The Is_chain_valid() function will check the whole chain of the blockchain is valid. This will return true if everything is correct in the blockchain meaning each block is the valid proof of work.  So, take the chain and iterate to check for each block of the chain. Also, We will check that the previous_hash of the current block is equal to the hash of the previous block. The second check is that the proof of each block is valid. It means that the cryptographic hash_operation must start with 4 leading zeros for each of the block. 


Blockchain Mining: 
    The Flask based web application helps to interact with blockchain by making get requests in our Postman user-friendly interface. There are 4 things you need to do in blockchain mining. Those are
   * Creating the Web Application
   * Creating the Blockchain object
   * Mining a New Block
   * Getting the Full Blockchain
           The Flask quickstart guide has all the information to start using the flask. The first step when using the flask is to create a web application. So, you need to create the object app from the flask class which is already imported from the python library. Remember, when you mine a block and want to display in postman, you need to use @app.route() function that has the requests. We have 2 requests that are mine_block() and get_chain().
          Now, we need to mine a new block by making a new request that we have in flask based web application. The mine_block() function will need to solve the proof of work problem and the keys required to create the block are index, timestamp, proof, and previous_hash. The response variable contains all the information about the block with messages. Lastly, you need to return this response to the user-friendly display of postman. We use the jsonify to return the response in JSON format with the status code of 200 which is a standard response for successful HTTP requests. This is the first function will interact with the blockchain.
        The second get request is the full blockchain to be displayed in the user interface of postman.  So, the response variable contains what will be displayed when we send the get request. Basically, it contains two keys which are chain key and the value of the key is the chain of our blockchain. This will display the whole chain. The second variable is the length of the blockchain which will count the whole block. Now, you can return the response in JSON format with HTTP status code of 200.

BlockChain Creation Demo: 


               Now, we are ready to run the code of the Blockchain implementation from our flask app. In the postman, we will test 2 requests. We start with the get_chain request to test the genesis block is created with the proof of 1 and previous_hash of 0 and we mine as many blocks as we want and get_chain for the actual state of the chain. So, run the app with the single line of code app.run() that has the arguments of host and port. The flask quickstart guide provides information about the host of  '0.0.0.0' will make the server publically available for a port no 5000. So, start to run the application at the right folder in anaconda prompt. Once the server is running, jump into postman. Then,simply make the get request and request url . The flask url(http://127.0.0.1:5000) and add the get_chain request which will create the genesis block.

           Now, you can mine for new blocks by mine_block() function. As you can see the index: 2 or second block is created. We have the previous hash and the proof. The solution to our problem is a cryptographic hash of square of the new proof minus square of the one starting with 4 leading zeros which is 533 and we have the timestamp of the block mined. Now, we request the get_chain which will give the whole blockchain. The length of the chain will be displayed at the end of the chain.

        Finally, it is to check if the blockchain is valid or not. So, it is new a get request with the name of is_valid. Now, the Is_valid() function is ready to execute this request in postman. We will use is_chain_valid method to return true or false when the blockchain is valid or not. If it is true, it will display everything is alright else it will display as we have the problem in the blockchain.

    

Comments

Post a Comment

Thanks for your comments and we are moderating your comments for publishing in our blog post.