github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/docs/source/Setup/Chaincode-setup.rst (about)

     1  Writing, Building, and Running Chaincode in a Development Environment
     2  ---------------------------------------------------------------------
     3  
     4  Chaincode developers need a way to test and debug their chaincode
     5  without having to set up a complete peer network. By default, when you
     6  want to interact with chaincode, you need to first ``Deploy`` it using
     7  the CLI, REST API, gRPC API, or SDK. Upon receiving this request, the
     8  peer node would typically spin up a Docker container with the relevant
     9  chaincode. This can make things rather complicated for debugging
    10  chaincode under development, because of the turnaround time with the
    11  ``launch chaincode - debug docker container - fix problem - launch chaincode - lather - rinse - repeat``
    12  cycle. As such, the fabric peer has a ``--peer-chaincodedev`` flag that
    13  can be passed on start-up to instruct the peer node not to deploy the
    14  chaincode as a Docker container.
    15  
    16  The following instructions apply to *developing* chaincode in Go or
    17  Java. They do not apply to running in a production environment. However,
    18  if *developing* chaincode in Java, please see the `Java chaincode
    19  setup <https://github.com/hyperledger/fabric/blob/master/docs/Setup/JAVAChaincode.md>`__
    20  instructions first, to be sure your environment is properly configured.
    21  
    22  **Note:** We have added support for `System
    23  chaincode <https://github.com/hyperledger/fabric/blob/master/docs/SystemChaincode-noop.md>`__.
    24  
    25  Choices
    26  -------
    27  
    28  Once again, you have the choice of using one of the following
    29  approaches:
    30  
    31  -  `Option 1 <#option-1-vagrant-development-environment>`__ using the
    32     **Vagrant** `development
    33     environment <https://github.com/hyperledger/fabric/blob/master/docs/dev-setup/devenv.md>`__
    34     that is used for developing the fabric itself
    35  -  `Option 2 <#option-2-docker-for-mac-or-windows>`__ using Docker for
    36     Mac or Windows
    37  -  `Option 3 <#option-3-docker-toolbox>`__ using Docker toolbox
    38  
    39  By using options *2* or *3*, above, you avoid having to build everything
    40  from scratch, and there's no need to keep a clone of the fabric GitHub
    41  repos current/up-to-date. Instead, you can simply pull and run the
    42  ``fabric-peer`` and ``fabric-membersrvc`` images directly from
    43  DockerHub.
    44  
    45  You will need multiple terminal windows - essentially one for each
    46  component. One runs the validating peer; another runs the chaincode; the
    47  third runs the CLI or REST API commands to execute transactions.
    48  Finally, when running with security enabled, an additional fourth window
    49  is required to run the **Certificate Authority (CA)** server. Detailed
    50  instructions are provided in the sections below.
    51  
    52  Option 1 Vagrant development environment
    53  ----------------------------------------
    54  
    55  Security Setup (optional)
    56  ~~~~~~~~~~~~~~~~~~~~~~~~~
    57  
    58  From the ``devenv`` subdirectory of your fabric workspace environment,
    59  ``ssh`` into Vagrant:
    60  
    61  ::
    62  
    63      cd $GOPATH/src/github.com/hyperledger/fabric/devenv
    64      vagrant ssh
    65  
    66  To set up the local development environment with security enabled, you
    67  must first build and run the **Certificate Authority (CA)** server:
    68  
    69  ::
    70  
    71      cd $GOPATH/src/github.com/hyperledger/fabric
    72      make membersrvc && membersrvc
    73  
    74  Running the above commands builds and runs the CA server with the
    75  default setup, which is defined in the
    76  `membersrvc.yaml <https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml>`__
    77  configuration file. The default configuration includes multiple users
    78  who are already registered with the CA; these users are listed in the
    79  ``eca.users`` section of the configuration file. To register additional
    80  users with the CA for testing, modify the ``eca.users`` section of the
    81  `membersrvc.yaml <https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml>`__
    82  file to include additional ``enrollmentID`` and ``enrollmentPW`` pairs.
    83  Note the integer that precedes the ``enrollmentPW``. That integer
    84  indicates the role of the user, where 1 = client, 2 = non-validating
    85  peer, 4 = validating peer, and 8 = auditor.
    86  
    87  Running the validating peer
    88  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    89  
    90  In a **new** terminal window, from the ``devenv`` subdirectory of your
    91  fabric workspace environment, ``ssh`` into Vagrant:
    92  
    93  ::
    94  
    95      cd $GOPATH/src/github.com/hyperledger/fabric/devenv
    96      vagrant ssh
    97  
    98  Build and run the peer process.
    99  
   100  ::
   101  
   102      cd $GOPATH/src/github.com/hyperledger/fabric
   103      make peer
   104      peer node start --peer-chaincodedev
   105  
   106  Alternatively, rather than tweaking the ``core.yaml`` and rebuilding,
   107  you can enable security and privacy on the peer with environment
   108  variables:
   109  
   110  ::
   111  
   112      CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer node start --peer-chaincodedev
   113  
   114  Now, you are ready to start `running the
   115  chaincode <#running-the-chaincode>`__.
   116  
   117  Option 2 Docker for Mac or Windows
   118  ----------------------------------
   119  
   120  If you would prefer to simply run the fabric components as built and
   121  published by the Hyperledger project on your Mac or Windows
   122  laptop/server using the Docker for
   123  `Mac <https://docs.docker.com/engine/installation/mac/>`__ or
   124  `Windows <https://docs.docker.com/engine/installation/windows/>`__
   125  platform, following these steps. If using `Docker
   126  Toolbox <https://docs.docker.com/toolbox/overview/>`__, please skip to
   127  `Option 3 <#option-3-docker-toolbox>`__, below.
   128  
   129  Pull images from DockerHub
   130  ~~~~~~~~~~~~~~~~~~~~~~~~~~
   131  
   132  First, pull the latest images published by the Hyperledger fabric
   133  project from DockerHub.
   134  
   135  ::
   136  
   137      docker pull hyperledger/fabric-peer:latest
   138      docker pull hyperledger/fabric-membersrvc:latest
   139  
   140  Running the Peer and CA
   141  ~~~~~~~~~~~~~~~~~~~~~~~
   142  
   143  To run the fabric-peer and fabric-membersrvc images, we'll use `Docker
   144  Compose <https://docs.docker.com/compose/>`__. It significantly
   145  simplifies things. To do that, we'll create a docker-compose.yml file
   146  with a description of the two services we'll be running. Here's the
   147  docker-compose.yml to launch the two processes:
   148  
   149  ::
   150  
   151      membersrvc:
   152        image: hyperledger/fabric-membersrvc
   153        ports:
   154          - "7054:7054"
   155        command: membersrvc
   156      vp0:
   157        image: hyperledger/fabric-peer
   158        ports:
   159          - "7050:7050"
   160          - "7051:7051"
   161          - "7053:7053"
   162        environment:
   163          - CORE_PEER_ADDRESSAUTODETECT=true
   164          - CORE_VM_ENDPOINT=unix:///var/run/docker.sock
   165          - CORE_LOGGING_LEVEL=DEBUG
   166          - CORE_PEER_ID=vp0
   167          - CORE_PEER_PKI_ECA_PADDR=membersrvc:7054
   168          - CORE_PEER_PKI_TCA_PADDR=membersrvc:7054
   169          - CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054
   170          - CORE_SECURITY_ENABLED=true
   171          - CORE_SECURITY_ENROLLID=test_vp0
   172          - CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT
   173        links:
   174          - membersrvc
   175        command: sh -c "sleep 5; peer node start --peer-chaincodedev"
   176  
   177  Save that in a directory with the name ``docker-compose.yml``. Then, run
   178  ``docker-compose up`` to start the two processes.
   179  
   180  Now, you are ready to start `running the
   181  chaincode <#running-the-chaincode>`__.
   182  
   183  Option 3 Docker Toolbox
   184  -----------------------
   185  
   186  If you are using `Docker
   187  Toolbox <https://docs.docker.com/toolbox/overview/>`__, please follow
   188  these instructions.
   189  
   190  Pull images from DockerHub
   191  ~~~~~~~~~~~~~~~~~~~~~~~~~~
   192  
   193  First, pull the latest images published by the Hyperledger fabric
   194  project from DockerHub.
   195  
   196  ::
   197  
   198        docker pull hyperledger/fabric-peer:latest
   199        docker pull hyperledger/fabric-membersrvc:latest
   200  
   201  Running the Peer and CA
   202  ~~~~~~~~~~~~~~~~~~~~~~~
   203  
   204  To run the fabric-peer and fabric-membersrvc images, we'll use `Docker
   205  Compose <https://docs.docker.com/compose/>`__. It significantly
   206  simplifies things. To do that, we'll create a docker-compose.yml file
   207  with a description of the two services we'll be running. Here's the
   208  docker-compose.yml to launch the two processes:
   209  
   210  ::
   211  
   212      membersrvc:
   213        image: hyperledger/fabric-membersrvc
   214        command: membersrvc
   215      vp0:
   216        image: hyperledger/fabric-peer
   217        environment:
   218          - CORE_PEER_ADDRESSAUTODETECT=true
   219          - CORE_VM_ENDPOINT=http://172.17.0.1:2375
   220          - CORE_LOGGING_LEVEL=DEBUG
   221          - CORE_PEER_ID=vp0
   222          - CORE_PEER_PKI_ECA_PADDR=membersrvc:7054
   223          - CORE_PEER_PKI_TCA_PADDR=membersrvc:7054
   224          - CORE_PEER_PKI_TLSCA_PADDR=membersrvc:7054
   225          - CORE_SECURITY_ENABLED=true
   226          - CORE_SECURITY_ENROLLID=test_vp0
   227          - CORE_SECURITY_ENROLLSECRET=MwYpmSRjupbT
   228        links:
   229          - membersrvc
   230        command: sh -c "sleep 5; peer node start --peer-chaincodedev"
   231  
   232  Save that in a directory with the name ``docker-compose.yml``. Then, run
   233  ``docker-compose up`` to start the two processes.
   234  
   235  Running the chaincode
   236  ---------------------
   237  
   238  Start a **new** terminal window.
   239  
   240  Vagrant
   241  ~~~~~~~
   242  
   243  If you are using `Option
   244  1 <#option-1-vagrant-development-environment>`__, you'll need to ``ssh``
   245  to Vagrant. Otherwise, `skip <#not-vagrant>`__ this step.
   246  
   247  ::
   248  
   249      cd $GOPATH/src/github.com/hyperledger/fabric/devenv
   250      vagrant ssh
   251  
   252  Next, we'll build the **chaincode\_example02** code, which is provided
   253  in the Hyperledger fabric source code repository. If you are using
   254  `Option 1 <#option-1-vagrant-development-environment>`__, then you can
   255  do this from your clone of the fabric repository.
   256  
   257  ::
   258  
   259      cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
   260      go build
   261  
   262  Not Vagrant
   263  ~~~~~~~~~~~
   264  
   265  If you are using either `Option
   266  2 <#option-2-docker-for-mac-or-windows>`__ or `Option
   267  3 <#option-3-docker-toolbox>`__, you'll need to download the sample
   268  chaincode. The chaincode project must be placed somewhere under the
   269  ``src`` directory in your local ``$GOPATH`` as shown below.
   270  
   271  ::
   272  
   273      mkdir -p $GOPATH/src/github.com/chaincode_example02/
   274      cd $GOPATH/src/github.com/chaincode_example02
   275      curl --request GET https://raw.githubusercontent.com/hyperledger/fabric/master/examples/chaincode/go/chaincode_example02/chaincode_example02.go > chaincode_example02.go
   276  
   277  Next, you'll need to clone the Hyperledger fabric to your local $GOPATH,
   278  so that you can build your chaincode. **Note:** this is a temporary
   279  stop-gap until we can provide an independent package for the chaincode
   280  shim.
   281  
   282  ::
   283  
   284      mkdir -p $GOPATH/src/github.com/hyperledger
   285      cd $GOPATH/src/github.com/hyperledger
   286      git clone http://gerrit.hyperledger.org/r/fabric
   287  
   288  Now, you should be able to build your chaincode.
   289  
   290  ::
   291  
   292      cd $GOPATH/src/github.com/chaincode_example02
   293      go build
   294  
   295  When you are ready to start creating your own Go chaincode, create a new
   296  subdirectory under $GOPATH/src. You can copy the
   297  **chaincode\_example02** file to the new directory and modify it.
   298  
   299  Starting and registering the chaincode
   300  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   301  
   302  Run the following chaincode command to start and register the chaincode
   303  with the validating peer:
   304  
   305  ::
   306  
   307      CORE_CHAINCODE_ID_NAME=mycc CORE_PEER_ADDRESS=0.0.0.0:7051 ./chaincode_example02
   308  
   309  The chaincode console will display the message "Received REGISTERED,
   310  ready for invocations", which indicates that the chaincode is ready to
   311  receive requests. Follow the steps below to send a chaincode deploy,
   312  invoke or query transaction. If the "Received REGISTERED" message is not
   313  displayed, then an error has occurred during the deployment; revisit the
   314  previous steps to resolve the issue.
   315  
   316  Running the CLI or REST API
   317  ---------------------------
   318  
   319  -  `chaincode deploy via CLI and
   320     REST <#chaincode-deploy-via-cli-and-rest>`__
   321  -  `chaincode invoke via CLI and
   322     REST <#chaincode-invoke-via-cli-and-rest>`__
   323  -  `chaincode query via CLI and
   324     REST <#chaincode-query-via-cli-and-rest>`__
   325  
   326  If you were running with security enabled, see `Removing temporary files
   327  when security is
   328  enabled <#removing-temporary-files-when-security-is-enabled>`__ to learn
   329  how to clean up the temporary files.
   330  
   331  See the `logging
   332  control <https://github.com/hyperledger/fabric/blob/master/docs/Setup/logging-control.md>`__
   333  reference for information on controlling logging output from the
   334  ``peer`` and chaincodes.
   335  
   336  Terminal 3 (CLI or REST API)
   337  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   338  
   339  **Note on REST API port**
   340  ^^^^^^^^^^^^^^^^^^^^^^^^^
   341  
   342  The default REST interface port is ``7050``. It can be configured in
   343  `core.yaml <https://github.com/hyperledger/fabric/blob/master/sampleconfig/core.yaml>`__
   344  using the ``rest.address`` property. If using Vagrant, the REST port
   345  mapping is defined in
   346  `Vagrantfile <https://github.com/hyperledger/fabric/blob/master/devenv/Vagrantfile>`__.
   347  
   348  **Note on security functionality**
   349  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   350  
   351  Current security implementation assumes that end user authentication
   352  takes place at the application layer and is not handled by the fabric.
   353  Authentication may be performed through any means considered appropriate
   354  for the target application. Upon successful user authentication, the
   355  application will perform user registration with the CA exactly once. If
   356  registration is attempted a second time for the same user, an error will
   357  result. During registration, the application sends a request to the
   358  certificate authority to verify the user registration and if successful,
   359  the CA responds with the user certificates and keys. The enrollment and
   360  transaction certificates received from the CA will be stored locally
   361  inside ``/var/hyperledger/production/crypto/client/`` directory. This
   362  directory resides on a specific peer node which allows the user to
   363  transact only through this specific peer while using the stored crypto
   364  material. If the end user needs to perform transactions through more
   365  then one peer node, the application is responsible for replicating the
   366  crypto material to other peer nodes. This is necessary as registering a
   367  given user with the CA a second time will fail.
   368  
   369  With security enabled, the CLI commands and REST payloads must be
   370  modified to include the ``enrollmentID`` of a registered user who is
   371  logged in; otherwise an error will result. A registered user can be
   372  logged in through the CLI or the REST API by following the instructions
   373  below. To log in through the CLI, issue the following commands, where
   374  ``username`` is one of the ``enrollmentID`` values listed in the
   375  ``eca.users`` section of the
   376  `membersrvc.yaml <https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml>`__
   377  file.
   378  
   379  From your command line terminal, move to the ``devenv`` subdirectory of
   380  your workspace environment. Log into a Vagrant terminal by executing the
   381  following command:
   382  
   383  ::
   384  
   385          vagrant ssh
   386  
   387  Register the user though the CLI, substituting for ``<username>``
   388  appropriately:
   389  
   390  ::
   391  
   392          cd $GOPATH/src/github.com/hyperledger/fabric/peer
   393          peer network login <username>
   394  
   395  The command will prompt for a password, which must match the
   396  ``enrollmentPW`` listed for the target user in the ``eca.users`` section
   397  of the
   398  `membersrvc.yaml <https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml>`__
   399  file. If the password entered does not match the ``enrollmentPW``, an
   400  error will result.
   401  
   402  To log in through the REST API, send a POST request to the
   403  ``/registrar`` endpoint, containing the ``enrollmentID`` and
   404  ``enrollmentPW`` listed in the ``eca.users`` section of the
   405  `membersrvc.yaml <https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml>`__
   406  file.
   407  
   408  **REST Request:**
   409  
   410  ::
   411  
   412      POST localhost:7050/registrar
   413  
   414      {
   415        "enrollId": "jim",
   416        "enrollSecret": "6avZQLwcUe9b"
   417      }
   418  
   419  **REST Response:**
   420  
   421  ::
   422  
   423      200 OK
   424      {
   425          "OK": "Login successful for user 'jim'."
   426      }
   427  
   428  chaincode deploy via CLI and REST
   429  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   430  
   431  First, send a chaincode deploy transaction, only once, to the validating
   432  peer. The CLI connects to the validating peer using the properties
   433  defined in the core.yaml file. **Note:** The deploy transaction
   434  typically requires a ``path`` parameter to locate, build, and deploy the
   435  chaincode. However, because these instructions are specific to local
   436  development mode and the chaincode is deployed manually, the ``name``
   437  parameter is used instead.
   438  
   439  ::
   440  
   441      peer chaincode deploy -n mycc -c '{"Args": ["init", "a","100", "b", "200"]}'
   442  
   443  Alternatively, you can run the chaincode deploy transaction through the
   444  REST API.
   445  
   446  **REST Request:**
   447  
   448  ::
   449  
   450      POST <host:port>/chaincode
   451  
   452      {
   453        "jsonrpc": "2.0",
   454        "method": "deploy",
   455        "params": {
   456          "type": 1,
   457          "chaincodeID":{
   458              "name": "mycc"
   459          },
   460          "input": {
   461              "args":["init", "a", "100", "b", "200"]
   462          }
   463        },
   464        "id": 1
   465      }
   466  
   467  **REST Response:**
   468  
   469  ::
   470  
   471      {
   472          "jsonrpc": "2.0",
   473          "result": {
   474              "status": "OK",
   475              "message": "mycc"
   476          },
   477          "id": 1
   478      }
   479  
   480  **Note:** When security is enabled, modify the CLI command and the REST
   481  API payload to pass the ``enrollmentID`` of a logged in user. To log in
   482  a registered user through the CLI or the REST API, follow the
   483  instructions in the `note on security
   484  functionality <#note-on-security-functionality>`__. On the CLI, the
   485  ``enrollmentID`` is passed with the ``-u`` parameter; in the REST API,
   486  the ``enrollmentID`` is passed with the ``secureContext`` element. If
   487  you are enabling security and privacy on the peer process with
   488  environment variables, it is important to include these environment
   489  variables in the command when executing all subsequent peer operations
   490  (e.g. deploy, invoke, or query).
   491  
   492  ::
   493  
   494        CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode deploy -u jim -n mycc -c '{"Args": ["init", "a","100", "b", "200"]}'
   495  
   496  **REST Request:**
   497  
   498  ::
   499  
   500      POST <host:port>/chaincode
   501  
   502      {
   503        "jsonrpc": "2.0",
   504        "method": "deploy",
   505        "params": {
   506          "type": 1,
   507          "chaincodeID":{
   508              "name": "mycc"
   509          },
   510          "input": {
   511              "args":["init", "a", "100", "b", "200"]
   512          },
   513          "secureContext": "jim"
   514        },
   515        "id": 1
   516      }
   517  
   518  The deploy transaction initializes the chaincode by executing a target
   519  initializing function. Though the example shows "init", the name could
   520  be arbitrarily chosen by the chaincode developer. You should see the
   521  following output in the chaincode window:
   522  
   523  ::
   524  
   525          <TIMESTAMP_SIGNATURE> Received INIT(uuid:005dea42-d57f-4983-803e-3232e551bf61), initializing chaincode
   526          Aval = 100, Bval = 200
   527  
   528  Chaincode invoke via CLI and REST
   529  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   530  
   531  Run the chaincode invoking transaction on the CLI as many times as
   532  desired. The ``-n`` argument should match the value provided in the
   533  chaincode window (started in Vagrant terminal 2):
   534  
   535  ::
   536  
   537          peer chaincode invoke -l golang -n mycc -c '{"Args": ["invoke", "a", "b", "10"]}'
   538  
   539  Alternatively, run the chaincode invoking transaction through the REST
   540  API.
   541  
   542  **REST Request:**
   543  
   544  ::
   545  
   546      POST <host:port>/chaincode
   547  
   548      {
   549        "jsonrpc": "2.0",
   550        "method": "invoke",
   551        "params": {
   552            "type": 1,
   553            "chaincodeID":{
   554                "name":"mycc"
   555            },
   556            "input": {
   557               "args":["invoke", "a", "b", "10"]
   558            }
   559        },
   560        "id": 3
   561      }
   562  
   563  **REST Response:**
   564  
   565  ::
   566  
   567      {
   568          "jsonrpc": "2.0",
   569          "result": {
   570              "status": "OK",
   571              "message": "5a4540e5-902b-422d-a6ab-e70ab36a2e6d"
   572          },
   573          "id": 3
   574      }
   575  
   576  **Note:** When security is enabled, modify the CLI command and REST API
   577  payload to pass the ``enrollmentID`` of a logged in user. To log in a
   578  registered user through the CLI or the REST API, follow the instructions
   579  in the `note on security
   580  functionality <#note-on-security-functionality>`__. On the CLI, the
   581  ``enrollmentID`` is passed with the ``-u`` parameter; in the REST API,
   582  the ``enrollmentID`` is passed with the ``secureContext`` element. If
   583  you are enabling security and privacy on the peer process with
   584  environment variables, it is important to include these environment
   585  variables in the command when executing all subsequent peer operations
   586  (e.g. deploy, invoke, or query).
   587  
   588  ::
   589  
   590        CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode invoke -u jim -l golang -n mycc -c '{"Args": ["invoke", "a", "b", "10"]}'
   591  
   592  **REST Request:**
   593  
   594  ::
   595  
   596      POST <host:port>/chaincode
   597  
   598      {
   599        "jsonrpc": "2.0",
   600        "method": "invoke",
   601        "params": {
   602            "type": 1,
   603            "chaincodeID":{
   604                "name":"mycc"
   605            },
   606            "input": {
   607               "args":["invoke", "a", "b", "10"]
   608            },
   609            "secureContext": "jim"
   610        },
   611        "id": 3
   612      }
   613  
   614  The invoking transaction runs the specified chaincode function name
   615  "invoke" with the arguments. This transaction transfers 10 units from A
   616  to B. You should see the following output in the chaincode window:
   617  
   618  ::
   619  
   620          <TIMESTAMP_SIGNATURE> Received RESPONSE. Payload 200, Uuid 075d72a4-4d1f-4a1d-a735-4f6f60d597a9
   621          Aval = 90, Bval = 210
   622  
   623  Chaincode query via CLI and REST
   624  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   625  
   626  Run a query on the chaincode to retrieve the desired values. The ``-n``
   627  argument should match the value provided in the chaincode window
   628  (started in Vagrant terminal 2):
   629  
   630  ::
   631  
   632          peer chaincode query -l golang -n mycc -c '{"Args": ["query", "b"]}'
   633  
   634  The response should be similar to the following:
   635  
   636  ::
   637  
   638          {"Name":"b","Amount":"210"}
   639  
   640  If a name other than "a" or "b" is provided in a query sent to
   641  ``chaincode_example02``, you should see an error response similar to the
   642  following:
   643  
   644  ::
   645  
   646          {"Error":"Nil amount for c"}
   647  
   648  Alternatively, run the chaincode query transaction through the REST API.
   649  
   650  **REST Request:**
   651  
   652  ::
   653  
   654      POST <host:port>/chaincode
   655  
   656      {
   657        "jsonrpc": "2.0",
   658        "method": "query",
   659        "params": {
   660            "type": 1,
   661            "chaincodeID":{
   662                "name":"mycc"
   663            },
   664            "input": {
   665               "args":["query", "a"]
   666            }
   667        },
   668        "id": 5
   669      }
   670  
   671  **REST Response:**
   672  
   673  ::
   674  
   675      {
   676          "jsonrpc": "2.0",
   677          "result": {
   678              "status": "OK",
   679              "message": "90"
   680          },
   681          "id": 5
   682      }
   683  
   684  **Note:** When security is enabled, modify the CLI command and REST API
   685  payload to pass the ``enrollmentID`` of a logged in user. To log in a
   686  registered user through the CLI or the REST API, follow the instructions
   687  in the `note on security
   688  functionality <#note-on-security-functionality>`__. On the CLI, the
   689  ``enrollmentID`` is passed with the ``-u`` parameter; in the REST API,
   690  the ``enrollmentID`` is passed with the ``secureContext`` element. If
   691  you are enabling security and privacy on the peer process with
   692  environment variables, it is important to include these environment
   693  variables in the command when executing all subsequent peer operations
   694  (e.g. deploy, invoke, or query).
   695  
   696  ::
   697  
   698            CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode query -u jim -l golang -n mycc -c '{"Args": ["query", "b"]}'
   699  
   700  **REST Request:**
   701  
   702  ::
   703  
   704      POST <host:port>/chaincode
   705  
   706      {
   707        "jsonrpc": "2.0",
   708        "method": "query",
   709        "params": {
   710            "type": 1,
   711            "chaincodeID":{
   712                "name":"mycc"
   713            },
   714            "input": {
   715               "args":["query", "a"]
   716            },
   717            "secureContext": "jim"
   718        },
   719        "id": 5
   720      }
   721  
   722  Removing temporary files when security is enabled
   723  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   724  
   725  **Note:** this step applies **ONLY** if you were using Option 1 above.
   726  For Option 2 or 3, the cleanup is handled by Docker.
   727  
   728  After the completion of a chaincode test with security enabled, remove
   729  the temporary files that were created by the CA server process. To
   730  remove the client enrollment certificate, enrollment key, transaction
   731  certificate chain, etc., run the following commands. Note, that you must
   732  run these commands if you want to register a user who has already been
   733  registered previously.
   734  
   735  From your command line terminal, ``ssh`` into Vagrant:
   736  
   737  ::
   738  
   739      cd $GOPATH/src/github.com/hyperledger/fabric/devenv
   740      vagrant ssh
   741  
   742  And then run:
   743  
   744  ::
   745  
   746      rm -rf /var/hyperledger/production