A Blockchain is a distributed system or ledger between computer network nodes and it serves as a digital database for information storage. The most well-known use of blockchain technology is for preserving a secure and transparent record of transactions in cryptocurrency systems like Bitcoin. Without the aid of a reliable third party, it ensures the security and authenticity of a data record and fosters confidence. It aims to make it possible to share and record digital information without editing it. This is an innovative technology that serves as the basis for immutable ledgers, or records of transactions that cannot be changed, removed, or destroyed. That is why it is also referred as distributed ledger technologies because of this (DLT).
A plug-and-play blockchain platform called Hyperledger Fabric serves as a backbone for creating blockchain-based goods, services, and applications that are intended for usage by private businesses. With several significant advantages over other well-known distributed ledger or blockchain technologies, Hyperledger Fabric is an open-source permissioned enterprise-grade DLT platform created for business contexts.
Instead of supporting domain-specific languages, Fabric enables general-purpose programming languages like Java, Go, and Node.js in smart contracts (DSL).
Because of Fabric’s support for pluggable consensus methods, the platform may be more easily customized to meet specific use cases and trust models. Thus, we can employ fully byzantine fault tolerant, crash tolerant, conventional byzantine fault tolerant, etc. Fully byzantine fault tolerant consensus may be viewed in some cases as superfluous and a significant performance throughput bottleneck. A crash fault tolerant (CFT) consensus protocol may be more appropriate in those circumstances, but a multi-party, decentralized use case may call for a more conventional byzantine fault tolerant (BFT) consensus system.
At an excessive level, Fabric is constituted of the subsequent modular components:
Before we begin:
Before starting the test network, clone the fabric-samples repository, and download the Fabric images. Since we follow the fabric samples provided by Fabric community through out the session.
Fabric has provided a test network for learning by running nodes on your local machine. Developers can use the network to test their applications. The network is only a tool for Learning and testing and not as a model for how to set up a network. Modifications to the scripts are not appreciated and that may even break the network. It has limited configuration that should not be used as a template for deploying a production network:
The below command installs the Hyperledger Fabric platform-specific binaries and config files for the version into the /bin and /config directories of fabric-samples:
curl -sSL https://bit.ly/2ysbOFE | bash -s — 2.2.2 1.4.9
Navigate to the test network directory by using the following command:
cd fabric-samples/test-network
From test-network directory, run the following command to remove any containers or artifacts from any previous runs:
./network.sh down
You can then bring up the network by the following command. You will experience problems if you try to run the script from another directory, so make sure you are inside test-network directory:
./network.sh up
This will create a Fabric network that consists of two peer nodes, one ordering node. If the command completes successfully, you will see the logs of the nodes being created:
After the test network is deployed, Run the following command to list all Docker containers running on your machine. You should see the three nodes that were created by the network.sh script:
docker ps –a
Peers are the fundamental components of any Fabric network. Peers store the blockchain ledger and validate transactions before they are committed to the ledger. Peers run smart contracts that contain the business logic that is used to manage the assets on the blockchain ledger.
Every peer in the network belongs to an organization. In the test network, includes two peer organizations, Org1 and Org2. Each organization operates one peer each in the network, peer0.org1.example.com and peer0.org2.example.com.
Every Fabric network will have an ordering service. Test network also includes a single orderer organization that maintains the network’s ordering service. Peers validate transactions and add blocks of transactions to the blockchain ledger, they do not decide on the order of transactions or include them into new blocks. On the distributed network, peers may be running far away from each other and not have a common view of when a transaction was created.
We have peer and orderer nodes running on our machine, we can use the script to create a Fabric channel for transactions between organizations Org1 and Org2.A private layer of communication between specific network members is called channels. Channels can be used only by organizations invited to the channel and invisible to other members of the network. Each channel will have a separate blockchain ledger. Organizations that have been invited “join” their peers to the channel to store the channel ledger and validate the transactions on the channel.
Use the network.sh script to create a channel between Org1 and Org2 and join their peers to the channel. Run the bellow command to create a channel with the default name of mychannel:
./network.sh createChannel
If the command was successful, the following message printed in your logs:
========= Channel successfully joined ===========
Use the channel flag to create a channel with a custom name. Example, the following command would create a channel with name channel1:
./network.sh createChannel -c channel1
The channel flag also allows you to create different channels by specifying different channel names. After the creation of first channel, use the below command to create a second channel named channel2:
./network.sh createChannel -c channel2
If you want to bring up the network and create a channel in a single step, use the command:
./network.sh up createChannel
Smart contracts contain the business logic that governs assets on the blockchain ledger. The network can invoke smart contracts to create assets on the ledger, change and transfer those assets and query smart contracts to read data on the ledger.
In Fabric, smart contracts are deployed on the network in packages called chaincode. A Chaincode is first installed on the peers of an organization and then deployed to a channel. This can then be used to endorse transactions and interact with the blockchain ledger. Before a chaincode can be deployed to a channel, its members must agree on a chaincode definition that establishes it. Once the required number of organizations agree, the chaincode definition can be committed to the channel, and the chaincode is ready to be used.
Once you have created a channel, you can start a chaincode on the channel using the following command:
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
Or
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode- javascript -ccl javascript
The deployCC subcommand will install the asset-transfer (basic) chaincode on peer0.org1.example.com and peer0.org2.example.com and then deploy the chaincode on the channel specified using the channel flag (or mychannel if no channel is specified). If chaincode deployment is done for the first time; the script will install the chaincode dependencies. Use the language flag, -ccl, to install the Go, typescript or javascript versions of the chaincode. The asset-transfer (basic) chaincode in the asset-transfer-basic folder of the fabric-samples directory. This folder contains sample chaincode provided as examples and used by tutorials to highlight Fabric features.
After you bring up the test network, use the peer CLI to interact with your network. The peer CLI allows to invoke deployed smart contracts, update channels, or install and deploy new smart contracts from the CLI.
Make sure that operations are done from the test-network directory. The peer binaries will be there in the bin folder of the fabric-samples repository. Use the following command to add those binaries to your CLI Path:
export PATH=${PWD}/../bin:$PATH
Set the FABRIC_CFG_PATH to point to the core.yaml file in the fabric-samples repository:
export FABRIC_CFG_PATH=$PWD/../config/
You can now set the environment variables that allow you to operate the peer CLI as Org1:
# Environment variables for Org1 export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp export CORE_PEER_ADDRESS=localhost:7051
If ./network.sh deployCC -ccl go, is used to install, and start the asset-transfer (basic) chaincode, invoke the InitLedger function of the (Go) chaincode to put an initial list of assets on the ledger (if using typescript or javascript ./network.sh deployCC -ccl javascript for example, invoke the InitLedger function of the respective chaincodes).
Run the following command to initialize the ledger with assets:
peer chaincode invoke -o localhost:7050 –ordererTLSHostnameOverride orderer.example.com –tls –cafile”${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem” -C mychannel -n basic –peerAddresses localhost:7051 –tlsRootCertFiles”${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt” –peerAddresses localhost:9051 tlsRootCertFiles”${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.cr” -c ‘{“function”:”InitLedger”,”Args”:[]}’
If successful, you should see similar output to below:
-> INFO 001 Chaincode invoke successful. result: status:200
Query the ledger from your CLI. Run the following command to get the list of assets that is added to the channel ledger:
peer chaincode query -C mychannel -n basic -c ‘{“Args”:[“GetAllAssets”]}’
If successful, you should see the following output:
[ {"ID": "asset1", "color": "blue", "size": 5, "owner": "Tomoko", "appraisedValue": 300}, {"ID": "asset2", "color": "red", "size": 5, "owner": "Brad", "appraisedValue": 400}, {"ID": "asset3", "color": "green", "size": 10, "owner": "Jin Soo", "appraisedValue": 500}, {"ID": "asset4", "color": "yellow", "size": 10, "owner": "Max", "appraisedValue": 600}, {"ID": "asset5", "color": "black", "size": 15, "owner": "Adriana", "appraisedValue": 700}, {"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800} ]
Chaincodes are invoked when a network member wants to transfer or change an asset on the ledger. Using the following command to change the owner of an asset on the ledger by invoking the asset-transfer (basic) chaincode:
peer chaincode invoke -o localhost:7050 –ordererTLSHostnameOverride orderer.example.com –tls –cafile”${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem” -C mychannel -n basic –peerAddresses localhost:7051 –tlsRootCertFiles”${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt” –peerAddresses localhost:9051 –tlsRootCertFile”${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt” -c ‘{“function”:”TransferAsset”,”Args”:[“asset6″,”Christopher”]}’
If the command is successful, you will get the following response:
2019-12-04 17:38:21.048 EST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
The endorsement policy for the asset-transfer (basic) chaincode requires the transaction to be signed by Org1 and Org2. The chaincode invokes command needs to target both peer0.org1.example.com and peer0.org2.example.com using the –peerAddresses flag. Since TLS is enabled for the network, the command also needs to reference the TLS certificate for each peer using the –tlsRootCertFiles flag.
After we invoke use another query to check how the invoke changed the assets on the blockchain ledger. Since we already queried the Org1 peer, query the chaincode running on the Org2 peer. Following is the environment variables to operate as Org2:
# Environment variables for Org2 export CORE_PEER_TLS_ENABLED=true export CORE_PEER_LOCALMSPID="Org2MSP" export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp export CORE_PEER_ADDRESS=localhost:9051
Query the asset-transfer (basic) chaincode running on peer0.org2.example.com:
peer chaincode query -C mychannel -n basic -c ‘{“Args”:[“ReadAsset”,”asset6″]}’
The result will show that “asset6” was transferred to Christopher:
{“ID”:”asset6″,”color”:”white”,”size”:15,”owner”:”Christopher”,”appraisedValue”:800}
You can bring down the network with the following command:
The above command will stop and remove the node and chaincode containers, delete the organization crypto material, remove the chaincode images from your Docker Registry. It will also remove the channel artifacts and docker volumes from previous runs.
The goal of Hyperledger is to provide an environment that makes it easier for businesses to use Blockchain technology. It also aims to provide simple APIs for development and interoperability. Although novel and difficult, the concepts are straightforward, modular, and simple enough for IT and business people to understand. Hyperledger Blockchain can significantly improve your company network if you need to manage relationships, track transactions, have unbiased control, boost transparency, or if your system is prone to errors because of manual processes.