github.com/kaituanwang/hyperledger@v2.0.1+incompatible/docs/source/chaincode4noah.md (about) 1 # Chaincode for Operators 2 3 ## What is Chaincode? 4 5 Chaincode is a program, written in [Go](https://golang.org), [Node.js](https://nodejs.org), 6 or [Java](https://java.com/en/) that implements a prescribed interface. 7 Chaincode runs in a secured Docker container isolated from the endorsing peer 8 process. Chaincode initializes and manages ledger state through transactions 9 submitted by applications. 10 11 A chaincode typically handles business logic agreed to by members of the 12 network, so it may be considered as a "smart contract". Ledger updates created 13 by a chaincode are scoped exclusively to that chaincode and can't be accessed 14 directly by another chaincode. However, within the same network, given the 15 appropriate permission a chaincode may invoke another chaincode to access 16 its state. 17 18 In the following sections, we will explore chaincode through the eyes of a 19 blockchain network operator rather than an application developer. Chaincode 20 operators can use this tutorial to learn how to use the Fabric chainode 21 lifecycle to deploy and manage chaincode on their network. 22 23 ## Chaincode lifecycle 24 25 The Fabric chaincode lifecycle is a process that allows multiple organizations 26 to agree on how a chaincode will be operated before it can be used on a channel. 27 The tutorial will discuss how a chaincode operator would use the Fabric 28 lifecycle to perform the following tasks: 29 30 - [Install and define a chaincode](#install-and-define-a-chaincode) 31 - [Upgrade a chaincode](#upgrade-a-chaincode) 32 - [Deployment Scenarios](#deployment-scenarios) 33 - [Migrate to the new Fabric lifecycle](#migrate-to-the-new-fabric-lifecycle) 34 35 If you are upgrading from a v1.4.x network and need to edit your channel 36 configurations to enable the new lifecycle, check out 37 [Enabling the new chaincode lifecycle](./enable_cc_lifecycle.html). 38 39 ## Install and define a chaincode 40 41 Fabric chaincode lifecycle requires that organizations agree to the parameters 42 that define a chaincode, such as name, version, and the chaincode endorsement 43 policy. Channel members come to agreement using the following four steps. Not 44 every organization on a channel needs to complete each step. 45 46 1. **Package the chaincode:** This step can be completed by one organization or 47 by each organization. 48 2. **Install the chaincode on your peers:** Every organization that will use the 49 chaincode to endorse a transaction or query the ledger needs to complete this 50 step. 51 3. **Approve a chaincode definition for your organization:** Every organization 52 that will use the chaincode needs to complete this step. The chaincode 53 definition needs to be approved by a sufficient number of organizations 54 to satisfy the channel's LifecycleEndorsment policy (a majority, by default) 55 before the chaincode can be started on the channel. 56 4. **Commit the chaincode definition to the channel:** The commit transaction 57 needs to be submitted by one organization once the required number of 58 organizations on the channel have approved. The submitter first collects 59 endorsements from enough peers of the organizations that have approved, and 60 then submits the transaction to commit the chaincode definition. 61 62 This tutorial provides a detailed overview of the operations of the Fabric 63 chaincode lifecycle rather than the specific commands. To learn more about how 64 to use the Fabric lifecycle using the Peer CLI, see [Install and define a chaincode](build_network.html#install-define-chaincode) 65 in the Building your First Network Tutorial or the [peer lifecycle command reference](commands/peerlifecycle.html). 66 67 ### Step One: Packaging the smart contract 68 69 Chaincode needs to be packaged in a tar file before it can be installed on your 70 peers. You can package a chaincode using the Fabric peer binaries, the Node 71 Fabric SDK, or a third party tool such as GNU tar. When you create a chaincode 72 package, you need to provide a chaincode package label to create a succinct and 73 human readable description of the package. 74 75 If you use a third party tool to package the chaincode, the resulting file needs 76 to be in the format below. The Fabric peer binaries and the Fabric SDKs will 77 automatically create a file in this format. 78 - The chaincode needs to be packaged in a tar file, ending with a `.tar.gz` file 79 extension. 80 - The tar file needs to contain two files (no directory): a metadata file 81 "Chaincode-Package-Metadata.json" and another tar containing the chaincode 82 files. 83 - "Chaincode-Package-Metadata.json" contains JSON that specifies the 84 chaincode language, code path, and package label. 85 You can see an example of a metadata file below: 86 ``` 87 {"Path":"fabric-samples/chaincode/fabcar/go","Type":"golang","Label":"fabcarv1"} 88 ``` 89 90 ![Packaging the chaincode](lifecycle/Lifecycle-package.png) 91 92 *The chaincode is packaged separately by Org1 and Org2. Both organizations use 93 MYCC_1 as their package label in order to identify the package using the name 94 and version. It is not necessary for organizations to use the same package 95 label.* 96 97 ### Step Two: Install the chaincode on your peers 98 99 You need to install the chaincode package on every peer that will execute and 100 endorse transactions. Whether using the CLI or an SDK, you need to complete this 101 step using your **Peer Administrator**. Your peer will build the chaincode 102 after the chaincode is installed, and return a build error if there is a problem 103 with your chaincode. It is recommended that organizations only package a chaincode 104 once, and then install the same package on every peer that belongs to their org. 105 If a channel wants to ensure that each organization is running the same chaincode, 106 one organization can package a chaincode and send it to other channel members 107 out of band. 108 109 A successful install command will return a chaincode package identifier, which 110 is the package label combined with a hash of the package. This package 111 identifier is used to associate a chaincode package installed on your peers with 112 a chaincode definition approved by your organization. **Save the identifier** 113 for next step. You can also find the package identifier by querying the packages 114 installed on your peer using the Peer CLI. 115 116 ![Installing the chaincode](lifecycle/Lifecycle-install.png) 117 118 *A peer administrator from Org1 and Org2 installs the chaincode package MYCC_1 119 on the peers joined to the channel. Installing the chaincode package builds the 120 chaincode and creates a package identifier of MYCC_1:hash.* 121 122 ### Step Three: Approve a chaincode definition for your organization 123 124 The chaincode is governed by a **chaincode definition**. When channel members 125 approve a chaincode definition, the approval acts as a vote by an organization 126 on the chaincode parameters it accepts. These approved organization definitions 127 allow channel members to agree on a chaincode before it can be used on a channel. 128 The chaincode definition includes the following parameters, which need to be 129 consistent across organizations: 130 131 - **Name:** The name that applications will use when invoking the chaincode. 132 - **Version:** A version number or value associated with a given chaincodes 133 package. If you upgrade the chaincode binaries, you need to change your 134 chaincode version as well. 135 - **Sequence:** The number of times the chaincode has been defined. This value 136 is an integer, and is used to keep track of chaincode upgrades. For example, 137 when you first install and approve a chaincode definition, the sequence number 138 will be 1. When you next upgrade the chaincode, the sequence number will be 139 incremented to 2. 140 - **Endorsement Policy:** Which organizations need to execute and validate the 141 transaction output. The endorsement policy can be expressed as a string passed 142 to the CLI or the SDK, or it can reference a policy in the channel config. By 143 default, the endorsement policy is set to ``Channel/Application/Endorsement``, 144 which defaults to require that a majority of organizations in the channel 145 endorse a transaction. 146 - **Collection Configuration:** The path to a private data collection definition 147 file associated with your chaincode. For more information about private data 148 collections, see the [Private Data architecture reference](https://hyperledger-fabric.readthedocs.io/en/master/private-data-arch.html). 149 - **Initialization:** All chaincode need to contain an ``Init`` function that is 150 used to initialize the chaincode. By default, this function is never executed. 151 However, you can use the chaincode definition to request that the ``Init`` 152 function be callable. If execution of ``Init`` is requested, fabric will ensure 153 that ``Init`` is invoked before any other function and is only invoked once. 154 - **ESCC/VSCC Plugins:** The name of a custom endorsement or validation 155 plugin to be used by this chaincode. 156 157 The chaincode definition also includes the **Package Identifier**. This is a 158 required parameter for each organization that wants to use the chaincode. The 159 package ID does not need to be the same for all organizations. An organization 160 can approve a chaincode definition without installing a chaincode package or 161 including the identifier in the definition. 162 163 Each channel member that wants to use the chaincode needs to approve a chaincode 164 definition for their organization. This approval needs to be submitted to the 165 ordering service, after which it is distributed to all peers. This approval 166 needs to be submitted by your **Organization Administrator**. After the approval 167 transaction has been successfully submitted, the approved definition is stored 168 in a collection that is available to all the peers of your organization. As a 169 result you only need to approve a chaincode for your organization once, even if 170 you have multiple peers. 171 172 ![Approving the chaincode definition](lifecycle/Lifecycle-approve.png) 173 174 *An organization administrator from Org1 and Org2 approve the chaincode definition 175 of MYCC for their organization. The chaincode definition includes the chaincode 176 name, version, and the endorsement policy, among other fields. Since both 177 organizations will use the chaincode to endorse transactions, the approved 178 definitions for both organizations need to include the packageID.* 179 180 ### Step Four: Commit the chaincode definition to the channel 181 182 Once a sufficient number of channel members have approved a chaincode definition, 183 one organization can commit the definition to the channel. You can use the 184 ``checkcommitreadiness`` command to check whether committing the chaincode 185 definition should be successful based on which channel members have approved a 186 definition before committing it to the channel using the peer CLI. The commit 187 transaction proposal is first sent to the peers of channel members, who query the 188 chaincode definition approved for their organizations and endorse the definition 189 if their organization has approved it. The transaction is then submitted to the 190 ordering service, which then commits the chaincode definition to the channel. 191 The commit definition transaction needs to be submitted as the **Organization** 192 **Administrator**. 193 194 The number of organizations that need to approve a definition before it can be 195 successfully committed to the channel is governed by the 196 ``Channel/Application/LifecycleEndorsement`` policy. By default, this policy 197 requires that a majority of organizations in the channel endorse the transaction. 198 The LifecycleEndorsement policy is separate from the chaincode endorsement 199 policy. For example, even if a chaincode endorsement policy only requires 200 signatures from one or two organizations, a majority of channel members still 201 need to approve the chaincode definition according to the default policy. When 202 committing a channel definition, you need to target enough peer organizations in 203 the channel to satisfy your LifecycleEndorsement policy. You can learn more 204 about the Fabric chaincode lifecycle policies in the [Policies concept topic](policies/policies.html). 205 206 You can also set the ``Channel/Application/LifecycleEndorsement`` policy to be a 207 signature policy and explicitly specify the set of organizations on the channel 208 that can approve a chaincode definition. This allows you to create a channel where 209 a select number of organizations act as chaincode administrators and govern the 210 business logic used by the channel. You can also use a signature policy if your 211 channel has a large number Idemix organizations, which cannot approve 212 chaincode definitions or endorse chaincode and may prevent the channel from 213 reaching a majority as a result. 214 215 ![Committing the chaincode definition to the channel](lifecycle/Lifecycle-commit.png) 216 217 *One organization administrator from Org1 or Org2 commits the chaincode definition 218 to the channel. The definition on the channel does not include the packageID.* 219 220 An organization can approve a chaincode definition without installing the 221 chaincode package. If an organization does not need to use the chaincode, they 222 can approve a chaincode definition without a package identifier to ensure that 223 the Lifecycle Endorsement policy is satisfied. 224 225 After the chaincode definition has been committed to the channel, the chaincode 226 container will launch on all of the peers where the chaincode has been installed, 227 allowing channel members to start using the chaincode. It may take a few minutes for 228 the chaincode container to start. You can use the chaincode definition to require 229 the invocation of the ``Init`` function to initialize the chaincode. If the 230 invocation of the ``Init`` function is requested, the first invoke of the 231 chaincode must be a call to the ``Init`` function. The invoke of the ``Init`` 232 function is subject to the chaincode endorsement policy. 233 234 ![Starting the chaincode on the channel](lifecycle/Lifecycle-start.png) 235 236 *Once MYCC is defined on the channel, Org1 and Org2 can start using the chaincode. The first invoke of the chaincode on each peer starts the chaincode 237 container on that peer.* 238 239 ## Upgrade a chaincode 240 241 You can upgrade a chaincode using the same Fabric lifecycle process as you used 242 to install and start the chainocode. You can upgrade the chaincode binaries, or 243 only update the chaincode policies. Follow these steps to upgrade a chaincode: 244 245 1. **Repackage the chaincode:** You only need to complete this step if you are 246 upgrading the chaincode binaries. 247 248 ![Re-package the chaincode package](lifecycle/Lifecycle-upgrade-package.png) 249 250 *Org1 and Org2 upgrade the chaincode binaries and repackage the chaincode. Both organizations use a different package label.* 251 252 2. **Install the new chaincode package on your peers:** Once again, you only 253 need to complete this step if you are upgrading the chaincode binaries. 254 Installing the new chaincode package will generate a package ID, which you will 255 need to pass to the new chaincode definition. You also need to change the 256 chaincode version, which is used by the lifecycle process to track if the 257 chaincode binaries have been upgraded. 258 259 ![Re-install the chaincode package](lifecycle/Lifecycle-upgrade-install.png) 260 261 *Org1 and Org2 install the new package on their peers. The installation creates a new packageID.* 262 263 3. **Approve a new chaincode definition:** If you are upgrading the chaincode 264 binaries, you need to update the chaincode version and the package ID in the 265 chaincode definition. You can also update your chaincode endorsement policy 266 without having to repackage your chaincode binaries. Channel members simply 267 need to approve a definition with the new policy. The new definition needs to 268 increment the **sequence** variable in the definition by one. 269 270 ![Approve a new chaincode definition](lifecycle/Lifecycle-upgrade-approve.png) 271 272 *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.* 273 274 4. **Commit the definition to the channel:** When a sufficient number of channel 275 members have approved the new chaincode definition, one organization can 276 commit the new definition to upgrade the chaincode definition to the channel. 277 There is no separate upgrade command as part of the lifecycle process. 278 279 ![Commit the new definition to the channel](lifecycle/Lifecycle-upgrade-commit.png) 280 281 *An organization administrator from Org1 or Org2 commits the new chaincode definition to the channel.* 282 283 After you commit the chaincode definition, a new chaincode container will 284 launch with the code from the upgraded chaincode binaries. If you requested the 285 execution of the ``Init`` function in the chaincode definition, you need to 286 initialize the upgraded chaincode by invoking the ``Init`` function again after 287 the new definition is successfully committed. If you updated the chaincode 288 definition without changing the chaincode version, the chaincode container will 289 remain the same and you do not need to invoke ``Init`` function. 290 291 ![Upgrade the chaincode](lifecycle/Lifecycle-upgrade-start.png) 292 293 *Once the new definition has been committed to the channel, each peer will automatically start the new chaincode container.* 294 295 The Fabric chaincode lifecycle uses the **sequence** in the chaincode definition 296 to keep track of upgrades. All channel members need to increment the sequence 297 number by one and approve a new definition to upgrade the chaincode. The version 298 parameter is used to track the chaincode binaries, and needs to be changed only 299 when you upgrade the chaincode binaries. 300 301 ## Deployment scenarios 302 303 The following examples illustrate how you can use the Fabric chaincode lifecycle 304 to manage channels and chaincode. 305 306 ### Joining a channel 307 308 A new organization can join a channel with a chaincode already defined, and start 309 using the chaincode after installing the chaincode package and approving the 310 chaincode definition that has already been committed to the channel. 311 312 ![Approve a chaincode definition](lifecycle/Lifecycle-join-approve.png) 313 314 *Org3 joins the channel and approves the same chaincode definition that was 315 previously committed to the channel by Org1 and Org2.* 316 317 After approving the chaincode definition, the new organization can start using 318 the chaincode after the package has been installed on their peers. The definition 319 does not need to be committed again. If the endorsement policy is set the default 320 policy that requires endorsements from a majority of channel members, then the 321 endorsement policy will be updated automatically to include the new organization. 322 323 ![Start the chaincode](lifecycle/Lifecycle-join-start.png) 324 325 *The chaincode container will start after the first invoke of the chaincode on 326 the Org3 peer.* 327 328 ### Updating an endorsement policy 329 330 You can use the chaincode definition to update an endorsement policy without 331 having to repackage or re-install the chaincode. Channel members can approve 332 a chaincode definition with a new endorsement policy and commit it to the 333 channel. 334 335 ![Approve new chaincode definition](lifecycle/Lifecycle-endorsement-approve.png) 336 337 *Org1, Org2, and Org3 approve a new endorsement policy requiring that all three 338 organizations endorse a transaction. They increment the definition sequence from 339 one to two, but do not need to update the chaincode version.* 340 341 The new endorsement policy will take effect after the new definition is 342 committed to the channel. Channel members do not have to restart the chaincode 343 container by invoking the chaincode or executing the `Init` function in order to 344 update the endorsement policy. 345 346 ![Commit new chaincode definition](lifecycle/Lifecycle-endorsement-commit.png) 347 348 *One organization commits the new chaincode definition to the channel to 349 update the endorsement policy.* 350 351 ### Approving a definition without installing the chaincode 352 353 You can approve a chaincode definition without installing the chaincode package. 354 This allows you to endorse a chaincode definition before it is committed to the 355 channel, even if you do not want to use the chaincode to endorse transactions or 356 query the ledger. You need to approve the same parameters as other members of the 357 channel, but not need to include the packageID as part of the chaincode 358 definition. 359 360 ![Org3 does not install the chaincode](lifecycle/Lifecycle-no-package.png) 361 362 *Org3 does not install the chaincode package. As a result, they do not need to 363 provide a packageID as part of chaincode definition. However, Org3 can still 364 endorse the definition of MYCC that has been committed to the channel.* 365 366 ### One organization disagrees on the chaincode definition 367 368 An organization that does not approve a chaincode definition that has been 369 committed to the channel cannot use the chaincode. Organizations that have 370 either not approved a chaincode definition, or approved a different chaincode 371 definition will not be able to execute the chaincode on their peers. 372 373 ![Org3 disagrees on the chaincode](lifecycle/Lifecycle-one-disagrees.png) 374 375 *Org3 approves a chaincode definition with a different endorsement policy than 376 Org1 and Org2. As a result, Org3 cannot use the MYCC chaincode on the channel. 377 However, Org1 or Org2 can still get enough endorsements to commit the definition 378 to the channel and use the chaincode. Transactions from the chaincode will still 379 be added to the ledger and stored on the Org3 peer. However, the Org3 will not 380 be able to endorse transactions.* 381 382 An organization can approve a new chaincode definition with any sequence number 383 or version. This allows you to approve the definition that has been committed 384 to the channel and start using the chaincode. You can also approve a new 385 chaincode definition in order to correct any mistakes made in the process of 386 approving or packaging a chaincode. 387 388 ### The channel does not agree on a chaincode definition 389 390 If the organizations on a channel do not agree on a chaincode definition, the 391 definition cannot be committed to the channel. None of the channel members will 392 be able to use the chaincode. 393 394 ![Majority disagree on the chaincode](lifecycle/Lifecycle-majority-disagree.png) 395 396 *Org1, Org2, and Org3 all approve different chaincode definitions. As a result, 397 no member of the channel can get enough endorsements to commit a chaincode 398 definition to the channel. No channel member will be able to use the chaincode.* 399 400 ### Organizations install different chaincode packages 401 402 Each organization can use a different packageID when they approve a chaincode 403 definition. This allows channel members to install different chaincode binaries 404 that use the same endorsement policy and read and write to data in the same 405 chaincode namespace. 406 407 Channel members can use this capability to install chaincode written in 408 different languages and work with the language they are most comfortable. As 409 long as the chaincode generates the same read-write sets, channel members using 410 chaincode in different languages will be able to endorse transactions and commit 411 them to the ledger. However, organizations should test that their chaincode 412 is consistent and that they are able to generate valid endorsements before 413 defining it on a channel in production. 414 415 ![Using different chaincode languages](lifecycle/Lifecycle-languages.png) 416 417 *Org1 installs a package of the MYCC chaincode written in Golang, while Org2 418 installs MYCC written in Java.* 419 420 Organizations can also use this capability to install smart contracts that 421 contain business logic that is specific to their organization. Each 422 organization's smart contract could contain additional validation that the 423 organization requires before their peers endorse a transaction. Each organization 424 can also write code that helps integrate the smart contract with data from their 425 existing systems. 426 427 ![Using different chaincode binaries](lifecycle/Lifecycle-binaries.png) 428 429 *Org1 and Org2 each install versions of the MYCC chaincode containing business 430 logic that is specific to their organization.* 431 432 ### Creating multiple chaincodes using one package 433 434 You can use one chaincode package to create multiple chaincode instances on a 435 channel by approving and committing multiple chaincode definitions. Each 436 definition needs to specify a different chaincode name. This allows you to run 437 multiple instances of a smart contract on a channel, but have the contract be 438 subject to different endorsement policies. 439 440 ![Starting multiple chaincodes](lifecycle/Lifecycle-multiple.png) 441 442 *Org1 and Org2 use the MYCC_1 chaincode package to approve and commit two 443 different chaincode definitions. As a result, both peers have two chaincode 444 containers running on their peers. MYCC1 has an endorsement policy of 1 out of 2, 445 while MYCC2 has an endorsement policy of 2 out of 2.* 446 447 ## Migrate to the new Fabric lifecycle 448 449 For information about migrating to the new lifecycle, check out [Considerations for getting to v2.0](./upgrade_to_newest_version.html#chaincode-lifecycle). 450 451 If you need to update your channel configurations to enable the new lifecycle, check out [Enabling the new chaincode lifecycle](./enable_cc_lifecycle.html). 452 453 <!--- Licensed under Creative Commons Attribution 4.0 International License 454 https://creativecommons.org/licenses/by/4.0/ -->