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