Overview

Skill Level: Any Skill Level

In this recipe, we will explore how to start up a network using CouchDB and then look at transactions applied into CouchDB from a web User Interface.

Ingredients

To follow and complete this recipe, you need to have good knowledge of blockchain, Hyperledger transactions, JavaScript as well as basic knowledge of Linux.

Step-by-step

  1. Hyperledger Fabric Overview

    Hyperledger Fabric is an open source enterprise-grade platform that leverages a highly-modular and configurable architecture. Hyperledger Fabric is optimized for a broad range of industry use cases, including the finance, banking, healthcare, insurance, and public sectors, as well as supply chains and digital asset management.
    For those who are not familiar with Hyperledger project Intro to Hyperledger Family and Hyperledger Blockchain Ecosystem, Hyperledger Design Philosophy and Framework Architecture, The Survey of Hyperledger Fabric Architecture and Components for Blockchain Developers and Overview of Building Blockchain Smart Contracts in Hyperledger articles are strongly recommended.
    Hyperledger Fabric supports Smart Contact development in general-purpose programming languages, such as JavaScript, Java, Go, and Node.js. Hyperledger Fabric is also operating under a governance model to build trust between participants on a shared network.
    In our previous recipes, we have successfully installed Hyperledger Fabric on an AWS EC2 virtual machine and set up the first Hyperledger Fabric network. We learned the following:

    • Generating the crypto/certificate using cryptogen
    • Generating the configuration transaction using configtxgen
    • Bring up the nodes based on what is defined in the docker-compose file
    • Using the CLI to set up the first network
    • Using the CLI to install and instantiate the chaincode
    • Using the CLI to invoke and query the chaincode

    We also learned how to add a new Organization to a Channel in Hyperledger Fabric.
    In this recipe, we will explore how to start up a network using CouchDB and then look at transactions applied into CouchDB from a web UI. To successfully execute this recipe, you need install the Hyperledger Fabric with samples and binaries on the AWS EC2 instance.

  2. Run CouchDB

    To use CouchDB, follow these steps:
    1. Make sure network is not up. If it is up, shut down the network, as shown here:

    cd fabric-samples/first-network

     sudo ./byfn.sh down

    2. Start up the BYFN network using CouchDB:

    Here we will start up the network by using the CouchDB database.

    $ cd fabric-samples/first-network

    $ sudo ./byfn.sh up -c mychannel -s couchdb

    Following screenshot shows our network starting up:
    Hyperledger Fabric and CouchDB Web UI

     

    3. Install chaincode by navigating into the CLI container using the command-line interface:

    $ sudo docker exec -it cli bash

    peer chaincode install -n marbles -v 1.0 -p github.com/chaincode/marbles02/go

     

    4. Instantiate the chaincode:

    $ export CHANNEL_NAME=mychannel

    peer chaincode instantiate -o orderer.example.com:7050 –tls –cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/

     

    crypto/ordererOrganizations/example.com/orderers/ orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

    -C $CHANNEL_NAME -n marbles -v 1.0 -c ‘{“Args”:[“init”]}’ -P “OR (‘Org0MSP.peer’,’Org1MSP.peer’)”

    5. Invoke the chaincode. The following commands invoke chaincode to create marble.

    peer chaincode invoke -o orderer.example.com:7050 –tls –cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/ crypto/ordererOrganizations/example.com/orderers/ orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -c ‘{“Args”:[“initMarble”,”marble5″,”blue”,”35″,”tom”]}’

     

    Following screenshot shows successful creation of chanincode
    Hyperledger Fabric and CouchDB Web UI

     

    6. Open the CouchDB UI by navigating to http://host-ip:5984/_utils/#/_all_dbs (in my case, my AWS public IP address is 3.91.245.92, so the URL is http://3.91.245.92:5984/_utils/#/_all_dbs):
    Hyperledger Fabric and CouchDB Web UI

     

    In order to allow public access to CouchDB, we need open port 5984. Navigate to the AWS security group under the instance, launch the wizard, and choose Action | Edit Inbound Rules | Add Inbound Rule. This is shown as follows. After this, click Save. You can follow the below example to allow all IP address to access CouchDB:
    Hyperledger Fabric and CouchDB Web UI

     

    7. From mychannel_marbles, we can query and see the transaction ID with marble5:
    Hyperledger Fabric and CouchDB Web UI

     

    8. Click marble5, and you will see the default marble5 files:
    Hyperledger Fabric and CouchDB Web UI

     

    Here, we saw how to use CouchDB to view how transactions get created, and updated them on the Fabric network. We will write a smart contract and deploy it as an application in the next recipe.

  3. Put Things Together

    In this recipe, we learned how to use CouchDB as the state database with Hyperledger Fabric. We also looked at how to use CouchDB to deploy Marbles to the network.
    Hyperledger Fabric supports two types of peer databases: LevelDB is the default state database embedded in the peer node and stores chaincode data as simple key-value pairs; and CouchDB is an optional alternate state database that supports rich queries when chaincode data values are modeled as JSON. This recipe describes the steps required to use CouchDB as the state database with Fabric. CouchDB is a JSON document datastore rather than a pure key-value store, therefore enabling indexing of the contents of the documents in the database.

    In the last recipe, we will show you how to write your first smart contract application and deploy it into the blockchain.
    This recipe is written in collaboration with Brian Wu who is a senior Hyperledger instructor at Coding Bootcamps school in Washington DC.

Join The Discussion