Deploying Smart Contract in Hyperledger-V2.0

Rangesh Sripathi
3 min readJun 28, 2020

--

With Latest Release of Hyperledger(v2.1) Chaincode Deployment model has changed, Listed below are new changes with latest release.

Through out this article , I would be using Fabcar.go as example , you can grab it here — https://github.com/hyperledger/fabric-samples/tree/master/chaincode/fabcar/go

1) Packaging

Chaincode has to be packaged with go mod vendor , since shim interface are replaced by contract api . Once packaged you would see vendor folder with necessary interfaces / dependencies.

peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1

This will generate tar file with all the chaincode/smarcontract zipped .

2) Install

I have used two organization from test-network running in same box(Vagrant)

https://github.com/hyperledger/fabric-samples/tree/master/test-network

Each Org has one endorsing peer.

Org1 Install :

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

Chaincode package Identifier (CCID) will be generated upon successful installation.

Org2 Install :

Switching to Org2 from CLI with necessary env varibles.

export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051

Now that we have made sure we have installed chaincode in both org , next step is to approval process which is associated with endorsement policies.

3) Approval :

In this process you define the necessary endorsement policies and as well the sequence number. Incremental sequence number defines upgrade process.

In this example endorsement policy defaults that both org should endorse a transaction ,hence it has to be approved by both org.

Ensure you set necessary env variables when switching between orgs from CLI.

Org1 Approval:

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

This will generate taxid and ensures Org1 has approved the chaincode installation.

Org2 Approval

In similar way, Org2 has to commit / approve the chaincode.

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLS
HostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence
1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.exa

Either Org can always check the status by querying commit-readiness , i.e if other participant has approved the chaincode installation.

4) Commit

Either of Organization can commit on the approved chaincode, upon successful commit the chaincode would be eligible for Invoke/Query

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameO
verride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls --cafile ${PWD}/organization
s/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses loc
alhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.cr
t --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.

--

--