Blockchain

IMPORTANT: Go to Challenge Hub to register for points.


Last updated: May 15 16:00

IBM Blockchain Challenge


Level 1: Getting Started

Level 1.1: Reading materials

Follow these links to learn about Blockchain and Hyperledger Fabric.

  1. IBM Blockchain
  2. Hyperledger

Get points: If you are participating in the hackZone challenge and would like to earn points, please return to the Advocate Hub and answer questions based on this level.

Level 1.2: Prepare the lab environment

  1. Obtain a server from the Challenge Hub if you have not done so already.
  2. Check for an email from HACKZONE that contains information and credentials needed for you to continue.
  3. Start an SSH terminal (e.g. putty on windows, or terminal in Linux or Mac) and connect to the IP address of your assigned server.
  4. Login to user “blockchain” using the supplied password.
  5. Switch to the “root” user.
    sudo –i            
  6. The Hyperledger fabric code has already been installed for you. Start the fabric code by executing the following script.
    startfabric              

    This will start four peers (Docker containers) and other required services. You can monitor the progress by checking the /tmp/startfabric.log file.

  7. Verify that the peers started successfully by executing the following command.
    dockerps              

    Output should be similar to the following:

    Image:

    The Docker containers are now started and the peers in the network are communicating.

  8. Check the output log of the certificate authority.
    docker logs dockercompose_membersrvc_1 

    Output should be similar to the following:

    Image:

    Question: What is ECA and what functions does the administrator interface provide?

    Question: What is TCA and what functions does the administrator interface provide?

  9. Check the output log of the vp0 peer (leader node in the PBFT consensus). Since the amount of output is large, write the results to a log file.
    docker logs dockercompose_vp0_1 &> /tmp/vp0.log

    View the log file and find the answers to the following questions.

    less /tmp/vp0.log

    Question: What type of consensus is used by the fabric?

    Question: Is Security enabled?

    Question: Is Privacy enabled?

    Question: Peer vp0 is registered as which user?

    Exit the log file.

    q

Get points: If you are participating in the hackZone challenge and would like to earn points, please return to the Advocate Hub and answer questions based on this level.

Level 2: Marbles Application

Level 2.1: Overview

This application is going to demonstrate transferring marbles between two users leveraging IBM Blockchain. We are going to do this in Node.js and a bit of GoLang. The backend of this application will be the GoLang code running in our blockchain network. The chaincode itself will create a marble by storing it to the chaincode state. The chaincode is able to store data as a string in a key/value pair setup. Thus, we will stringify JSON objects to store more complex structures.

Level 2.2: Install the Marbles Application

  1. Install the Marbles application by executing the following script.
    installmarbles              
  2. Start the Marbles application by executing the following script.
    startmarbles              
  3. Examine the log. When you see the message” Websocket Up”, the application has started successfully.
    tail -f /tmp/startmarbles.log 

    Image:

    Exit the log file.

    Ctrl+c
  4. Issue the following command to see the chaincode Docker containers.
    dockerps              

    Image:

    Four new Docker containers are created for the Marbles application.

    Question: The container name consists of 3 components. What are they?

Level 2.3: Run the Marbles Application

Level 2.3.1: Marbles Application Web GUI

We are going to use a Web GUI that can set values and pass them to the chaincode. Interacting with the chaincode is done with a HTTP REST call to a peer on the network

  1. Launch a browser and enter http://<your virtual server IP address>:3000

    Image:

  2. Click the VIEW TX BLOCKS tab.

    You should see block 001 (block height = 1).

    Image:

  3. Create one marble for Bob.
    Click on CREATE 
    Select a Color 
    Select a Size 
    Select Bob 
    Click on the CREATE button

    Image:

    Question: Each block corresponds to one transaction. What is the block height now?

  4. Look at the newly created transaction block.

    Image:

    Question: What transaction was invoked to create a marble?

    Question: What parameters were passed to this function?

  5. Create one more marble for Bob.
  6. Create two marbles for Leroy.

    Image:

    Question: How many blocks are there now?

  7. Return to the terminal, check the logs for peer vp0 and peer vp1. 
    docker logs dev-vp0-<Insert chaincode ID here> &>/tmp/devvp0.log 
    docker logs dev-vp1-<Insert chaincode ID here> &>/tmp/devvp1.log

    Search both logs for the text ‘marble index’.

    Question: What does the 'marble index' function return?

    Question: Are the results the same for both peers?

  8. Now, return to the browser GUI. Transfer (drag and drop) one marble from Bob to Leroy.

    Image:

    Look at the newly created transaction block.

    Question: What function was invoked for the transfer?

    Question: What parameters were passed to this function?

  9. Next, move a marble from Leroy to the trash. (Note: You may have to close the TX BLOCKS tab to see the trash can.)

    Look at the newly created transaction block.

    Image:

    Question: What transaction was invoked to remove a marble?

    Question: What parameters were passed to this function?

  10. Optional: You can use this GUI to play a little with marble creation and transfers.

