Hyperledger Fabric Trading Flow
Hyperledger Fabric v1.1 provides several special chain codes to perform certain special tasks, which are known as system chain codes (Systems Chaincode). The purpose of this paper is to summarize the implementation, function and usage of these chain codes. Similar to the user chain code, the system chain code also implements the Init () and Invoke () methods. There are five types of contracts in fabric:
- Configuration System Chaincode (CSCC)--Core/scc/cscc/configure.go
- Life Cycle System Chaincode (LSCC)--Core/scc/lscc/lscc.go
- Query System Chaincode (QSCC)--Core/scc/qscc/query.go
- Endorser System Chaincode (ESCC)--Core/scc/escc/endorser_onevalidsignature.go
- Validator System Chaincode (VSCC)--Core/scc/vscc/validator_onevalidsignature.go
Below, the functionality and use of each system chain code is elaborated. It is important to note that because we may need to transmit the serialized protobuf bytes of the Golang struct, a simple invoke/query with the command line (CLI) may not be able to use the full functionality provided by the system chain code. Therefore, it is recommended to use the SDK to perform these functions. In this article, the invoke/query operation is performed only through the CLI to demonstrate some of the functionality of the system chain code.
1. Configuration System Chaincode (CSCC)
CSCC manages channel-related information on peer and performs channel configuration transactions. It provides five methods: (i) Joinchain, (ii) Getconfigblock, (iii) Getconfigtree, (iv) Simulateconfigtreeupdate, (v) getchannels.
The use of these features is described below. All commands are executed in the client that points to Peer0 in the sample network (reference setup). In order to run CSCC related commands, we need to use Peer channel and peer Chaincodein CLI commands.
The Joinchain method is used to add a peer to a channel. It requires a parameter, the serialized protobuf bytes of the channel configuration block, where the channel configuration chunk is retrieved as the peer channel Create command returned from Orderer. The following CLI command enables the peer to join a channel named Ch1 . When calling CSCC, thepeer channel join command is responsible for reading the ch1.block and passing it in bytes form. However, if we use Peer Chaincode invoke directly to invoke the Joinchain method, it is more difficult to put the contents of Ch1.block into the CLI request.
$ peer channel join -b ch1.block
The Getconfigblock method is used to get the current configuration chunk for a given channel. It requires a parameter, which is the byte form of the channel name. The following two CLI commands can be used to get the configuration chunk of the channel MyChannel .
$ peer chaincode query -C "mychannel" -n cscc -c '{"Args":["GetConfigBlock", "mychannel"]}'
Or
peer channel fetch -o orderer0:7050 config -c mychannel
The getchannels method is used to obtain the channel currently joined by the peer. The following two CLI commands can be used to get all the channels.
$ peer chaincode query -C "" -n cscc -c '{"Args":["GetChannels"]}'
Or
$ peer channel list
getconfigtree and simulateconfigtreeupdate are used to obtain the config structure and simulate the configuration of config updates. If you want to add or remove an organization from a channel, you must obtain the Config tree for modification, and you must obtain an endorsement of CSCC when calling the Simulateconfigtree method.
2. Life Cycle System Chaincode (LSCC)
LSCC is used to manage the lifecycle of a chain code--Install on peer, deploy and upgrade on a channel, and get information from a user's running chain code. It provides eight methods: (i) install, (ii) deploy, (iii) upgrade, (iv) GetID, (v) Getdepspec , (vi) getccdata, (vii) getchaincodes, (viii) getinstalledchaincodes.
The install method is used to store the Chaincode program to peer's file system (/var/hyperledger/production/chaincodes). It requires a parameter, the serialization protobuf bytes of Chaincode deployment Spec (CORE/COMMON/CCPROVIDER/CDSPACKAGE.GO). Although we can call LSCC directly by passing in the contents of the entire chaincode, it is a better practice to use the peer chaincode install command, which invokes LSCC by reading the contents of Chaincode.
$ peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
The deploy method is used to instantiate a contract on a given channel. It can accept five parameters, the first two parameters--the channel name and Chaincode deployment spec is required, the other is that parameter--the endorsement strategy, the name of the endorsement system contract and the name of the verification system contract is optional.
$ peer chaincode instantiate -o orderer0:7050 -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP', 'Org2MSP')"
The Getdepspec method is used to obtain the Chaincode deployment spec for the contract installed on the peer. The following CLI command obtains the deployment spec for the MYCC contract from the channel MyChannel.
$ peer chaincode query -C mychannel -n lscc -c '{"Args":["getdepspec", "mychannel", "mychannel"]}'
The getchaincodes method is used to obtain a list of contracts deployed on the channel. The following CLI command obtains the instantiated list of contracts from channel MyChannel.
$ peer chaincode query -C mychannel -n lscc -c '{"Args":["getchaincodes"]}'
The getinstalledchaincodes method is used to obtain a list of the contracts installed on the peer.
$ peer chaincode query -C "" -n lscc -c '{"Args":["getinstalledchaincodes"]}'
The upgrade method is used to upgrade contracts.
$ peer chaincode upgrade -o orderer0:7050 -C mychannel -n mycc -v 2.0 -c '{"Args":["reinit"]}' -P "OR ('Org1MSP', 'Org2MSP')"
GetID used to get the ID of the contract
$ peer chaincode query -C mychannel -n lscc -c '{"Args":["getid","mychannel","mycc"]}'
The getccdata is used to obtain the data for the contract.
$ peer chaincode query -C mychannel -n lscc -c '{"Args":["getccdata","mychannel","mycc"]}'
3. Query System Chaincode (QSCC)
QSCC exposes specific methods to the user, allowing users to query chunks and transactions stored in block storage. It provides five methods: (i) getchaininfo, (ii) Getblockbynumber, (iii) Getblockbyhash, (iv) Gettransactionbyid, (v) Getblockbytxid.
The Getblockbynumber method is used to get the serialized chunk. The following CLI command obtains a chunk with a sequence number of 3 from the channel MyChannel.
$ peer chaincode query -C mychannel -n qscc -c '{"Args":["GetBlockByNumber", "mychannel", "3"]}'
Other methods are similar.
4. Endorser System Chaincode (ESCC)
ESCC is called by the endorsement node (CORE/ENDORSER/ENDORSER.GO). The endorsement node, after executing the transaction, places its front in the transaction response message. The transaction response message also includes the results of the intersection execution, such as transaction status, contract events, and Read/write set. A call function can contain 5-7 parameters, namely header, Chaincodeproposalpayload, Chaincodeid, Response, simulation result, events, payload Visibility
5. Validator System Chaincode (VSCC)
VSCC is called by the Bookkeeping node (core/committer/txvalidator/validator.go) to validate the signature collection of each transaction according to the contract's endorsement strategy.
Translated from: System chaincodes in Hyperledger Fabric v1.1