Search icon CANCEL
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hyperledger Cookbook

You're reading from  Hyperledger Cookbook

Product type Book
Published in Apr 2019
Publisher Packt
ISBN-13 9781789534887
Pages 310 pages
Edition 1st Edition
Languages
Concepts
Authors (3):
Xun (Brian) Wu Xun (Brian) Wu
Profile icon Xun (Brian) Wu
Chuanfeng Zhang Chuanfeng Zhang
Profile icon Chuanfeng Zhang
Zhibin (Andrew) Zhang Zhibin (Andrew) Zhang
Profile icon Zhibin (Andrew) Zhang
View More author details

Table of Contents (12) Chapters

Preface 1. Working with Hyperledger Fabric 2. Implementing Hyperledger Fabric 3. Modeling a Business Network Using Hyperledger Composer 4. Integrating Hyperledger Fabric with Explorer 5. Working with Hyperledger Sawtooth 6. Operating an Ethereum Smart Contract with Hyperledger Burrow 7. Working with Hyperledger Iroha 8. Exploring the CLI with Hyperledger Indy 9. Hyperledger Blockchain Scalability and Security 10. Hyperledger Blockchain Ecosystem 11. Other Books You May Enjoy

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 npm@5.3.0
$ 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

  • Fabric Explored: A Technical Deep-Dive on Hyperledger Fabric, IBM blockchain, IBM September 17, 2018 (https://www.slideshare.net/MattLucas3/blockchain-hyperledger-fabric-explored-v45)
  • The hyperledger-fabricdocs Documentation, release master, January 27, 2019: https://media.readthedocs.org/pdf/hyperledger-fabric/latest/hyperledger-fabric.pdf
You have been reading a chapter from
Hyperledger Cookbook
Published in: Apr 2019 Publisher: Packt ISBN-13: 9781789534887
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}