Deploying the corprem 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. By default, it will be at the following location:

/fabric-samples/chaincode/

Let's look at how to setup the contract project environment:

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

'use strict';

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

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

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

  1. Next, create a lib folder in the corprem directory. By default, your lib folder's filepath should be this:
/fabric-samples/chaincode/corprem/lib
  1. Copy and paste the corprem.js file with the corprem smart contract code that we wrote in the previous section. We need to install this smart contract to all four peers, peer0.banka.example.compeer1.banka.example.compeer0.bankb.example.com,and peer1.bankb.example.com of our Hyperledger Fabric network. To do so, we'll be using the peer chaincode install command.

The utility peer chaincode is a tool available in the cli (command-line interface) 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 the peer0.banka.example.com peer, as follows:

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

Let's go through the different parts of the command, one by one, as follows:

  • The docker exec command is used to execute a command within the Docker container.
  • The -e tag 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 which will be used to issue identities for a trusted domain/organization on the blockchain network. Here, we set it to bankaMSP, which is the MSP service for Bank A.
  • The CORE_PEER_ADDRESS environment variable indicates the external client port for peer0 (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 file path for the Membership Service Provider, for peer0 within peer0's Docker container.
  • The CORE_PEER_TLS_ROOTCERT_FILE environment variable indicates the location of the root certificate (CA certificate), for the digital signature used by peer0.
  • cli indicates the container name where we are executing the peer chaincode command. 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 in our case.
  1. Run the command as a Linux user with Docker privileges on the Terminal line. On successful execution, it should print a message similar to the one in the following code block. The 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, as follows:
//chaincode install statement for peer1 banka

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

//chaincode install statement for peer0 bankb

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

//chaincode install statement for peer1 bankb

docker exec -e CORE_PEER_LOCALMSPID=bankbMSP -e CORE_PEER_ADDRESS=peer1.bankb.example.com:10051 -e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/bankb.example.com/users/[email protected]/msp -e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/bankb.example.com/peers/peer0.bankb.example.com/tls/ca.crt cli peer chaincode install -n corprem -v 1.0 -p /opt/gopath/src/github.com/chaincode/corprem -l node
  1. Run them on the Terminal window. You should get a successful peer installation message, like the one in the following code snippet, after each peer install command:
INFO 003 Installed remotely response:<status:200 payload:"OK" >

  1. Next, we need to instantiate the chaincode across the nodes. To instantiate the chaincode, we'll use the following command: 
docker exec 
-e CORE_PEER_LOCALMSPID=bankaMSP
-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/banka.example.com/users/[email protected]/msp cli peer chaincode instantiate
-o orderer.example.com:7050
-C bkchannel
-n corprem
-l node
-v 1.0
-c '{"Args":[]}'
-P 'AND('''bankaMSP.member''','''bankbMSP.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.banka.example.com:7051
--tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/banka.example.com/peers/peer0.banka.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 bankaMSP, which is the MSP service for Bank A.

The CORE_PEER_MSPCONFIGPATH environment variable indicates the configuration file path for the Membership Service Provider for peer0 within peer0'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, which can be read or written to only by the approved participants of the channel. We set it to the bkchannel channel we created earlier.
  • The -n tag indicates the name of the chaincode being instantiated. Here, we set it to corprem
  • The -l tag indicates the scripting language, which in our case is node.
  • The -v tag is the version number of the chaincode.
  • The -c tag indicates the constructor arguments for the contract, if any.
  • The -P tag indicates the member participants of the network.
  • The --tls tag indicates that messages will be ssl-encrypted. The following tags, --cafile and --tlsRootCerFiles, indicate the certificate file for the orderer and peer0.banka.

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 our corprem chaincode (smart contract). Let's move on to creating the IPFS network.

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

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