All Products
Search
Document Center

Blockchain as a Service:Deploy chaincodes

Last Updated:Dec 18, 2023

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.

Note

This chapter is applicable to users who purchase an instances of Fabric V2.2. If you purchase an instances 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:

    1. Use govendor to load dependencies.

      1. Run the following command to install govendor:

        go get -u -v github.com/kardianos/govendor
      2. Go to the project directory of the chaincode and run the following command to initialize the vendor directory.

        govendor init
      3. Run the following command to automatically move dependencies used in this project from GOPATH to the vendor directory:

        govendor add +external
    2. Run the go mod commands to load dependencies.

      1. Run the following command to initialize go mod:

        go mod init
      2. 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:

    1. 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.

    2. For a chaincode written in Go, you can ignore the -l option. For Node.js, specify -l node. For Java, specify -l java.

    3. 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:

  • Mac: hyperledger-fabric-darwin-amd64-2.2.0.tar.gz

  • Linux: hyperledger-fabric-linux-amd64-2.2.0.tar.gz


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.

  1. Upload the chaincode

  2. Install the chaincode

  3. Submit the chaincode definition

Upload the chaincode

  1. Log on to the Alibaba Cloud Blockchain as a Service (BaaS) console.

  2. In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.

  3. On the page that appears, click the Chaincode Package management tab.

  4. On the Chaincode Package management tab, click Upload Chaincode.

  5. 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.

  6. Click OK.

    image.png

    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

  1. Log on to the Alibaba Cloud BaaS console.

  2. In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.

  3. On the page that appears, click the Chaincode Package management tab.

  4. 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.

    Precompilation

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

  1. Log on to the Alibaba Cloud BaaS console.

  2. In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.

  3. On the page that appears, click the Chaincode Package management tab.

  4. On the Chaincode Package management tab, find the chaincode whose definition you want to submit and click Submit definition in the Actions column.

  5. In the panel that appears, set parameters based on the instructions.

    image.png

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:- Support: If your chaincode implements the Init method, you can call the chaincode only after you send a special initialization transaction by using the SDK. In this case, select Support to initialize your chaincode. - Not supported: If your chaincode does not contain the Init method, you do not need to initialize your chaincode. Select Not supported in this case.

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: - OR ('org0MSP.peer','org1MSP.peer') indicates that one of the two organizations in the channel must endorse the chaincode transaction. - AND ('org0MSP.peer','org1MSP.peer') indicates that two organizations in this channel must endorse the chaincode transaction. - OutOf (a,'org0MSP.member','org1MSP.member','org2MSP.member'): If a is set to 1, one of the two organizations in the channel must endorse the chaincode transaction. If a is set to 2, organizations in at least two channels are required to endorse the chaincode transaction. If a equals the number of organizations, all organizations in the channel must endorse the chaincode transaction.

Note

The * in *.peer is determined by MSP ID in the preceding figure.

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: [{"name": "collectionName", "policy": "OR('org0MSP.peer')", "memberOnlyRead": false, "requiredPeerCount": 0, "maxPeerCount": 3, "blockToLive": 0}] indicates that the chaincode requires a private data namespace called collectionName. Only peers in Organization Org0 can obtain the private data in this namespace.

Note

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.

  1. 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.

    View the status information

  2. Wait for other organizations in the channel to confirm and submit the chaincode definition.

    Note

    By default, the chaincode definition can be submitted only after it is confirmed by 50% of the organizations in the channel.

    1. Log on to the Alibaba Cloud BaaS console as the member of another organization.

    2. In the My Organizations section of the Overview page, find the organization that you want to manage and click the name of the organization.

    3. 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.

    4. 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.

      Deployed

      Note

      If 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.

What to do next

Manage users and Access blockchain networks.