Deploying the DocsApp smart contract

We need to set up a new chaincode directory for our contract. In your Hyperledger Fabric fabric-samples directory, navigate to the chaincode directory. It will be at the following location by default:

/fabric-samples/chaincode/

We will use the following steps to deploy our DocsApp smart contract:

  1. In the chaincode folder, create a new folder with the name docsapp.
  2. In the docsapp folder, create an index.js file with the following values:
/*
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const docsapp = require('./lib/docsapp');

module.exports.docsapp = docsapp;
module.exports.contracts = [ docsapp ];

This will declare the docsapp object, which will be used by our peer chaincode install and peer chaincode instantiate tools for deploying our chaincode within the blockchain.

  1. Next, create a lib folder in the docsapp directory. By default, your lib folders filepath should look as follows:
/fabric-samples/chaincode/docsapp/lib
  1. Copy and paste the docsapp.js file along with the DocsApp smart contract code that we wrote in the previous section. 
  2. Now, we need to install this smart contract for all our peers, peer0.org1.example.com, peer1.org1.example.com, peer0.org2.example.com, and peer1.org2.example.com, which comprise our Hyperledger Fabric network. To do this, we'll be using the peer chaincode install command.

The peer chaincode utility is a tool that is available in the command-line interface (CLI) Docker container, which is available by default as a Docker image in the Hyperledger Fabric repository. It allows us to interact with and carry out operations on chaincodes (a chaincode is the Hyperledger Fabric equivalent of a smart contract). 

Let's look at the peer chaincode install statement that we will be using to deploy our chaincode for org1 on peer1:

docker exec 
-e CORE_PEER_LOCALMSPID=Org1MSP
-e CORE_PEER_ADDRESS=
peer0.org1.example.com:7051
-e CORE_PEER_MSPCONFIGPATH=/opt/
gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
-e CORE_PEER_TLS_ROOTCERT_FILE=/
opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
cli peer chaincode install -n docsapp -v 1.0
-p /opt/gopath/src/
github.com/chaincode/docsapp -l node

Let's go through the different parts of the command one by one. The docker exec command is used to execute a command within the Docker container. The  -e is used to set environment variables inside the Docker container before executing a command.

The CORE_PEER_LOCALMSPID environment variable indicates the membership service provider for the node. The membership service provider is used to define the Root Certificate Authorities and Intermediate Certificate Authorities that will be used to issue identities for a trusted domain/organization on the blockchain network. Here, we set it to Org1MSP, which is the MSP service for Org1.

The CORE_PEER_ADDRESS environment variable indicates the external client port for peer 1 (node 1 in our Hyperledger Fabric network) to which requests need to be submitted. We set it here to peer0.org1.example.com:7051, which is our node's client port.

The CORE_PEER_MSPCONFIGPATH environment variable indicates the configuration path for the membership policy for peer 1 within peer 1's Docker container.

The CORE_PEER_TLS_ROOTCERT_FILE environment variable indicates the location of the root certificate (the certificate authority certificate) for the digital signature used by peer 1.

The cli phrase indicates the container name in which we are executing peer chaincode commands. This, as we discussed earlier, is the command-line interface Docker container.

Lastly, we use the -n tag to indicate the chaincode name, -v, to indicate the version of the chaincode, -p, to indicate the path where the smart contract code is available, and -l to indicate the scripting language of the smart contract, which is node (Node.js) in our case. 

  1. Run the command with a Linux user with Docker privileges in the Terminal. It should print a message similar to the following one on successful execution. This message is from the command-line container:
2019-10-29 05:56:55.357 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
  1. Now, craft a peer chaincode install statement for all the other nodes in our network:
//chaincode install statement for peer1 org1

docker exec -e CORE_PEER_LOCALMSPID=Org1MSP -e CORE_PEER_ADDRESS=
peer1.org1.example.com:8051 -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt cli peer chaincode install -n docsapp -v 1.0 -p /opt/gopath/src/github.com/chaincode/docsapp -l node


//chaincode install statement for peer0 org2

docker exec -e CORE_PEER_LOCALMSPID=Org2MSP -e CORE_PEER_ADDRESS=peer0.org2.example.com:9051 -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt cli peer chaincode install -n docsapp -v 1.0 -p /opt/gopath/src/github.com/chaincode/docsapp -l node

//chaincode install statement for peer1 org2

docker exec -e CORE_PEER_LOCALMSPID=Org2MSP -e CORE_PEER_ADDRESS=peer1.org2.example.com:10051 -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt cli peer chaincode install -n docsapp -v 1.0 -p /opt/gopath/src/github.com/chaincode/docsapp -l node
  1. Run them in the Terminal. You should get a successful peer installation message after each peer install command, as follows:
INFO 003 Installed remotely response:<status:200 payload:"OK" >
  1. Next, we need to instantiate the chaincode across the nodes. We'll use the following command for instantiating the chaincode:
docker exec 
-e CORE_PEER_LOCALMSPID=Org1MSP
-e CORE_PEER_MSPCONFIGPATH=/opt/
gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp cli peer chaincode instantiate
-o
orderer.example.com:7050
-C mychannel
-n docsapp
-l node
-v 1.0
-c '{"Args":[]}'
-P 'AND('''Org1MSP.member''','
''Org2MSP.member''')'
--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 --peerAddresses peer0.org1.example.com:7051
--tlsRootCertFiles /opt/gopath/src/
github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

Let's look at the environment variables, which are denoted by the -e tag.

The CORE_PEER_LOCALMSPID environment variable indicates the membership service provider for the node. The membership service provider is used to define the Root Certificate Authorities and Intermediate Certificate Authorities, which will be used to issue identities for a trusted domain/organization on the blockchain network. Here, we set it to Org1MSP, which is the MSP service for Org1.

The CORE_PEER_MSPCONFIGPATH environment variable indicates the configuration file path for the Membership Service Provider for peer 1 within peer 1's Docker container.

  • The -C tag indicates the Hyperledger Fabric network channel to which this chaincode will be instantiated. In Hyperledger Fabric, channels are private blockchain ledgers that can be read or written to only by the approved participants of the channel. We set it to the default mychannel channel.
  • The -n tag indicates the name of the chaincode being instantiated. Here, we set it to docsapp.
  • The -l tag indicates the scripting language, which in our case is a node.
  • The -v tag is the version number of the chaincode.
  • The -c tag indicates the constructor arguments for the contract, if there are any.
  • The -P tag indicates the member participants of the network.
  • The --tls tag indicates that messages will be SSL encrypted. The tags --cafile and --tlsRootCerFiles tags indicate the certificate file for orderer and peer0.org1.

Run the command in the Terminal window. If instantiation is successful, you will not get any message on the Terminal window.

That completes writing and deploying of our docsapp chaincode (smart contract). Let's move on to writing the backend node server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.90.131