fabric-samples/chaincode-docker-devmode
jchen8tw 044e5b78ef new README and new chaincode Lifecycle
Signed-off-by: jchen8tw <11806483+jchen8tw@users.noreply.github.com>
2020-08-13 21:04:51 +08:00
..
msp new README and new chaincode Lifecycle 2020-08-13 21:04:51 +08:00
.gitignore [FAB-13862] Rename example02 ABstore 2019-01-30 09:02:23 +01:00
docker-compose-simple.yaml new README and new chaincode Lifecycle 2020-08-13 21:04:51 +08:00
installChaincode.sh new README and new chaincode Lifecycle 2020-08-13 21:04:51 +08:00
myc.tx new README and new chaincode Lifecycle 2020-08-13 21:04:51 +08:00
orderer.block new README and new chaincode Lifecycle 2020-08-13 21:04:51 +08:00
README.rst new README and new chaincode Lifecycle 2020-08-13 21:04:51 +08:00
script.sh [FAB-4371] - Chaincode Dev Mode 2017-06-23 13:00:49 -04:00

Using dev mode
==============

Normally chaincodes are started and maintained by peer. However in “dev
mode", chaincode is built and started by the user. This mode is useful
during chaincode development phase for rapid code/build/run/debug cycle
turnaround.

We start "dev mode" by leveraging pre-generated orderer and channel artifacts for
a sample dev network.  As such, the user can immediately jump into the process
of compiling chaincode and driving calls.

Install Fabric Samples
----------------------

If you haven't already done so, please `install samples <http://hyperledger-fabric.readthedocs.io/en/latest/install.html>`_.

Navigate to the ``chaincode-docker-devmode`` directory of the ``fabric-samples``
clone:

.. code:: bash

  cd chaincode-docker-devmode

Download docker images
^^^^^^^^^^^^^^^^^^^^^^

We need four docker images in order for "dev mode" to run against the supplied
docker compose script.  If you installed the ``fabric-samples`` repo clone and
followed the instructions to `install samples, binaries and docker images <http://hyperledger-fabric.readthedocs.io/en/latest/install.html>`_, then
you should have the necessary Docker images installed locally.

.. note:: If you choose to manually pull the images then you must retag them as
          ``latest``.

Issue a ``docker images`` command to reveal your local Docker Registry.  You
should see something similar to following:

.. code:: bash

  docker images
  REPOSITORY                     TAG                                  IMAGE ID            CREATED             SIZE
  hyperledger/fabric-tools       latest                c584c20ac82b        9 days ago         1.42 GB
  hyperledger/fabric-tools       x86_64-1.1.0-preview  c584c20ac82b        9 days ago         1.42 GB
  hyperledger/fabric-orderer     latest                2fccc91736df        9 days ago         159 MB
  hyperledger/fabric-orderer     x86_64-1.1.0-preview  2fccc91736df        9 dyas ago         159 MB
  hyperledger/fabric-peer        latest                337f3d90b452        9 days ago         165 MB
  hyperledger/fabric-peer        x86_64-1.1.0-preview  337f3d90b452        9 days ago         165 MB
  hyperledger/fabric-ccenv       latest                82489d1c11e8        9 days ago         1.35 GB
  hyperledger/fabric-ccenv       x86_64-1.1.0-preview  82489d1c11e8        9 days ago         1.35 GB

.. note:: If you retrieved the images through the `install samples, binaries and docker images <http://hyperledger-fabric.readthedocs.io/en/latest/install.html>`_,
          then you will see additional images listed.  However, we are only concerned with
          these four.

Now open three terminals and navigate to your ``chaincode-docker-devmode``
directory in each.

Terminal 1 - Start the network
------------------------------

.. code:: bash

    docker-compose -f docker-compose-simple.yaml up

The above starts the network with the ``SingleSampleMSPSolo`` orderer profile and
launches the peer in "dev mode".  It also launches two additional containers -
one for the chaincode environment and a CLI to interact with the chaincode.  The
commands for create and join channel are embedded in the CLI container, so we
can jump immediately to the chaincode calls.

.. note:: TLS is not enabled as it is not supported when running chaincode in dev mode.

Terminal 2 - Install the chaincode
----------------------------------------

Even though you are in ``--peer-chaincodedev`` mode, you still have to install the
chaincode so the life-cycle system chaincode can go through its checks normally.
This requirement may be removed in future when in ``--peer-chaincodedev`` mode.

We'll leverage the CLI container to drive these calls.

.. code:: bash

    docker exec -it cli bash

.. code:: bash

    ./installChaincode.sh abstore.tar.gz abstore 1 0

This will package the chaincode to ``abstore.tar.gz`` in the chaincode directory plus
approve and commit the chaincode definition for you.

.. code:: bash
  
    ========= CHAINCODE ID ===================================
    abstore:377daf05b5f66815cc0bce7fd285dd9812c06243a2242c745c02e8962c1dcc6d
    Use this chaincode id to start chaincode container

After the new chaincode has installed you will then see the output of chaincode id.


Terminal 3 - Build & start the chaincode
------------------------------

.. code:: bash

    docker exec -it chaincode sh

You should see the following:

.. code:: sh

    /opt/gopath/src/chaincode $

Now, compile your chaincode:

.. code:: sh

    cd abstore/go
    go build -o abstore

Now run the chaincode with the chaincode id previosly given:

.. code:: sh

    CORE_CHAINCODE_ID_NAME=abstore:377daf05b5f66815cc0bce7fd285dd9812c06243a2242c745c02e8962c1dcc6d CORE_PEER_TLS_ENABLED=false ./abstore -peer.address peer:7052

The chaincode is started with peer and chaincode logs indicating successful registration with the peer.

Back to Terminal 2 - Invoke the chaincode
----------------------------------------

.. code:: bash

    peer chaincode invoke -o orderer:7050 -C myc -n abstore -c '{"Args":["init","a","100","b","200"]}'

Now issue an invoke to move ``10`` from ``a`` to ``b``.

.. code:: bash

    peer chaincode invoke -o orderer:7050 -n abstore -c '{"Args":["invoke","a","b","10"]}' -C myc

Finally, query ``a``.  We should see a value of ``90``.

.. code:: bash

    peer chaincode query -n abstore -c '{"Args":["query","a"]}' -C myc


Testing new chaincode
---------------------

By default, we mount only ``abstore``.  However, you can easily test different
chaincodes by adding them to the ``chaincode`` subdirectory and relaunching
your network.  At this point they will be accessible in your ``chaincode`` container.

.. Licensed under Creative Commons Attribution 4.0 International License
     https://creativecommons.org/licenses/by/4.0/