Blockchain – First Level Implementation Guide for System Architects


Do you know why you are here? Yes! I shared this link to you and you opened it! If you are interested in designing or constructing Blockchain Network, this page may be your alphabet of your idea or innovation. Here you can also learn and understand how Blockchain can be designed for your Business Requirements.

I hope that you are already familiar with Ledger, Distributed Database, Cryptography, Hashes and Decentralization from my last article. If you don't have any idea, No Problem! I will try to explain my design by providing simple algorithms to implement Blockchain Network for adding Transactions, Blocks, Mining and Chaining.

I want to start this article with a fundamental which may answer why Blockchain will be inevitable. I start with few basic questions.
  • Why are you using Coins and Currencies rather they are just metals and papers? And why are they over-valued?
  • Why are you depositing your Money into Banks? Are they your relatives or friends?
Reason is that You Trust Them.
  • You trust your Government
  • You trust your Financial Regulatory Authority
  • You trust your Bankers
Commonly you trust a system or organization. What will happen if all your trusts are replaced with technology? It happened before and it is called Evolution. Blockchain is the trust which protects data from third-party and increases the efficiency of transaction without any Government, Regulations and Bankers involvement.

This is the article for System Architects to design Blockchain Network so I want to move to the topic directly. I have already explained the core concepts in my previous articles with Cryptocurrency and please refer if you would like to.

Before Getting Started

We need to define our goal and strategy for our further sections. This step will narrow down to design and learn Blockchain Server and Client implementations. 

Usecase: 
  • Creating a new transaction with Sender and Receiver details
  • Adding them into Blockchain Network by verifying the transactions
  • Data should be updated in all Blockchain Hosts
Technology Requirements:
  • Blockchain Server: Python 3.7
  • HTTP Client: Postman
  • IDE: PyScripter
Technical Implementation:
  • Blockchain Server Class Building with Creating Block, Adding Transactions and Mining methods
  • Developing HTTP requests to communicate with Blockchain functions from Python Flask
  • Blockchain API Integration and Data Decentralization

Blockchain Implementation

Blockchain Server Design

Can we directly start with code? Ok,

This is the main class of our Blockchain which takes charge on maintaining entire Blockchain Network. We will see the definition and need of all core methods and helper methods.

New Block

Block consists of Index which helps to manage or assign unique ID to each Block. Timestamp helps to get current date and time of Block Creation. Block is attached with Hash and it also has information about previous Hash. Rather very first block in the network does not have any previous Hash References.

New Transactions

Transaction is the data part which consists of encoded details of the transactions. In our usecase, each transaction must have Sender, Receiver and Amount details which should be added into New Block.

Proof of Work for Mining

Miners Proof is the core concept of Blockchain which creates artificial complexity to identify the key to verify transactions by Minors. Until identifying valid proof, miner's also continuously running their proof of work algorithm. For making this step easier, I made it to four zeros.

Blockchain API Interaction from HTTP Client

There is a framework in Python called Flask which provides facility to access Python functions from HTTP Requests. For accessing our BlockchainCore Class, I am going to create three methods or links which are for Creating New Transaction to a Block, Mining New Block and Adding New Block into Core Blockchain.

Adding New Transaction


In new transaction link, details of Sender, Receiver and Amount will be sent from HTTP Client in POST Method. As a client, we submit the required details to BlockchainCore API.

Data Mining Method

This is our PoW implementation and miners will run the script for finding Hash. Once Hash is identified, New Block is forged with transaction details and Hash details. For ensuring the chain, few basic validation has been done from Server side like Previous Hash / Last Proof.

Maintaining Chain

Chain link is requested to add the newly identified Block with Transaction into existing chain. Once New Block is created with transaction successfully then it should be added and maintained in core Blockchain network. Chain method is called to perform this task.

Running Blockchain Network


1. Run BlockchainCore.py in 172.10.20.5:9000
2. Initiate HTTP Client from Postman http://localhost:9000/transaction/new (Adding New Transaction)
3. Initiate HTTP Client from Postman http://localhost:9000/mine (Data Mining Method)
4. Initiate HTTP Client from Postman http://localhost:9000/chain (Maintaining Chain)

Result View
 "chain": [
    {
      "index": 1,
      "previous_hash": 1,
      "miners_proof": 100,
      "timestamp": ,
      "transactions": []
    },
    {
      "index": 2,
      "previous_hash": ,
      "miners_proof": ,
      "timestamp": ,
      "transactions": [
        {
          "amount": 1,
          "recipient": ,
          "sender": ,
        }
      ]
    } ]

Conclusion

I don't still talk about decentralization in this article because I just wanted my code to be worked with sample PoC transactions. Probably I will talk about Cryptocurrency implementation in my BlockchainCore class with multiple Host Systems. In the mean time, please comment your suggestions and queries for improving my base class or methods. Thanks for your time. Good Day!

Share:

1 comment:

Categories

Follow Me