Level 2.3.2: Command Line Interface

  1. Return to the terminal, issue the following command to log in to peer vp0. 
    hlogin test_user9 H80SiB5ODKKQ 

    Image:

  2. Set the CID environment variable to the chaincode ID, which can be found in the Docker container name (use dockerps command) or in the Marble application block description (VIEW TX BLOCKS tab).
    export CID=<Insert chaincode ID here>                
  3. Query the list of marbles in the chain.
    hquery $CID _marbleindex  

    Image:

    Question: How many marbles are returned?

    Question: Does it match your current state in the browser?

  4. Select a marble and retrieve its properties.
    hquery $CID <Insert marble index here>                 

    Image:

  5. From the browser, move all the marbles to Bob.
  6. From the terminal, move one of Bob’s marbles to Leroy:
    hinvoke $CID set_user <Insert marble index here> leroy                 
  7. Retrieve the properties for the transferred marble.
    hquery $CID <Insert marble index here>                

    Question: Is Bob still the owner?

  8. Refresh the browser to check the update.

Get points: If you are participating in the hackZone challenge and would like to earn points, please return to the Advocate Hub and answer questions based on this level.

Level 2.4: Clean Up

  1. From the terminal, stop the fabric by executing the following script.
    stopfabric                
  2. Delete all the Docker containers by executing the following script.
    hcleandocker                

Level 3: Hyperledger Explorer<

Level 3.1: Overview

In this section, you will learn how to install the Hyperledger Explorer tool that is available on the Hyperledger github web site.  This tool will help you monitor the activity on your blockchain.

Level 3.2: Install Hyperledger Explorer

  1. Start an SSH terminal (e.g. putty on windows, or terminal in Linux or Mac) and connect to the IP address of your assigned server.
  2. Login to user “blockchain” using the supplied password.
  3. Switch to the “root” user sudo –i
    sudo –i                
  4. Start the Hyperledger fabric code by executing the following script.
    startfabric                    

    This will start four peers (Docker containers) and other required services. You can monitor the progress by checking the /tmp/startfabric.log file.

  5. Verify that the fours peers started successfully by executing the following command. 
    dockerps 

    Image:

  6. Return to the blockchain user by exiting the current root shell.
    exit                    
  7. Install the Hyperledger Explorer by executing the following script.
    installexplorer                    

    The installation is successful when you see these messages.

    Image:

Level 3.3: Start Hyperledger Explorer

  1. Start the Hyperledger Explorer by executing the following script.
    startexplorer                
  2. Hyperledger explorer listens on port 9090. Launch a browser and enter
    http://<your virtual server IP address>:9090

    Image:

    Question: How many peers do you see in your blockchain?

    Question: How many blocks do you have in your chain?

Level 3.4: Example smart contract

Example02 is an example smart contract provided by the Hyperledger fabric distribution.

  1. Set the following environment variables required by the smart contract.
    export CORE_PEER_ADDRESS=$(getip):7051
    export CORE_SECURITY_ENABLED=true     
  2. Log into the blockchain fabric as peer vp0
    sudo docker exec -it dockercompose_vp0_1 bash 
    peer network login test_user9 -p H80SiB5ODKKQ 
  3. Deploy the smart contract and initialize two accounts, ACCOUNT_A and ACCOUNT_B (Enter the following command as one line).
    peer chaincode deploy -l golang -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 
                  -c '{"Function":"init", "Args": ["ACCOUNT_A", "2000", "ACCOUNT_B", "1000"]}' -u test_user9   

    The command returns immediately. Please allow approximately two minutes for this transaction block to be created.

  4. Return to your browser and the Hyperledger Explorer.

    Display the contents of this newly created block by clicking the ‘View’ button for Block# 1.  Note: you may need to reload the page.

    Image:

    Question: Note (copy) the chaincode UUID. You will need it in the following step.

  5. Return to the terminal. Let’s execute a few transactions by using the chaincode function APIs.

    Set an environment variable for the chaincode UUID.

    export CID=<chaincode UUID>          

    Use the query function to find the balance of ACCOUNT_A.

    peer chaincode query -n $CID -c '{"Function": "query", "Args":["ACCOUNT_A"]}' -u test_user9  

    Question: What is the balance for ACCOUNT_A?

    Use the query function to find the balance of ACCOUNT_B.

    peer chaincode query -n $CID -c '{"Function": "query", "Args":["ACCOUNT_B"]}' -u test_user9  

    Question: What is the balance for ACCOUNT_B?

  6. Use the invoke function to transfer 10 units from ACCOUNT_A to ACCOUNT_B.
    Hint: Search the internet for Hyperledger fabric APIs.

    Question: Use the query function to find the balance for ACCOUNT_A.

    Question: Use the query function to find the balance for ACCOUNT_B.

    Question: From the Explorer, how many blocks are shown?

  7. Return to the blockchain user by exiting the current bash shell.
    exit                

Get points: If you are participating in the hackZone challenge and would like to earn points, please return to the Advocate Hub and answer questions based on this level.

Level 3.5: Clean Up

  1. From the terminal, switch to the “root” user
    sudo -i 
  2. stop the fabric by executing the following script.
    stopfabric                
  3. Delete all the Docker containers by executing the following script.
    hcleandocker