Hacker’s way to Build of Block-Chain!(Purely-Pythonic+HyperLedger)
What is Block-Chain?
it is just a DB and it is immutable. DB has access with multiple users and they can only create new entries.
What are the potentials behind Block-Chain?
-Cross-Question->Dude! Talk to a salesman who know how to sell comb to bald. I just write code.
How block chain works? No Idea, Let’s build ONE!!
Objective: We will use IBM hyperledger and Python-flask to create a wrapper around it and this wrapper will be able to make REST calls to hyperledger. Bingo-Bascially we will hack Hyper-ledger from Pythonic way!
- Create your Business-Network.
Understanding of .bna file is really important! (You can download )
Before a business network definition can be deployed it must be packaged into a Business Network Archive (.bna) file.
Create your model file, You can use Online Playground:http://composer-playground.mybluemix.net
We’re assuming each Entity (i.e. Factory), will make use of RFID tags to store information on the food, and will scan that tag as it’s received. This information, such as timestamp, the date, and state (production, freezing, packaging, distribution) is stored on the Block-chain.
Script File From here, you will define the transaction processor functions, these are the functions that will execute when the transactions are invoked.The ChangeStateToProduction function will change the state of the current food to Production.
“Access Control” from the left pane. From here, you can determine which participants of the business network have access to which assets and transactions.
These are the important Parts of .bna file for Hyperledger-Composer! One need to understand these three very well to get to know more about “A Smart Contract!”
1. Setting up the Environment(On Ubuntu 16.04 )
Download and install all required Pre-requisites:
Run the script:
Essential CLI tools
npm install -g composer-cli
Utility for running a REST Server on your machine to expose your business networks as RESTful APIs:
npm install -g composer-rest-server
Useful utility for generating application assets:
npm install -g generator-hyperledger-composer
Yeoman is a tool for generating applications, which utilises generator-hyperledger-composer:
npm install -g generator-hyperledger-composer
npm install yeoman-generator
npm install -g yo
npm install -g composer-playground
Install Hyperledger Fabric
This step gives you a local Hyperledger Fabric runtime to deploy your business networks to.
- In a directory of your choice (we will assume ~/fabric-tools), get the .zip file that contains the tools to install Hyperledger Fabric:
mkdir ~/fabric-tools && cd ~/fabric-tools
tar.gz is also available if you prefer: just replace the
.zip file with
fabric-dev-servers.tar.gz1 and the
unzip command with a
tar xvzf command in the above snippet.
- Use the scripts you just downloaded and extracted to download a local Hyperledger Fabric runtime:
Start the fabric:
Generate a PeerAdmin card:
You can start and stop your runtime using
~/fabric-tools/stopFabric.sh, and start it again with
At the end of your development session, you run
~/fabric-tools/stopFabric.sh and then
~/fabric-tools/teardownFabric.sh. Note that if you’ve run the teardown script, the next time you start the runtime, you’ll need to create a new PeerAdmin card just like you did on first time startup.
Deploying the Business Network
After creating the
.bna file, the business network can be deployed to the instance of Hyperledger Fabric. Normally, information from the Fabric administrator is required to create a
PeerAdmin identity, with privileges to deploy chaincode to the peer. However, as part of the development environment installation, a PeerAdmin identity has been created already.
After the runtime has been installed, a business network can be deployed to the peer. For best practice, a new identity should be created to administrate the business network after deployment. This identity is referred to as a network admin.
PeerAdmin business network card with the correct credentials is already created as part of development environment installation.
Deploying a business network to the Hyperledger Fabric requires the Hyperledger Composer chaincode to be installed on the peer, then the business network archive (.bna) must be sent to the peer, and a new participant, identity, and associated card must be created to be the network administrator. Finally, the network administrator business network card must be imported for use, and the network can then be pinged to check it is responding.
- To install the composer runtime, run the following command:
composer runtime install --card PeerAdmin@hlfv1 --businessNetworkName pizza-on-the-blockchain
composer runtime install command requires a PeerAdmin business network card (in this case one has been created and imported in advance), and the name of the business network.
- To deploy the business network, from the
pizza-on-the-blockchaindirectory, run the following command:
composer network start --card PeerAdmin@hlfv1 --networkAdmin
admin --networkAdminEnrollSecret adminpw --archiveFile
firstname.lastname@example.org --file networkadmin.card
composer network start command requires a business network card, as well as the name of the admin identity for the business network, the file path of the
.bna and the name of the file to be created ready to import as a business network card.
- To import the network administrator identity as a usable business network card, run the following command:
composer card import --file networkadmin.card
composer card import command requires the filename specified in
composer network start to create a card.
- To check that the business network has been deployed successfully, run the following command to ping the network:
composer network ping --card admin@pizza-on-the-blockchain
composer network ping command requires a business network card to identify the network to ping.
Hyperledger Composer can generate a bespoke REST API based on a business network. For developing a web application, the REST API provides a useful layer of language-neutral abstraction.
- To create the REST API, navigate to the
pizza-on-the-blockchaindirectory and run the following command:
admin@pizza-on-the-blockchainas the card name.
- Select never use namespaces when asked whether to use namespaces in the generated API.
- Select No when asked whether to secure the generated API.
- Select Yes when asked whether to enable event publication.
- Select No when asked whether to enable TLS security.
The generated API is connected to the deployed blockchain and business network.
Once the REST server is up and running, head over to https://localhost:3000/explorer
- Ensure Python is installed on your local environment (Both Python 2 and Python 3 are supported).
- Install the requirements using the command
pip install -r requirements.txt.
- Run the application as:
- Point your web browser to the address
One may Enjoy Different parts of My Application here.