github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/docs/source/deploy_chaincode.md (about)

     1  # Deploying a smart contract to a channel
     2  
     3  End users interact with the blockchain ledger by invoking smart contracts. In Hechain, smart contracts are deployed in packages referred to as chaincode. Organizations that want to validate transactions or query the ledger need to install a chaincode on their peers. After a chaincode has been installed on the peers joined to a channel, channel members can deploy the chaincode to the channel and use the smart contracts in the chaincode to create or update assets on the channel ledger.
     4  
     5  A chaincode is deployed to a channel using a process known as the Fabric chaincode lifecycle. The Fabric chaincode lifecycle allows multiple organizations to agree how a chaincode will be operated before it can be used to create transactions. For example, while an endorsement policy specifies which organizations need to execute a chaincode to validate a transaction, channel members need to use the Fabric chaincode lifecycle to agree on the chaincode endorsement policy. For a more in-depth overview about how to deploy and manage a chaincode on a channel, see [Fabric chaincode lifecycle](./chaincode_lifecycle.html).
     6  
     7  You can use this tutorial to learn how to use the [peer lifecycle chaincode commands](./commands/peerlifecycle.html) to deploy a chaincode to a channel of the Fabric test network. Once you have an understanding of the commands, you can use the steps in this tutorial to deploy your own chaincode to the test network, or to deploy chaincode to a production network. In this tutorial, you will deploy the asset-transfer (basic) chaincode that is used by the [Running a Fabric Application tutorial](./write_first_app.html).
     8  
     9  **Note:** These instructions use the Fabric chaincode lifecycle introduced in the v2.0 release. If you would like to use the previous lifecycle to install and instantiate a chaincode, visit the [v1.4 version of the Fabric documentation](https://hyperledger-fabric.readthedocs.io/en/release-1.4).
    10  
    11  ## Start the network
    12  
    13  We will start by deploying an instance of the Fabric test network. Before you begin, make sure that you have installed the necessary software by following the instructions on [getting_started](getting_started.html). Use the following command to navigate to the test network directory within your local clone of the `fabric-samples` repository:
    14  
    15  ```
    16  cd fabric-samples/test-network
    17  ```
    18  
    19  For the sake of this tutorial, we want to operate from a known initial state. The following command will kill any active or stale docker containers and remove previously generated artifacts.
    20  
    21  ```
    22  ./network.sh down
    23  ```
    24  
    25  You can then use the following command to start the test network:
    26  
    27  ```
    28  ./network.sh up createChannel
    29  ```
    30  
    31  The `createChannel` command creates a channel named `mychannel` with two channel members, Org1 and Org2. The command also joins a peer that belongs to each organization to the channel. If the network and the channel are created successfully, you can see the following message printed in the logs:
    32  
    33  ```
    34  ========= Channel successfully joined ===========
    35  ```
    36  
    37  We can now use the Peer CLI to deploy the asset-transfer (basic) chaincode to the channel using the following steps:
    38  
    39  - [Step one: Package the smart contract](#package-the-smart-contract)
    40  - [Step two: Install the chaincode package](#install-the-chaincode-package)
    41  - [Step three: Approve a chaincode definition](#approve-a-chaincode-definition)
    42  - [Step four: Committing the chaincode definition to the channel](#committing-the-chaincode-definition-to-the-channel)
    43  
    44  ## Setup Logspout (optional)
    45  
    46  This step is not required but is extremely useful for troubleshooting chaincode. To monitor the logs of the smart contract, an administrator can view the aggregated output from a set of Docker containers using the `logspout` [tool](https://logdna.com/what-is-logspout/). The tool collects the output streams from different Docker containers into one place, making it easy to see what's happening from a single window. This can help administrators debug problems when they install smart contracts or developers when they invoke smart contracts. Because some containers are created purely for the purposes of starting a smart contract and only exist for a short time, it is helpful to collect all of the logs from your network.
    47  
    48  A script to install and configure Logspout, `monitordocker.sh`, is already included in the `commercial-paper` sample in the Fabric samples. We will use the same script in this tutorial as well. The Logspout tool will continuously stream logs to your terminal, so you will need to use a new terminal window. Open a new terminal and navigate to the `test-network` directory.
    49  
    50  ```
    51  cd fabric-samples/test-network
    52  ```
    53  
    54  You can run the `monitordocker.sh` script from any directory. For ease of use, we will copy the `monitordocker.sh` script from the `commercial-paper` sample to your working directory
    55  
    56  ```
    57  cp ../commercial-paper/organization/digibank/configuration/cli/monitordocker.sh .
    58  # if you're not sure where it is
    59  find . -name monitordocker.sh
    60  ```
    61  
    62  You can then start Logspout by running the following command:
    63  
    64  ```
    65  ./monitordocker.sh fabric_test
    66  ```
    67  
    68  You should see output similar to the following:
    69  
    70  ```
    71  Starting monitoring on all containers on the network net_basic
    72  Unable to find image 'gliderlabs/logspout:latest' locally
    73  latest: Pulling from gliderlabs/logspout
    74  4fe2ade4980c: Pull complete
    75  decca452f519: Pull complete
    76  ad60f6b6c009: Pull complete
    77  Digest: sha256:374e06b17b004bddc5445525796b5f7adb8234d64c5c5d663095fccafb6e4c26
    78  Status: Downloaded newer image for gliderlabs/logspout:latest
    79  1f99d130f15cf01706eda3e1f040496ec885036d485cb6bcc0da4a567ad84361
    80  
    81  ```
    82  
    83  You will not see any logs at first, but this will change when we deploy our chaincode. It can be helpful to make this terminal window wide and the font small.
    84  
    85  ## Package the smart contract
    86  
    87  We need to package the chaincode before it can be installed on our peers. The steps are different if you want to install a smart contract written in [Go](#go), [JavaScript](#javascript), or [Typescript](#typescript).
    88  
    89  ### Go
    90  
    91  Before we package the chaincode, we need to install the chaincode dependencies. Navigate to the folder that contains the Go version of the asset-transfer (basic) chaincode.
    92  
    93  ```
    94  cd fabric-samples/asset-transfer-basic/chaincode-go
    95  ```
    96  
    97  The sample uses a Go module to install the chaincode dependencies. The dependencies are listed in a `go.mod` file in the `asset-transfer-basic/chaincode-go` directory. You should take a moment to examine this file.
    98  
    99  ```
   100  $ cat go.mod
   101  module github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go
   102  
   103  go 1.14
   104  
   105  require (
   106          github.com/golang/protobuf v1.3.2
   107          github.com/hyperledger/fabric-chaincode-go v0.0.0-20200424173110-d7076418f212
   108          github.com/hyperledger/fabric-contract-api-go v1.1.0
   109          github.com/hyperledger/fabric-protos-go v0.0.0-20200424173316-dd554ba3746e
   110          github.com/stretchr/testify v1.5.1
   111  )
   112  ```
   113  
   114  The `go.mod` file imports the Fabric contract API into the smart contract package. You can open `asset-transfer-basic/chaincode-go/chaincode/smartcontract.go` in a text editor to see how the contract API is used to define the `SmartContract` type at the beginning of the smart contract:
   115  
   116  ```
   117  // SmartContract provides functions for managing an Asset
   118  type SmartContract struct {
   119  	contractapi.Contract
   120  }
   121  ```
   122  
   123  The `SmartContract` type is then used to create the transaction context for the functions defined within the smart contract that read and write data to the blockchain ledger.
   124  
   125  ```
   126  // CreateAsset issues a new asset to the world state with given details.
   127  func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, id string, color string, size int, owner string, appraisedValue int) error {
   128  	exists, err := s.AssetExists(ctx, id)
   129  	if err != nil {
   130  		return err
   131  	}
   132  	if exists {
   133  		return fmt.Errorf("the asset %s already exists", id)
   134  	}
   135  
   136  	asset := Asset{
   137  		ID:             id,
   138  		Color:          color,
   139  		Size:           size,
   140  		Owner:          owner,
   141  		AppraisedValue: appraisedValue,
   142  	}
   143  	assetJSON, err := json.Marshal(asset)
   144  	if err != nil {
   145  		return err
   146  	}
   147  
   148  	return ctx.GetStub().PutState(id, assetJSON)
   149  }
   150  
   151  ```
   152  
   153  You can learn more about the Go contract API by visiting the [API documentation](https://github.com/hyperledger/fabric-contract-api-go) and the [smart contract processing topic](developapps/smartcontract.html).
   154  
   155  To install the smart contract dependencies, run the following command from the `asset-transfer-basic/chaincode-go` directory.
   156  
   157  ```
   158  GO111MODULE=on go mod vendor
   159  ```
   160  
   161  If the command is successful, the go packages will be installed inside a `vendor` folder.
   162  
   163  Now that we that have our dependencies, we can create the chaincode package. Navigate back to our working directory in the `test-network` folder so that we can package the chaincode together with our other network artifacts.
   164  
   165  ```
   166  cd ../../test-network
   167  ```
   168  
   169  You can use the `peer` CLI to create a chaincode package in the required format. The `peer` binaries are located in the `bin` folder of the `fabric-samples` repository. Use the following command to add those binaries to your CLI Path:
   170  
   171  ```
   172  export PATH=${PWD}/../bin:$PATH
   173  ```
   174  
   175  You also need to set the `FABRIC_CFG_PATH` to point to the `core.yaml` file in the `fabric-samples` repository:
   176  
   177  ```
   178  export FABRIC_CFG_PATH=$PWD/../config/
   179  ```
   180  
   181  To confirm that you are able to use the `peer` CLI, check the version of the binaries. The binaries need to be version `2.0.0` or later to run this tutorial.
   182  
   183  ```
   184  peer version
   185  ```
   186  
   187  You can now create the chaincode package using the [peer lifecycle chaincode package](commands/peerlifecycle.html#peer-lifecycle-chaincode-package) command:
   188  
   189  ```
   190  peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_1.0
   191  ```
   192  
   193  This command will create a package named `basic.tar.gz` in your current directory.
   194  The `--lang` flag is used to specify the chaincode language and the `--path` flag provides the location of your smart contract code. The path must be a fully qualified path or a path relative to your present working directory.
   195  The `--label` flag is used to specify a chaincode label that will identify your chaincode after it is installed. It is recommended that your label include the chaincode name and version.
   196  
   197  Now that we created the chaincode package, we can [install the chaincode](#install-the-chaincode-package) on the peers of the test network.
   198  
   199  ### JavaScript
   200  
   201  Before we package the chaincode, we need to install the chaincode dependencies. Navigate to the folder that contains the JavaScript version of the asset-transfer (basic) chaincode.
   202  
   203  ```
   204  cd fabric-samples/asset-transfer-basic/chaincode-javascript
   205  ```
   206  
   207  The dependencies are listed in the `package.json` file in the `asset-transfer-basic/chaincode-javascript` directory. You should take a moment to examine this file. You can find the dependencies section displayed below:
   208  
   209  ```
   210  "dependencies": {
   211  		"fabric-contract-api": "^2.0.0",
   212  		"fabric-shim": "^2.0.0"
   213  ```
   214  
   215  The `package.json` file imports the Fabric contract class into the smart contract package. You can open `lib/assetTransfer.js` in a text editor to see the contract class imported into the smart contract and used to create the asset-transfer (basic) class.
   216  
   217  ```
   218  const { Contract } = require('fabric-contract-api');
   219  
   220  class AssetTransfer extends Contract {
   221  	...
   222  }
   223  
   224  ```
   225  
   226  The `AssetTransfer` class provides the transaction context for the functions defined within the smart contract that read and write data to the blockchain ledger.
   227  
   228  ```
   229  async CreateAsset(ctx, id, color, size, owner, appraisedValue) {
   230          const asset = {
   231              ID: id,
   232              Color: color,
   233              Size: size,
   234              Owner: owner,
   235              AppraisedValue: appraisedValue,
   236          };
   237  
   238          await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset)));
   239      }
   240  ```
   241  
   242  You can learn more about the JavaScript contract API by visiting the [API documentation](https://hyperledger.github.io/fabric-chaincode-node/{BRANCH}/api/) and the [smart contract processing topic](developapps/smartcontract.html).
   243  
   244  To install the smart contract dependencies, run the following command from the `asset-transfer-basic/chaincode-javascript` directory.
   245  
   246  ```
   247  npm install
   248  ```
   249  
   250  If the command is successful, the JavaScript packages will be installed inside a `node_modules` folder.
   251  
   252  Now that we that have our dependencies, we can create the chaincode package. Navigate back to our working directory in the `test-network` folder so that we can package the chaincode together with our other network artifacts.
   253  
   254  ```
   255  cd ../../test-network
   256  ```
   257  
   258  You can use the `peer` CLI to create a chaincode package in the required format. The `peer` binaries are located in the `bin` folder of the `fabric-samples` repository. Use the following command to add those binaries to your CLI Path:
   259  
   260  ```
   261  export PATH=${PWD}/../bin:$PATH
   262  ```
   263  
   264  You also need to set the `FABRIC_CFG_PATH` to point to the `core.yaml` file in the `fabric-samples` repository:
   265  
   266  ```
   267  export FABRIC_CFG_PATH=$PWD/../config/
   268  ```
   269  
   270  To confirm that you are able to use the `peer` CLI, check the version of the binaries. The binaries need to be version `2.0.0` or later to run this tutorial.
   271  
   272  ```
   273  peer version
   274  ```
   275  
   276  You can now create the chaincode package using the [peer lifecycle chaincode package](commands/peerlifecycle.html#peer-lifecycle-chaincode-package) command:
   277  
   278  ```
   279  peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-javascript/ --lang node --label basic_1.0
   280  ```
   281  
   282  This command will create a package named `basic.tar.gz` in your current directory. The `--lang` flag is used to specify the chaincode language and the `--path` flag provides the location of your smart contract code. The `--label` flag is used to specify a chaincode label that will identify your chaincode after it is installed. It is recommended that your label include the chaincode name and version.
   283  
   284  Now that we created the chaincode package, we can [install the chaincode](#install-the-chaincode-package) on the peers of the test network.
   285  
   286  ### Typescript
   287  
   288  Before we package the chaincode, we need to install the chaincode dependencies. Navigate to the folder that contains the TypeScript version of the asset-transfer (basic) chaincode.
   289  
   290  ```
   291  cd fabric-samples/asset-transfer-basic/chaincode-typescript
   292  ```
   293  
   294  The dependencies are listed in the `package.json` file in the `asset-transfer-basic/chaincode-typescript` directory. You should take a moment to examine this file. You can find the dependencies section displayed below:
   295  
   296  ```
   297  "dependencies": {
   298  		"fabric-contract-api": "^2.0.0",
   299  		"fabric-shim": "^2.0.0"
   300  ```
   301  
   302  The `package.json` file imports the Fabric contract class into the smart contract package. You can open `src/assetTransfer.ts` in a text editor to see the contract class imported into the smart contract and used to create the asset-transfer (basic) class. Also notice that the Asset class is imported from the type definition file `asset.ts`.
   303  
   304  ```
   305  import { Context, Contract } from 'fabric-contract-api';
   306  import { Asset } from './asset';
   307  
   308  export class AssetTransfer extends Contract {
   309  	...
   310  }
   311  
   312  ```
   313  
   314  The `AssetTransfer` class provides the transaction context for the functions defined within the smart contract that read and write data to the blockchain ledger.
   315  
   316  ```
   317   // CreateAsset issues a new asset to the world state with given details.
   318      public async CreateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) {
   319          const asset = {
   320              ID: id,
   321              Color: color,
   322              Size: size,
   323              Owner: owner,
   324              AppraisedValue: appraisedValue,
   325          };
   326  
   327          await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset)));
   328      }
   329  
   330  ```
   331  
   332  You can learn more about the JavaScript contract API by visiting the [API documentation](https://hyperledger.github.io/fabric-chaincode-node/{BRANCH}/api/) and the [smart contract processing topic](developapps/smartcontract.html).
   333  
   334  To install the smart contract dependencies, run the following command from the `asset-transfer-basic/chaincode-typescript` directory.
   335  
   336  ```
   337  npm install
   338  ```
   339  
   340  If the command is successful, the JavaScript packages will be installed inside a `node_modules` folder.
   341  
   342  Now that we that have our dependencies, we can create the chaincode package. Navigate back to our working directory in the `test-network` folder so that we can package the chaincode together with our other network artifacts.
   343  
   344  ```
   345  cd ../../test-network
   346  ```
   347  
   348  You can use the `peer` CLI to create a chaincode package in the required format. The `peer` binaries are located in the `bin` folder of the `fabric-samples` repository. Use the following command to add those binaries to your CLI Path:
   349  
   350  ```
   351  export PATH=${PWD}/../bin:$PATH
   352  ```
   353  
   354  You also need to set the `FABRIC_CFG_PATH` to point to the `core.yaml` file in the `fabric-samples` repository:
   355  
   356  ```
   357  export FABRIC_CFG_PATH=$PWD/../config/
   358  ```
   359  
   360  To confirm that you are able to use the `peer` CLI, check the version of the binaries. The binaries need to be version `2.0.0` or later to run this tutorial.
   361  
   362  ```
   363  peer version
   364  ```
   365  
   366  You can now create the chaincode package using the [peer lifecycle chaincode package](commands/peerlifecycle.html#peer-lifecycle-chaincode-package) command:
   367  
   368  ```
   369  peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-typescript/ --lang node --label basic_1.0
   370  ```
   371  
   372  This command will create a package named `basic.tar.gz` in your current directory. The `--lang` flag is used to specify the chaincode language and the `--path` flag provides the location of your smart contract code. The `--label` flag is used to specify a chaincode label that will identify your chaincode after it is installed. It is recommended that your label include the chaincode name and version.
   373  
   374  Now that we created the chaincode package, we can [install the chaincode](#install-the-chaincode-package) on the peers of the test network.
   375  
   376  ## Install the chaincode package
   377  
   378  After we package the asset-transfer (basic) smart contract, we can install the chaincode on our peers. The chaincode needs to be installed on every peer that will endorse a transaction. Because we are going to set the endorsement policy to require endorsements from both Org1 and Org2, we need to install the chaincode on the peers operated by both organizations:
   379  
   380  - peer0.org1.example.com
   381  - peer0.org2.example.com
   382  
   383  Let's install the chaincode on the Org1 peer first. Set the following environment variables to operate the `peer` CLI as the Org1 admin user. The `CORE_PEER_ADDRESS` will be set to point to the Org1 peer, `peer0.org1.example.com`.
   384  
   385  ```
   386  export CORE_PEER_TLS_ENABLED=true
   387  export CORE_PEER_LOCALMSPID="Org1MSP"
   388  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
   389  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
   390  export CORE_PEER_ADDRESS=localhost:7051
   391  ```
   392  
   393  Issue the [peer lifecycle chaincode install](commands/peerlifecycle.html#peer-lifecycle-chaincode-install) command to install the chaincode on the peer:
   394  
   395  ```
   396  peer lifecycle chaincode install basic.tar.gz
   397  ```
   398  
   399  If the command is successful, the peer will generate and return the package identifier. This package ID will be used to approve the chaincode in the next step. You should see output similar to the following:
   400  
   401  ```
   402  2020-07-16 10:09:57.534 CDT [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nJbasic_1.0:e2db7f693d4aa6156e652741d5606e9c5f0de9ebb88c5721cb8248c3aead8123\022\tbasic_1.0" >
   403  2020-07-16 10:09:57.534 CDT [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: basic_1.0:e2db7f693d4aa6156e652741d5606e9c5f0de9ebb88c5721cb8248c3aead8123
   404  ```
   405  
   406  We can now install the chaincode on the Org2 peer. Set the following environment variables to operate as the Org2 admin and target the Org2 peer, `peer0.org2.example.com`.
   407  
   408  ```
   409  export CORE_PEER_LOCALMSPID="Org2MSP"
   410  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
   411  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
   412  export CORE_PEER_ADDRESS=localhost:9051
   413  ```
   414  
   415  Issue the following command to install the chaincode:
   416  
   417  ```
   418  peer lifecycle chaincode install basic.tar.gz
   419  ```
   420  
   421  The chaincode is built by the peer when the chaincode is installed. The install command will return any build errors from the chaincode if there is a problem with the smart contract code.
   422  
   423  ## Approve a chaincode definition
   424  
   425  After you install the chaincode package, you need to approve a chaincode definition for your organization. The definition includes the important parameters of chaincode governance such as the name, version, and the chaincode endorsement policy.
   426  
   427  The set of channel members who need to approve a chaincode before it can be deployed is governed by the `/Channel/Application/LifecycleEndorsement` policy. By default, this policy requires that a majority of channel members need to approve a chaincode before it can be used on a channel. Because we have only two organizations on the channel, and a majority of 2 is 2, we need approve a chaincode definition of asset-transfer (basic) as Org1 and Org2.
   428  
   429  If an organization has installed the chaincode on their peer, they need to include the packageID in the chaincode definition approved by their organization. The package ID is used to associate the chaincode installed on a peer with an approved chaincode definition, and allows an organization to use the chaincode to endorse transactions. You can find the package ID of a chaincode by using the [peer lifecycle chaincode queryinstalled](commands/peerlifecycle.html#peer-lifecycle-chaincode-queryinstalled) command to query your peer.
   430  
   431  ```
   432  peer lifecycle chaincode queryinstalled
   433  ```
   434  
   435  The package ID is the combination of the chaincode label and a hash of the chaincode binaries. Every peer will generate the same package ID. You should see output similar to the following:
   436  
   437  ```
   438  Installed chaincodes on peer:
   439  Package ID: basic_1.0:69de748301770f6ef64b42aa6bb6cb291df20aa39542c3ef94008615704007f3, Label: basic_1.0
   440  ```
   441  
   442  We are going to use the package ID when we approve the chaincode, so let's go ahead and save it as an environment variable. Paste the package ID returned by `peer lifecycle chaincode queryinstalled` into the command below. **Note:** The package ID will not be the same for all users, so you need to complete this step using the package ID returned from your command window in the previous step.
   443  
   444  ```
   445  export CC_PACKAGE_ID=basic_1.0:69de748301770f6ef64b42aa6bb6cb291df20aa39542c3ef94008615704007f3
   446  ```
   447  
   448  Because the environment variables have been set to operate the `peer` CLI as the Org2 admin, we can approve the chaincode definition of asset-transfer (basic) as Org2. Chaincode is approved at the organization level, so the command only needs to target one peer. The approval is distributed to the other peers within the organization using gossip. Approve the chaincode definition using the [peer lifecycle chaincode approveformyorg](commands/peerlifecycle.html#peer-lifecycle-chaincode-approveformyorg) command:
   449  
   450  ```
   451  peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
   452  ```
   453  
   454  The command above uses the `--package-id` flag to include the package identifier in the chaincode definition. The `--sequence` parameter is an integer that keeps track of the number of times a chaincode has been defined or updated. Because the chaincode is being deployed to the channel for the first time, the sequence number is 1. When the asset-transfer (basic) chaincode is upgraded, the sequence number will be incremented to 2. If you are using the low level APIs provided by the Fabric Chaincode Shim API, you could pass the `--init-required` flag to the command above to request the execution of the Init function to initialize the chaincode. The first invoke of the chaincode would need to target the Init function and include the `--isInit` flag before you could use the other functions in the chaincode to interact with the ledger.
   455  
   456  We could have provided a `--signature-policy` or `--channel-config-policy` argument to the `approveformyorg` command to specify a chaincode endorsement policy. The endorsement policy specifies how many peers belonging to different channel members need to validate a transaction against a given chaincode. Because we did not set a policy, the definition of asset-transfer (basic) will use the default endorsement policy, which requires that a transaction be endorsed by a majority of channel members present when the transaction is submitted. This implies that if new organizations are added or removed from the channel, the endorsement policy
   457  is updated automatically to require more or fewer endorsements. In this tutorial, the default policy will require a majority of 2 out of 2 and transactions will need to be endorsed by a peer from Org1 and Org2. If you want to specify a custom endorsement policy, you can use the [Endorsement Policies](endorsement-policies.html) operations guide to learn about the policy syntax.
   458  
   459  You need to approve a chaincode definition with an identity that has an admin role. As a result, the `CORE_PEER_MSPCONFIGPATH` variable needs to point to the MSP folder that contains an admin identity. You cannot approve a chaincode definition with a client user. The approval needs to be submitted to the ordering service, which will validate the admin signature and then distribute the approval to your peers.
   460  
   461  We still need to approve the chaincode definition as Org1. Set the following environment variables to operate as the Org1 admin:
   462  
   463  ```
   464  export CORE_PEER_LOCALMSPID="Org1MSP"
   465  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
   466  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
   467  export CORE_PEER_ADDRESS=localhost:7051
   468  ```
   469  
   470  You can now approve the chaincode definition as Org1.
   471  
   472  ```
   473  peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
   474  ```
   475  
   476  We now have the majority we need to deploy the asset-transfer (basic) the chaincode to the channel. While only a majority of organizations need to approve a chaincode definition (with the default policies), all organizations need to approve a chaincode definition to start the chaincode on their peers. If you commit the definition before a channel member has approved the chaincode, the organization will not be able to endorse transactions. As a result, it is recommended that all channel members approve a chaincode before committing the chaincode definition.
   477  
   478  ## Committing the chaincode definition to the channel
   479  
   480  After a sufficient number of organizations have approved a chaincode definition, one organization can commit the chaincode definition to the channel. If a majority of channel members have approved the definition, the commit transaction will be successful and the parameters agreed to in the chaincode definition will be implemented on the channel.
   481  
   482  You can use the [peer lifecycle chaincode checkcommitreadiness](commands/peerlifecycle.html#peer-lifecycle-chaincode-checkcommitreadiness) command to check whether channel members have approved the same chaincode definition. The flags used for the `checkcommitreadiness` command are identical to the flags used to approve a chaincode for your organization. However, you do not need to include the `--package-id` flag.
   483  
   484  ```
   485  peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --output json
   486  ```
   487  
   488  The command will produce a JSON map that displays if a channel member has approved the parameters that were specified in the `checkcommitreadiness` command:
   489  
   490  ```json
   491  {
   492  	"Approvals": {
   493  		"Org1MSP": true,
   494  		"Org2MSP": true
   495  	}
   496  }
   497  ```
   498  
   499  Since both organizations that are members of the channel have approved the same parameters, the chaincode definition is ready to be committed to the channel. You can use the [peer lifecycle chaincode commit](commands/peerlifecycle.html#peer-lifecycle-chaincode-commit) command to commit the chaincode definition to the channel. The commit command also needs to be submitted by an organization admin.
   500  
   501  ```
   502  peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"
   503  ```
   504  
   505  The transaction above uses the `--peerAddresses` flag to target `peer0.org1.example.com` from Org1 and `peer0.org2.example.com` from Org2. The `commit` transaction is submitted to the peers joined to the channel to query the chaincode definition that was approved by the organization that operates the peer. The command needs to target the peers from a sufficient number of organizations to satisfy the policy for deploying a chaincode. Because the approval is distributed within each organization, you can target any peer that belongs to a channel member.
   506  
   507  The chaincode definition endorsements by channel members are submitted to the ordering service to be added to a block and distributed to the channel. The peers on the channel then validate whether a sufficient number of organizations have approved the chaincode definition. The `peer lifecycle chaincode commit` command will wait for the validations from the peer before returning a response.
   508  
   509  You can use the [peer lifecycle chaincode querycommitted](commands/peerlifecycle.html#peer-lifecycle-chaincode-querycommitted) command to confirm that the chaincode definition has been committed to the channel.
   510  
   511  ```
   512  peer lifecycle chaincode querycommitted --channelID mychannel --name basic --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
   513  ```
   514  
   515  If the chaincode was successful committed to the channel, the `querycommitted` command will return the sequence and version of the chaincode definition:
   516  
   517  ```
   518  Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
   519  Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
   520  ```
   521  
   522  ## Invoking the chaincode
   523  
   524  After the chaincode definition has been committed to a channel, the chaincode will start on the peers joined to the channel where the chaincode was installed. The asset-transfer (basic) chaincode is now ready to be invoked by client applications. Use the following command to create an initial set of assets on the ledger. Note that the invoke command needs to target a sufficient number of peers to meet the chaincode endorsement policy. (Note the CLI does not access the Fabric Gateway peer, so each endorsing peer must be specified.)
   525  
   526  ```
   527  peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"InitLedger","Args":[]}'
   528  ```
   529  
   530  If the command is successful, you should see a response similar to the following:
   531  
   532  ```
   533  2020-02-12 18:22:20.576 EST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
   534  ```
   535  
   536  We can use a query function to read the set of cars that were created by the chaincode:
   537  
   538  ```
   539  peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
   540  ```
   541  
   542  The response to the query should be the following list of assets:
   543  
   544  ```
   545  [{"Key":"asset1","Record":{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300}},
   546  {"Key":"asset2","Record":{"ID":"asset2","color":"red","size":5,"owner":"Brad","appraisedValue":400}},
   547  {"Key":"asset3","Record":{"ID":"asset3","color":"green","size":10,"owner":"Jin Soo","appraisedValue":500}},
   548  {"Key":"asset4","Record":{"ID":"asset4","color":"yellow","size":10,"owner":"Max","appraisedValue":600}},
   549  {"Key":"asset5","Record":{"ID":"asset5","color":"black","size":15,"owner":"Adriana","appraisedValue":700}},
   550  {"Key":"asset6","Record":{"ID":"asset6","color":"white","size":15,"owner":"Michel","appraisedValue":800}}]
   551  ```
   552  
   553  ## Upgrading a smart contract
   554  
   555  You can use the same Fabric chaincode lifecycle process to upgrade a chaincode that has already been deployed to a channel. Channel members can upgrade a chaincode by installing a new chaincode package and then approving a chaincode definition with the new package ID, a new chaincode version, and with the sequence number incremented by one. The new chaincode can be used after the chaincode definition is committed to the channel. This process allows channel members to coordinate on when a chaincode is upgraded, and ensure that a sufficient number of channel members are ready to use the new chaincode before it is deployed to the channel.
   556  
   557  Channel members can also use the upgrade process to change the chaincode endorsement policy. By approving a chaincode definition with a new endorsement policy and committing the chaincode definition to the channel, channel members can change the endorsement policy governing a chaincode without installing a new chaincode package.
   558  
   559  To provide a scenario for upgrading the asset-transfer (basic) chaincode that we just deployed, let's assume that Org1 and Org2 would like to install a version of the chaincode that is written in another language. They will use the Fabric chaincode lifecycle to update the chaincode version and ensure that both organizations have installed the new chaincode before it becomes active on the channel.
   560  
   561  We are going to assume that Org1 and Org2 initially installed the GO version of the asset-transfer (basic) chaincode, but would be more comfortable working with a chaincode written in JavaScript. The first step is to package the JavaScript version of the asset-transfer (basic) chaincode. If you used the JavaScript instructions to package your chaincode when you went through the tutorial, you can install new chaincode binaries by following the steps for packaging a chaincode written in [Go](#go) or [TypeScript](#typescript).
   562  
   563  Issue the following commands from the `test-network` directory to install the chaincode dependencies.
   564  
   565  ```
   566  cd ../asset-transfer-basic/chaincode-javascript
   567  npm install
   568  cd ../../test-network
   569  ```
   570  
   571  You can then issue the following commands to package the JavaScript chaincode from the `test-network` directory. We will set the environment variables needed to use the `peer` CLI again in case you closed your terminal.
   572  
   573  ```
   574  export PATH=${PWD}/../bin:$PATH
   575  export FABRIC_CFG_PATH=$PWD/../config/
   576  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
   577  peer lifecycle chaincode package basic_2.tar.gz --path ../asset-transfer-basic/chaincode-javascript/ --lang node --label basic_2.0
   578  ```
   579  
   580  Run the following commands to operate the `peer` CLI as the Org1 admin:
   581  
   582  ```
   583  export CORE_PEER_TLS_ENABLED=true
   584  export CORE_PEER_LOCALMSPID="Org1MSP"
   585  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
   586  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
   587  export CORE_PEER_ADDRESS=localhost:7051
   588  ```
   589  
   590  We can now use the following command to install the new chaincode package on the Org1 peer.
   591  
   592  ```
   593  peer lifecycle chaincode install basic_2.tar.gz
   594  ```
   595  
   596  The new chaincode package will create a new package ID. We can find the new package ID by querying our peer.
   597  
   598  ```
   599  peer lifecycle chaincode queryinstalled
   600  ```
   601  
   602  The `queryinstalled` command will return a list of the chaincode that have been installed on your peer similar to this output.
   603  
   604  ```
   605  Installed chaincodes on peer:
   606  Package ID: basic_1.0:69de748301770f6ef64b42aa6bb6cb291df20aa39542c3ef94008615704007f3, Label: basic_1.0
   607  Package ID: basic_2.0:1d559f9fb3dd879601ee17047658c7e0c84eab732dca7c841102f20e42a9e7d4, Label: basic_2.0
   608  ```
   609  
   610  You can use the package label to find the package ID of the new chaincode and save it as a new environment variable. This output is
   611  for example only -- your package ID will be different, so DO NOT COPY AND PASTE!
   612  
   613  ```
   614  export NEW_CC_PACKAGE_ID=basic_2.0:1d559f9fb3dd879601ee17047658c7e0c84eab732dca7c841102f20e42a9e7d4
   615  ```
   616  
   617  Org1 can now approve a new chaincode definition:
   618  
   619  ```
   620  peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
   621  ```
   622  
   623  The new chaincode definition uses the package ID of the JavaScript chaincode package and updates the chaincode version. Because the sequence parameter is used by the Fabric chaincode lifecycle to keep track of chaincode upgrades, Org1 also needs to increment the sequence number from 1 to 2. You can use the [peer lifecycle chaincode querycommitted](commands/peerlifecycle.html#peer-lifecycle-chaincode-querycommitted) command to find the sequence of the chaincode that was last committed to the channel.
   624  
   625  We now need to install the chaincode package and approve the chaincode definition as Org2 in order to upgrade the chaincode. Run the following commands to operate the `peer` CLI as the Org2 admin:
   626  
   627  ```
   628  export CORE_PEER_LOCALMSPID="Org2MSP"
   629  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
   630  export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
   631  export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
   632  export CORE_PEER_ADDRESS=localhost:9051
   633  ```
   634  
   635  We can now use the following command to install the new chaincode package on the Org2 peer.
   636  
   637  ```
   638  peer lifecycle chaincode install basic_2.tar.gz
   639  ```
   640  
   641  You can now approve the new chaincode definition for Org2.
   642  
   643  ```
   644  peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
   645  ```
   646  
   647  Use the [peer lifecycle chaincode checkcommitreadiness](commands/peerlifecycle.html#peer-lifecycle-chaincode-checkcommitreadiness) command to check if the chaincode definition with sequence 2 is ready to be committed to the channel:
   648  
   649  ```
   650  peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 2.0 --sequence 2 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --output json
   651  ```
   652  
   653  The chaincode is ready to be upgraded if the command returns the following JSON:
   654  
   655  ```json
   656  {
   657  	"Approvals": {
   658  		"Org1MSP": true,
   659  		"Org2MSP": true
   660  	}
   661  }
   662  ```
   663  
   664  The chaincode will be upgraded on the channel after the new chaincode definition is committed. Until then, the previous chaincode will continue to run on the peers of both organizations. Org2 can use the following command to upgrade the chaincode:
   665  
   666  ```
   667  peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --sequence 2 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"
   668  ```
   669  
   670  A successful commit transaction will start the new chaincode right away. If the chaincode definition changed the endorsement policy, the new policy would be put in effect.
   671  
   672  You can use the `docker ps` command to verify that the new chaincode has started on your peers:
   673  
   674  ```
   675  $ docker ps
   676  CONTAINER ID        IMAGE                                                                                                                                                                    COMMAND                  CREATED             STATUS              PORTS                              NAMES
   677  7bf2f1bf792b        dev-peer0.org1.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56bc8b3f9e66f46c-69c9e3e44ed18cafd1e58de37a70e2ec54cd49c7da0cd461fbd5e333de32879b   "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes                                           dev-peer0.org1.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56bc8b3f9e66f46c
   678  985e0967c27a        dev-peer0.org2.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56bc8b3f9e66f46c-158e9c6a4cb51dea043461fc4d3580e7df4c74a52b41e69a25705ce85405d760   "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes                                           dev-peer0.org2.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56bc8b3f9e66f46c
   679  31fdd19c3be7        hyperledger/fabric-peer:latest                                                                                                                                           "peer node start"        About an hour ago   Up About an hour    0.0.0.0:7051->7051/tcp             peer0.org1.example.com
   680  1b17ff866fe0        hyperledger/fabric-peer:latest                                                                                                                                           "peer node start"        About an hour ago   Up About an hour    7051/tcp, 0.0.0.0:9051->9051/tcp   peer0.org2.example.com
   681  4cf170c7ae9b        hyperledger/fabric-orderer:latest
   682  ```
   683  
   684  If you used the `--init-required` flag, you need to invoke the Init function before you can use the upgraded chaincode. Because we did not request the execution of Init, we can test our new JavaScript chaincode by creating a new car:
   685  
   686  ```
   687  peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"CreateAsset","Args":["asset8","blue","16","Kelley","750"]}'
   688  ```
   689  
   690  You can query all the cars on the ledger again to see the new car:
   691  
   692  ```
   693  peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
   694  ```
   695  
   696  You should see the following result from the JavaScript chaincode:
   697  
   698  ```
   699  [{"Key":"asset1","Record":{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300}},
   700  {"Key":"asset2","Record":{"ID":"asset2","color":"red","size":5,"owner":"Brad","appraisedValue":400}},
   701  {"Key":"asset3","Record":{"ID":"asset3","color":"green","size":10,"owner":"Jin Soo","appraisedValue":500}},
   702  {"Key":"asset4","Record":{"ID":"asset4","color":"yellow","size":10,"owner":"Max","appraisedValue":600}},
   703  {"Key":"asset5","Record":{"ID":"asset5","color":"black","size":15,"owner":"Adriana","appraisedValue":700}},
   704  {"Key":"asset6","Record":{"ID":"asset6","color":"white","size":15,"owner":"Michel","appraisedValue":800}},
   705  "Key":"asset8","Record":{"ID":"asset8","color":"blue","size":16,"owner":"Kelley","appraisedValue":750}}]
   706  ```
   707  
   708  ## Clean up
   709  
   710  When you are finished using the chaincode, you can also use the following commands to remove the Logspout tool.
   711  
   712  ```
   713  docker stop logspout
   714  docker rm logspout
   715  ```
   716  
   717  You can then bring down the test network by issuing the following command from the `test-network` directory:
   718  
   719  ```
   720  ./network.sh down
   721  ```
   722  
   723  ## Next steps
   724  
   725  After you write your smart contract and deploy it to a channel, you can use the APIs provided by the Fabric SDKs to invoke the smart contracts from a client application. This allows end users to interact with the assets on the blockchain ledger. To get started with the Fabric SDKs, see the [Running a Fabric Application tutorial](write_first_app.html).
   726  
   727  ## troubleshooting
   728  
   729  ### Chaincode not agreed to by this org
   730  
   731  **Problem:** When I try to commit a new chaincode definition to the channel, the `peer lifecycle chaincode commit` command fails with the following error:
   732  
   733  ```
   734  Error: failed to create signed transaction: proposal response was not successful, error code 500, msg failed to invoke backing implementation of 'CommitChaincodeDefinition': chaincode definition not agreed to by this org (Org1MSP)
   735  ```
   736  
   737  **Solution:** You can try to resolve this error by using the `peer lifecycle chaincode checkcommitreadiness` command to check which channel members have approved the chaincode definition that you are trying to commit. If any organization used a different value for any parameter of the chaincode definition, the commit transaction will fail. The `peer lifecycle chaincode checkcommitreadiness` will reveal which organizations did not approve the chaincode definition you are trying to commit:
   738  
   739  ```
   740  {
   741  	"approvals": {
   742  		"Org1MSP": false,
   743  		"Org2MSP": true
   744  	}
   745  }
   746  ```
   747  
   748  ### Invoke failure
   749  
   750  **Problem:** The `peer lifecycle chaincode commit` transaction is successful, but when I try to invoke the chaincode for the first time, it fails with the following error:
   751  
   752  ```
   753  Error: endorsement failure during invoke. response: status:500 message:"make sure the chaincode asset-transfer (basic) has been successfully defined on channel mychannel and try again: chaincode definition for 'asset-transfer (basic)' exists, but chaincode is not installed"
   754  ```
   755  
   756  **Solution:** You may not have set the correct `--package-id` when you approved your chaincode definition. As a result, the chaincode definition that was committed to the channel was not associated with the chaincode package you installed and the chaincode was not started on your peers. If you are running a docker based network, you can use the `docker ps` command to check if your chaincode is running:
   757  
   758  ```
   759  docker ps
   760  CONTAINER ID        IMAGE                               COMMAND             CREATED             STATUS              PORTS                              NAMES
   761  7fe1ae0a69fa        hyperledger/fabric-orderer:latest   "orderer"           5 minutes ago       Up 4 minutes        0.0.0.0:7050->7050/tcp             orderer.example.com
   762  2b9c684bd07e        hyperledger/fabric-peer:latest      "peer node start"   5 minutes ago       Up 4 minutes        0.0.0.0:7051->7051/tcp             peer0.org1.example.com
   763  39a3e41b2573        hyperledger/fabric-peer:latest      "peer node start"   5 minutes ago       Up 4 minutes        7051/tcp, 0.0.0.0:9051->9051/tcp   peer0.org2.example.com
   764  ```
   765  
   766  If you do not see any chaincode containers listed, use the `peer lifecycle chaincode approveformyorg` command approve a chaincode definition with the correct package ID.
   767  
   768  ## Endorsement policy failure
   769  
   770  **Problem:** When I try to commit the chaincode definition to the channel, the transaction fails with the following error:
   771  
   772  ```
   773  2020-04-07 20:08:23.306 EDT [chaincodeCmd] ClientWait -> INFO 001 txid [5f569e50ae58efa6261c4ad93180d49ac85ec29a07b58f576405b826a8213aeb] committed with status (ENDORSEMENT_POLICY_FAILURE) at localhost:7051
   774  Error: transaction invalidated with status (ENDORSEMENT_POLICY_FAILURE)
   775  ```
   776  
   777  **Solution:** This error is a result of the commit transaction not gathering enough endorsements to meet the Lifecycle endorsement policy. This problem could be a result of your transaction not targeting a sufficient number of peers to meet the policy. This could also be the result of some of the peer organizations not including the `Endorsement:` signature policy referenced by the default `/Channel/Application/Endorsement` policy in their `configtx.yaml` file:
   778  
   779  ```
   780  Readers:
   781  		Type: Signature
   782  		Rule: "OR('Org2MSP.admin', 'Org2MSP.peer', 'Org2MSP.client')"
   783  Writers:
   784  		Type: Signature
   785  		Rule: "OR('Org2MSP.admin', 'Org2MSP.client')"
   786  Admins:
   787  		Type: Signature
   788  		Rule: "OR('Org2MSP.admin')"
   789  Endorsement:
   790  		Type: Signature
   791  		Rule: "OR('Org2MSP.peer')"
   792  ```
   793  
   794  When you [enable the Fabric chaincode lifecycle](enable_cc_lifecycle.html), you also need to use the new Fabric 2.0 channel policies in addition to upgrading your channel to the `V2_0` capability. Your channel needs to include the new `/Channel/Application/LifecycleEndorsement` and `/Channel/Application/Endorsement` policies:
   795  
   796  ```
   797  Policies:
   798  		Readers:
   799  				Type: ImplicitMeta
   800  				Rule: "ANY Readers"
   801  		Writers:
   802  				Type: ImplicitMeta
   803  				Rule: "ANY Writers"
   804  		Admins:
   805  				Type: ImplicitMeta
   806  				Rule: "MAJORITY Admins"
   807  		LifecycleEndorsement:
   808  				Type: ImplicitMeta
   809  				Rule: "MAJORITY Endorsement"
   810  		Endorsement:
   811  				Type: ImplicitMeta
   812  				Rule: "MAJORITY Endorsement"
   813  ```
   814  
   815  If you do not include the new channel policies in the channel configuration, you will get the following error when you approve a chaincode definition for your organization:
   816  
   817  ```
   818  Error: proposal failed with status: 500 - failed to invoke backing implementation of 'ApproveChaincodeDefinitionForMyOrg': could not set defaults for chaincode definition in channel mychannel: policy '/Channel/Application/Endorsement' must be defined for channel 'mychannel' before chaincode operations can be attempted
   819  ```
   820  
   821  <!--- Licensed under Creative Commons Attribution 4.0 International License
   822  https://creativecommons.org/licenses/by/4.0/) -->