Book Image

Hyperledger Cookbook

By : Xun (Brian) Wu, Chuanfeng Zhang, Zhibin (Andrew) Zhang
Book Image

Hyperledger Cookbook

By: Xun (Brian) Wu, Chuanfeng Zhang, Zhibin (Andrew) Zhang

Overview of this book

Hyperledger is an open-source project and creates private blockchain applications for a range of domains. This book will be your desk reference as you explore common and not-so-common challenges faced while building blockchain networks using Hyperledger services. We'll work through all Hyperledger platform modules to understand their services and features and build end-to-end blockchain applications using various frameworks and tools supported by Hyperledger. This book's independent, recipe-based approach (packed with real-world examples) will familiarize you with the blockchain development cycle. From modeling a business network to integrating with various tools, you will cover it all. We'll cover common and not-so-common challenges faced in the blockchain life cycle. Later, we'll delve into how we can interact with the Hyperledger Fabric blockchain, covering all the principles you need to master, such as chaincode, smart contracts, and much more. We'll also address the scalability and security issues currently faced in blockchain development. By the end of this book, you will be able to implement each recipe to plan, design, and create a full-fledged, private, decentralized application to meet organizational needs.
Table of Contents (12 chapters)

Writing your first application

In this recipe, we will explore how to create a smart contract and then deploy it into the blockchain.

To run this recipe, you need to have completed the Installing Hyperledger Fabric on AWS recipe in this chapter to install Hyperledger Fabric with samples and binaries on the AWS EC2 instance.

How to do it...

To write your first application, follow these steps:

  1. Set up the development environment:
        $ cd ~
cd fabric-samples/first-network
sudo docker ps
sudo ./byfn.sh down
sudo docker rm -f $(sudo docker ps -aq)
sudo docker network prune
cd ../fabcar && ls

You will notice that there are a few Node.js file present in fabcar folder such as enrollAdmin.js, invoke.js, query.js, registerUser.js, and package.json and all others packaged into one startFabric.sh file.

  1. Install the Fabric client:
        $ sudo npm install -g [email protected]
$ sudo npm update

You will notice from the following screenshot that the Fabric client 1.3.0 and Fabric CA client 1.30 packages are installed:

  1. Execute the following command to launch the network:
        $ sudo ./startFabric.sh node
  1. Open a new Terminal to stream the Docker logs:
        $ sudo docker logs -f ca.example.com

This will open the Docker file, which will look similar to the following screenshot:

Next, we will use the Node.js script to run, query, and update the records on Fabric network.

Accessing the API with SDK

In this recipe, the application uses an SDK to access the APIs that permit queries and updates to the ledger. Now we will perform the following steps:

  1. Enroll an admin user with the enrollAdmin.js script:
        $ sudo node enrollAdmin.js

When we launch the network, an admin user needs to be registered with certificate authority. We send an enrollment call to the CA server and retrieve the enrollment certificate (eCert) for this user. We then use this admin user to subsequently register and enroll other users:

  1. Register and enroll a user called user1 using the registerUser.js script:
        $ sudo node registerUser.js
  1. With the newly-generated eCert for the admin user, let's communicate with the CA server once more to register and enroll user1. We can use the ID of user1 to query and update the ledger:
  1. Let's run a query against the ledger:
        $ sudo node query.js
  1. It returns the following screenshot. You will find that there are 10 cars on the network, from CAR0 to CAR9. Each has a color, doctype, make, model, and owner:

  1. The following chaincode constructs the query using the queryAllCars function to query all cars:
         // queryCar chaincode function - requires 1 argument,
ex: args: ['CAR4'],
// queryAllCars chaincode function - requires no arguments,
ex: args: [''],
const request = {
//targets : --- letting this default to the
peers assigned to the channel
chaincodeId: 'fabcar',
fcn: 'queryAllCars',
args: ['']
}
  1. Update the ledger. To do this, we will update the invoke.js script. This time, the fabcar chaincode uses the createCar function to insert a new car, CAR10, into the ledger:
        var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'createCar',
args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
chainId: 'mychannel',
txId: tx_id
};

sudo node invoke.js

Here we will complete the transaction when CAR10 is created.

  1. Execute a query to verify the changes made. Change query.js using the queryCar function to query CAR10:
        var request = {
//targets: let default to the peer assigned to the client
chaincodeId: 'fabcar',
fcn: 'queryCar',
args: ['CAR10'],
chainId: 'mychannel',
txId: tx_id
};

  1. Run query.js again. We can now extract CAR10 from the ledger with the response as {"color":"Red","docType":"car","make":"Chevy","model":"Volt","owner":"Nick"}:
      sudo node query.js

This will result in the following query:

  1. Shut down the Fabric network:
      sudo docker stop $(sudo docker ps -a -q)
sudo docker rm $(sudo docker ps -a -q)
sudo docker ps

We have gone through the steps to query and update the transaction using smart contract chaincode. Now, let's see how it works under the hood.

How it works...

This concludes the recipe to create and deploy your first smart contract chaincode.

In the previous steps, we used query.js to query the key-value pair store. We can also query for the values of one or more keys, or perform complex searches on JSON data-storage formats. The following diagram shows how the query works:

The following is a representation of different functions in chaincode, which explains that we should first define the code functions to all the available APIs in the chaincode interface:

The following diagram shows the process of updating the ledger. Once an update to the ledger is proposed and endorsed, it will be returned to the application, and will in turn send the updated ledger to be ordered and written to every peer's ledger:

We learned how to write a small smart contract chaincode on the Fabric network to perform a transaction data query and update. In the next chapter, you will learn how to write an end-to-end Hyperledger Fabric application using all that we have learned in this chapter.

See also