Fabric chaincode lifecycle

官方文档:Fabric chaincode lifecycle

Install and define a chaincode

  1. Package the chaincode: This step can be completed by one organization or by each organization.
  2. Install the chaincode on your peers: Every organization that will use the chaincode to endorse a transaction or query the ledger needs to complete this step.
  3. Approve a chaincode definition for your organization: Every organization that will use the chaincode needs to complete this step. The chaincode definition needs to be approved by a sufficient number of organizations to satisfy the channel’s LifecycleEndorsment policy (a majority, by default) before the chaincode can be started on the channel.
  4. Commit the chaincode definition to the channel: The commit transaction needs to be submitted by one organization once the required number of organizations on the channel have approved. The submitter first collects endorsements from enough peers of the organizations that have approved, and then submits the transaction to commit the chaincode definition.

Upgrade a chaincode

  1. Repackage the chaincode: You only need to complete this step if you are upgrading the chaincode binaries.

    Re-package the chaincode package

    Org1 and Org2 upgrade the chaincode binaries and repackage the chaincode. Both organizations use a different package label.

  2. Install the new chaincode package on your peers: Once again, you only need to complete this step if you are upgrading the chaincode binaries. Installing the new chaincode package will generate a package ID, which you will need to pass to the new chaincode definition. You also need to change the chaincode version, which is used by the lifecycle process to track if the chaincode binaries have been upgraded.

    Re-install the chaincode package

    Org1 and Org2 install the new package on their peers. The installation creates a new packageID.

  3. Approve a new chaincode definition: If you are upgrading the chaincode binaries, you need to update the chaincode version and the package ID in the chaincode definition. You can also update your chaincode endorsement policy without having to repackage your chaincode binaries. Channel members simply need to approve a definition with the new policy. The new definition needs to increment the sequence variable in the definition by one.

    Approve a new chaincode definition

    Organization administrators from Org1 and Org2 approve the new chaincode definition for their respective organizations. The new definition references the new packageID and changes the chaincode version. Since this is the first update of the chaincode, the sequence is incremented from one to two.

  4. Commit the definition to the channel: When a sufficient number of channel members have approved the new chaincode definition, one organization can commit the new definition to upgrade the chaincode definition to the channel. There is no separate upgrade command as part of the lifecycle process.

    Commit the new definition to the channel

    An organization administrator from Org1 or Org2 commits the new chaincode definition to the channel.

Organizations install different chaincode packages

Each organization can use a different packageID when they approve a chaincode definition. This allows channel members to install different chaincode binaries that use the same endorsement policy and read and write to data in the same chaincode namespace.

每个组织在批准链码定义时都可以使用不同的packageID。这允许通道成员安装使用相同背书策略的不同链代码二进制文件,并在同一链码名称空间中读取和写入数据。

Organizations can use this capability to install smart contracts that contain business logic that is specific to their organization. Each organization’s smart contract could contain additional validation that the organization requires before their peers endorse a transaction. Each organization can also write code that helps integrate the smart contract with data from their existing systems.

组织可以使用此功能来安装包含其组织的特殊的业务逻辑的智能合约。每个组织的智能合约都可以包含组织在其peer认可交易之前所需的其他验证。每个组织还可以编写代码将智能合约与他们现有系统中的数据集成在一起。

Using different chaincode binaries

Org1 and Org2 each install versions of the MYCC chaincode containing business logic that is specific to their organization.

Creating multiple chaincodes using one package

You can use one chaincode package to create multiple chaincode instances on a channel by approving and committing multiple chaincode definitions. Each definition needs to specify a different chaincode name. This allows you to run multiple instances of a smart contract on a channel, but have the contract be subject to different endorsement policies.

您可以通过批准并提交多个链码定义,使用一个链码包在一个通道上创建多个链码实例。每个定义都需要指定一个不同的链码名称。这使您可以在一个通道上运行智能合约的多个实例,但是要让合约遵循不同的背书策略。

Starting multiple chaincodes

Org1 and Org2 use the MYCC_1 chaincode package to approve and commit two different chaincode definitions. As a result, both peers have two chaincode containers running on their peers. MYCC1 has an endorsement policy of 1 out of 2, while MYCC2 has an endorsement policy of 2 out of 2.

个人疑问:部署相的链码,使用不同的背书策略,这么做有什么用呢?

Migrate to the new Fabric lifecycle

For information about migrating to the new lifecycle, check out Considerations for getting to v2.0.

If you need to update your channel configurations to enable the new lifecycle, check out Enabling the new chaincode lifecycle.

More information

You can watch video below to learn more about the motivation of the new Fabric chaincode lifecycle and how it is implemented.