In the Hyperledger Fabric framework, chaincodes implement smart contracts. Chaincodes can be written in Node.js, Go, or Java. More languages will be supported in the future. This topic describes how to deploy a chaincode in a channel.
This chapter is applicable to users who purchase an instance of Fabric V2.2 or V2.5. If you purchase an instance of Fabric V1.4, please refer to the Manage chaincode (V1.4) deploy chaincodes.
Package the chaincode
Before you deploy a chaincode, you must develop, test, and package the chaincode in the development environment of Hyperledger Fabric.
Develop a chaincode.
After the chaincode is tested, run the
peer lifecycle chaincode package
command to package the chaincode. We recommend that you include required dependencies in the package. The following example describes how to place required dependencies in the vendor directory of a Go chaincode:Use govendor to load dependencies.
Run the following command to install govendor:
go get -u -v github.com/kardianos/govendor
Go to the project directory of the chaincode and run the following command to initialize the vendor directory.
govendor init
Run the following command to automatically move dependencies used in this project from GOPATH to the vendor directory:
govendor add +external
Run the go mod commands to load dependencies.
Run the following command to initialize go mod:
go mod init
Run the following command to automatically package dependencies into the vendor folder:
go mod vendor
For more information about the packaging command, see peer lifecycle chaincode commands. Notes:
For a Go chaincode, specify the relative path of the main package directory in $GOPATH/src in the -p option. For a Java chaincode, specify the project directory of the chaincode in the -p option. For a Node.js chaincode, specify the root directory of the chaincode project that contains the package.json file in the -p option. Do not specify a relative path in the ./xxx format in the -p option.
For a chaincode written in Go, you can ignore the -l option. For Node.js, specify -l node. For Java, specify -l java.
Use
--label
to specify the default chaincode name and version.
In the following example, the Go chaincode in the $GOPATH/src/github.com/hyperledger/fabric-samples/chaincode/sacc directory is compressed to the sacc.tar.gz package with a chaincode label of sacc_1_0.
peer lifecycle chaincode package -p github.com/hyperledger/fabric-samples/chaincode/sacc --label sacc_1_0 -l golang sacc.tar.gz
peer
packages:
To deploy a chaincode, perform the following three steps. For more information about how to upgrade an existing chaincode in a channel, see Upgrade chaincodes.
Upload the chaincode
Install the chaincode
Submit the chaincode definition
Upload the chaincode
Log on to the Alibaba Cloud Blockchain as a Service (BaaS) console.
In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.
On the page that appears, click the Chaincode Package management tab.
On the Chaincode Package management tab, click Upload Chaincode.
In the panel that appears, upload the chaincode that you have packaged in the development environment. For more information about how to package a chaincode, see the Package the chaincode section.
Click OK.
After the chaincode is uploaded, the chaincode appears in the chaincode list on the Chaincode Package management tab. The chaincode name displayed in the Label column is the name that you specify when you package the chaincode.
Install the chaincode
Prerequisites
The chaincode is uploaded.
Procedure
Log on to the Alibaba Cloud BaaS console.
In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.
On the page that appears, click the Chaincode Package management tab.
On the Chaincode Package management tab, find the chaincode that you want to install and click Installation in the Actions column to install the chaincode in the current organization.
During the installation, the chaincode is automatically precompiled to check for possible issues in the chaincode package.
The precompilation may take a few seconds or minutes, based on the complexity of the chaincode dependencies. After the installation is complete, the chaincode status in the Status column changes from Uninstalled to Installed, and the button in the Actions column changes from Installation to Submit definition.
Submit the chaincode definition
Prerequisites
The chaincode is uploaded and installed.
Procedure
Log on to the Alibaba Cloud BaaS console.
In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.
On the page that appears, click the Chaincode Package management tab.
On the Chaincode Package management tab, find the chaincode whose definition you want to submit and click Submit definition in the Actions column.
In the panel that appears, set parameters based on the instructions.
Parameter | Description |
Name | The name that you specify for the chaincode when you package the chaincode. You can keep the name or give the chaincode a new name. Note: You cannot submit the definition for a chaincode whose name is already used by another chaincode. |
Version | The version that you define when you submit the chaincode definition. |
Deploy Channel | The channel in which you deploy the chaincode. After the chaincode is deployed, the chaincode is visible to all the organizations in this channel. For more information about how to create a channel, see Create channels. |
Whether to initialize | Specifies whether to initialize the chaincode. Valid values:
|
Endorsement Policy | The policy that specifies the set of peers on a channel that must run the chaincode and endorse the execution results for the transaction to be considered valid. Example:
Note The |
Superior | After you click Superior, you can enter the JSON data of the private data collection of your chaincode in the Privacy Set Configuration field. You can skip this step if your chaincode does not use private data. Example of the JSON data of the private data collection: |
After you set the name or blockToLive parameter of a private data collection, you cannot change it anymore in later upgrades. For more information, see Using Private Data in Fabric.
Click Submit.
Then, you can view the status of the chaincode on the Chaincodes tab. After you submit the chaincode definition, the status in the Status in Channel column changes to In consensus, and the status in the Status in Organization column changes to Processing complete. You can click Approval details in the Actions column to view the basic information of the chaincode.
Wait for other organizations in the channel to confirm and submit the chaincode definition.
NoteBy default, the chaincode definition can be submitted only after it is confirmed by 50% of the organizations in the channel.
Log on to the Alibaba Cloud BaaS console as the member of another organization.
In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.
On the page that appears, click the Chaincodes tab. On the Chaincodes tab, find the chaincode that you want to manage and click Handle in the Actions column.
In the Chaincode processing panel, check whether the definition submitted meets the requirements, and click Submit if you want to confirm the definition.
After 50% of the organizations in the channel confirm the definition, the chaincode is deployed. On the Chaincodes tab, the status in the Status in Channel column changes from In consensus to Running. Upgrade and Display Log appear in the Actions column.
NoteIf the chaincode definition that you submit does not meet the requirements, other organizations can choose not to process the chaincode definition. You need to upload the chaincode package, install the chaincode, and submit the chaincode definition again. In this case, the parameters to submit are different from the original ones, and more than one consensus records appear on the Chaincodes tab. If an organization processes one consensus record, other consensus records of the chaincode will disappear.