Deployment and call of Hyperledger-fabric_ smart contract

Start the network and create a channel

Related knowledge points

  • Test network related folders are in test-network
  • Docker is an open source application container engine, which allows developers, operators and enterprises to package, distribute, deploy and run applications more conveniently. Using Docker, developers can use images to quickly build a standard development environment; after the development is complete, test and operation and maintenance personnel can use the exact same environment to deploy code, as long as the code is developed and tested, it can be seamlessly deployed in the production environment run.
  • Fabric is an open source blockchain platform that supports smart contracts and multiple consensus mechanisms. network.sh is a script file located in the test-network directory in the fabric-samples codebase. network.sh can be used to deploy and manage a simple test network, including two organizations, four nodes and one channel.
  • Remember to switch to administrator privileges – su root

Organization: An organization is a participant in the Fabric network, which represents a legal entity or a business unit23. Different organizations can jointly build a consortium chain, or join or withdraw from an existing consortium chain

Node: A node is a server or host in the Fabric network, which runs specific software services

Every node and user that interacts with the Fabric network needs to belong to an organization in order to participate in the network. The test network includes two peer organizations, Org1 and Org2. It also includes the individual orderer organizations that maintain the network ordering service.

Peers are a fundamental component of any Fabric network. Nodes store the blockchain ledger and verify transactions before they are committed to the ledger. Nodes run smart contracts that contain business logic for managing assets on the blockchain ledger.

Every peer in the network needs to belong to an organization. In the test network, each organization runs a peer node, peer0.org1.example.com and peer0.org2.example.com.

We can use a script to create a Fabric channel for transactions between Org1 and Org2. A channel is a private communication layer between members of a particular network. Channels are only available to organizations invited to the channel and are not visible to other members of the network. Each channel has a separate blockchain ledger.

cd fabric-samples/test-network

From inside the test-network directory, run the following command to remove any previously run containers or artifacts:

./network.sh down

Start the testnet, this command creates a Fabric network consisting of two peers and an orderer

 ./network.sh up
 
The network.sh script creates a channel between Org1 and Org2 and joins their peers to the channel

./network.sh createChannel

Endorsement policy: The endorsement policy in the blockchain is the rule used to instruct the blockchain nodes how to verify transactions. The endorsement policy is implemented in the chaincode (Chaincode), and all transactions must be carried out according to the endorsement policy, because only the transactions processed by the endorsement are legal and recognized transactions.

The endorsement in the blockchain refers to the process and mechanism in which the node undertaking the endorsement task verifies the transaction information for the blockchain transaction, and declares that the transaction is legal for the verified transaction. Endorsement nodes are nodes that undertake endorsement tasks in the blockchain. They need to run smart contracts related to transactions and sign the results. The endorsement policy is a rule for instructing blockchain node transaction verification, which defines the minimum set of organizations required to endorse a transaction to make it effective. The endorsement signature is the signature of the endorsement node on the verified transaction result, which proves the node’s approval of the transaction.

In Fabric, smart contracts are deployed on the network in the form of packages called chaincodes. Chaincode is installed on an organization’s peers and then deployed into channels, where it can then be used to endorse transactions and interact with the blockchain ledger. Before chaincode can be deployed to a channel, the members of the channel need to agree on a chaincode definition that establishes chaincode governance. When the required number of organizations agree, the chaincode definition can be committed to the channel and the chaincode is ready for use.

Package smart contract

  • go.mod is a dependency configuration file for go
  • Pay attention to switch to the root role
  • If the command is successful, the go package will be installed in a vendor folder.

Install go dependencies

cd fabric-samples/asset-transfer-basic/chaincode-go

GO111MODULE=on go mod vendor # install dependencies

cd ../../test-network

PeerCLI environment configuration

The peer binary is located in the bin folder of the fabric-samples repository. Add these binaries to your CLI path with the following commands:

export PATH=${PWD}/../bin:$PATH

Set FABRIC_CFG_PATH to point to the file fabric-samples in the repository core.yaml

export FABRIC_CFG_PATH=$PWD/../config/

peer version

Create chaincode package

Create a package named basic.tar.gz. The –lang flag is used to specify the chaincode language and the –path flag provides the location of your smart contract code

peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_1.0

Install the chaincode package

After we package the asset transfer (basic) smart contract, we can install the chaincode on our nodes. Chaincode needs to be installed on every node that will approve transactions.

Install on Org1

Set the following environment variables to operate the CLI with peer as the Org1 admin user. Set CORE_PEER_ADDRESS to point to the Org1 peer

Environment configuration
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

Install
peer lifecycle chaincode install basic.tar.gz

`

If the command is successful, the peer will generate and return a packet identifier

Install on Org2

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_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

peer lifecycle chaincode install basic.tar.gz

Approve chaincode definitions

Approve Org2

After installing the chaincode package, the chaincode definition needs to be approved for the organization. This definition includes important parameters for chaincode governance, such as name, version, and chaincode endorsement policy.

Query chaincode id and set variables

Find the bundle ID of the chaincode, every peer will generate the same bundle ID.

The bundle ID is used when approving the chaincode, so go ahead and save it as an environment variable. Paste the returned bundle ID into the command below. NOTE: The Bundle ID will be different for all users, so this step needs to be done using the Bundle ID returned from the command window in the previous step

peer lifecycle chaincode query installed

export CC_PACKAGE_ID=basic_1.0:69de748301770f6ef64b42aa6bb6cb291df20aa39542c3ef94008615704007f3
Approve chaincode
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --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"

Approve Org1

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --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"

Submit the chaincode definition to the channel

After a sufficient number of organizations approve the chaincode definition, one organization can commit the chaincode definition to the channel. If a majority of channel members approve the definition, the commit transaction will succeed and the parameters agreed in the chaincode definition will be implemented on the channel.

Check if channel members have approved the same chaincode definition
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example. com/msp/tlscacerts/tlsca.example.com-cert.pem" --output json

Submit to channel

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile "${PWD}/organizations /ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/ org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers /peer0.org2.example.com/tls/ca.crt"`

Use the peer lifecycle chaincode querycommitted command to confirm that the chaincode definition has been committed to the channel
peer lifecycle chaincode querycommitted --channelID mychannel --name basic --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert. pem"

Call the chaincode

After a chaincode definition is committed to a channel, the chaincode will be started on peers joining the channel on which the chaincode was installed. The asset transfer (basic) chaincode is now ready to be invoked by the client application. Create an initial set of assets on the ledger with the following command.

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/ tlscacerts/tlsca.example.com-cert.pem " -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1 .example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ ca.crt" -c '{"function":"InitLedger","Args":[]}'

Use the query function to read the collection of cars created by the chaincode

peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'