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