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