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

     1  Getting Started
     2  ===============
     3  
     4  The getting started scenario provisions a sample Fabric network consisting of
     5  two organizations, each maintaining two peers, and a "solo" ordering service.
     6  The required crypto material (x509 certs) for the network entities has been
     7  pre-generated and is baked into the appropriate directories and configuration
     8  files.  You do not need to modify any of these certificates.  The ``examples/e2e_cli``
     9  folder contains the docker compose files and supporting scripts that you will use
    10  to create and test the sample network.
    11  
    12  This section also demonstrates usage of a configuration generation tool,
    13  ``configtxgen``, to generate the network's configuration.  Upon conclusion of
    14  this section, you will have a fully functional transactional network.  Let's get going...
    15  
    16  Prerequisites
    17  -------------
    18  
    19  Complete the following to install the Fabric source code and build the ``configtxgen``
    20  tool:
    21  
    22  -  Follow the steps for setting up a :doc:`development
    23     environment <dev-setup/devenv>` and ensure that you have properly set the
    24     $GOPATH environment variable.  This process will load the various software
    25     dependencies onto your machine.
    26  
    27  -  Clone the Fabric code base.
    28  
    29  .. code:: bash
    30  
    31         git clone https://github.com/hyperledger/fabric.git
    32  
    33  - Build the ``configtxgen`` tool.
    34  
    35  If you are running Linux, open a terminal and execute the following from your
    36  ``fabric`` directory:
    37  
    38  .. code:: bash
    39  
    40      cd $GOPATH/src/github.com/hyperledger/fabric
    41      make configtxgen
    42      # if you see an error stating - 'ltdl.h' file not found
    43      sudo apt install libtool libltdl-dev
    44      # then run the make again
    45      make configtxgen
    46  
    47  If you are running OSX, first install `Xcode <https://developer.apple.com/xcode/>`__ version 8 or above.
    48  Next, open a terminal and execute the following from your ``fabric`` directory:
    49  
    50  .. code:: bash
    51  
    52     # install Homebrew
    53     /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    54     # add gnu-tar
    55     brew install gnu-tar --with-default-names
    56     # add libtool
    57     brew install libtool
    58     # make the configtxgen
    59     make configtxgen
    60  
    61  A successful build will result in the following output:
    62  
    63  .. code:: bash
    64  
    65     build/bin/configtxgen
    66     CGO_CFLAGS=" " GOBIN=/Users/johndoe/work/src/github.com/hyperledger/fabric/build/bin go install -ldflags "-X github.com/hyperledger/fabric/common/metadata.Version=1.0.0-snapshot-8d3275f -X github.com/hyperledger/fabric/common /metadata.BaseVersion=0.3.0 -X github.com/hyperledger/fabric/common/metadata.BaseDockerLabel=org.hyperledger.fabric"       github.com/hyperledger/fabric/common/configtx/tool/configtxgen
    67     Binary available as build/bin/configtxgen``
    68  
    69  The executable is placed in the ``build/bin/configtxgen`` directory of the Fabric
    70  source tree.
    71  
    72  All in one with docker images
    73  -----------------------------
    74  
    75  In order to expedite the process, we provide a script that performs all the heavy
    76  lifting.  This script will generate the configuration artifacts, spin up a
    77  local network, and drive the chaincode tests.  Navigate to the ``examples/e2e_cli``
    78  directory.
    79  
    80  First, pull the images from Docker Hub for your Fabric network:
    81  
    82  .. code:: bash
    83  
    84          # make the script an executable
    85          chmod +x download-dockerimages.sh
    86          # run the script
    87          ./download-dockerimages.sh
    88  
    89  This process will take a few minutes...
    90  
    91  You should see the exact output in your terminal upon completion of the script:
    92  
    93  .. code:: bash
    94  
    95             ===> List out hyperledger docker images
    96             hyperledger/fabric-ca          latest               35311d8617b4        7 days ago          240 MB
    97             hyperledger/fabric-ca          x86_64-1.0.0-alpha   35311d8617b4        7 days ago          240 MB
    98             hyperledger/fabric-couchdb     latest               f3ce31e25872        7 days ago          1.51 GB
    99             hyperledger/fabric-couchdb     x86_64-1.0.0-alpha   f3ce31e25872        7 days ago          1.51 GB
   100             hyperledger/fabric-kafka       latest               589dad0b93fc        7 days ago          1.3 GB
   101             hyperledger/fabric-kafka       x86_64-1.0.0-alpha   589dad0b93fc        7 days ago          1.3 GB
   102             hyperledger/fabric-zookeeper   latest               9a51f5be29c1        7 days ago          1.31 GB
   103             hyperledger/fabric-zookeeper   x86_64-1.0.0-alpha   9a51f5be29c1        7 days ago          1.31 GB
   104             hyperledger/fabric-orderer     latest               5685fd77ab7c        7 days ago          182 MB
   105             hyperledger/fabric-orderer     x86_64-1.0.0-alpha   5685fd77ab7c        7 days ago          182 MB
   106             hyperledger/fabric-peer        latest               784c5d41ac1d        7 days ago          184 MB
   107             hyperledger/fabric-peer        x86_64-1.0.0-alpha   784c5d41ac1d        7 days ago          184 MB
   108             hyperledger/fabric-javaenv     latest               a08f85d8f0a9        7 days ago          1.42 GB
   109             hyperledger/fabric-javaenv     x86_64-1.0.0-alpha   a08f85d8f0a9        7 days ago          1.42 GB
   110             hyperledger/fabric-ccenv       latest               91792014b61f        7 days ago          1.29 GB
   111             hyperledger/fabric-ccenv       x86_64-1.0.0-alpha   91792014b61f        7 days ago          1.29 GB
   112  
   113  
   114  Now run the all-in-one script:
   115  
   116  .. code:: bash
   117  
   118          ./network_setup.sh up <channel-ID>
   119  
   120  If you choose not to pass the ``channel-ID`` parameter, then your channel name
   121  will default to ``mychannel``.  In your terminal you will see the chaincode logs
   122  for the various commands being executed within the script.
   123  
   124  When the script completes successfully, you should see the following message
   125  in your terminal:
   126  
   127  .. code:: bash
   128  
   129    ===================== Query on PEER3 on channel 'mychannel' is successful =====================
   130  
   131    ===================== All GOOD, End-2-End execution completed =====================
   132  
   133  At this point your network is up and running and the tests have completed
   134  successfully.  Continue through this document for more advanced network
   135  operations.
   136  
   137  Clean up
   138  ^^^^^^^^
   139  
   140  Shut down your network:
   141  
   142  .. code:: bash
   143  
   144          # make sure you're in the e2e_cli directory
   145          docker rm -f $(docker ps -aq)
   146  
   147  Next, execute a ``docker images`` command in your terminal to view the
   148  **chaincode** images.  They will look similar to the following:
   149  
   150  .. code:: bash
   151  
   152    REPOSITORY                     TAG                  IMAGE ID            CREATED             SIZE
   153    dev-peer3-mycc-1.0             latest               13f6c8b042c6        5 minutes ago       176 MB
   154    dev-peer0-mycc-1.0             latest               e27456b2bd92        5 minutes ago       176 MB
   155    dev-peer2-mycc-1.0             latest               111098a7c98c        5 minutes ago       176 MB
   156  
   157  Remove these images:
   158  
   159    .. code:: bash
   160  
   161        docker rmi <IMAGE ID> <IMAGE ID> <IMAGE ID>
   162  
   163  For example:
   164  
   165    .. code:: bash
   166  
   167        docker rmi -f 13f e27 111
   168  
   169  Finally, delete the config artifacts.  Navigate to the ``crypto/orderer``
   170  directory and remove ``orderer.block`` and ``channel.tx``.  You can rerun the
   171  all-in-one script or continue reading for a deeper dive on configuration
   172  transactions and chaincode commands.
   173  
   174  Configuration Transaction Generator
   175  -----------------------------------
   176  
   177  The :doc:`configtxgen tool <configtxgen>` is used to create two artifacts:
   178  Orderer **bootstrap block** and Fabric **channel configuration transaction**.
   179  
   180  The orderer block is the genesis block for the ordering service, and the
   181  channel transaction file is broadcast to the orderer at channel creation
   182  time.
   183  
   184  The ``configtx.yaml`` contains the definitions for the sample network and presents
   185  the topology of the network components - two members (Org0 & Org1), each managing
   186  and maintaining two peers.  It also points to the filesystem location
   187  where the cryptographic material for each network entity is stored.   This
   188  directory, ``crypto``, contains the admin certs, ca certs, signing certs, and
   189  private key for each entity.
   190  
   191  For ease of use, we provide a script - ``generateCfgTrx.sh`` - that orchestrates
   192  the process of running ``configtxgen``.  The script will output our two
   193  configuration artifacts - ``orderer.block`` and ``channel.tx``.  If you ran the
   194  all-in-one script then these artifacts have already been created.  Navigate to the
   195  ``crypto/orderer`` directory and ensure they have been deleted.
   196  
   197  Run the ``generateCfgTrx.sh`` shell script
   198  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   199  
   200  Make sure you are in the ``e2e_cli`` directory:
   201  
   202  .. code:: bash
   203  
   204     cd $GOPATH/src/github.com/hyperledger/fabric/examples/e2e_cli
   205  
   206  The ``generateCfgTrx.sh`` script takes an optional <channel-ID> argument. If none
   207  is provided, then a channel will be generated with a default channel-ID of ``mychannel``.
   208  
   209  .. code:: bash
   210  
   211      # as mentioned above, the <channel-ID> parm is optional
   212      ./generateCfgTrx.sh <channel-ID>
   213  
   214  After you run the shell script, you should see an output in your
   215  terminal similar to the following:
   216  
   217  .. code:: bash
   218  
   219      2017/02/28 17:01:52 Generating new channel configtx
   220      2017/02/28 17:01:52 Creating no-op MSP instance
   221      2017/02/28 17:01:52 Obtaining default signing identity
   222      2017/02/28 17:01:52 Creating no-op signing identity instance
   223      2017/02/28 17:01:52 Serializing identity
   224      2017/02/28 17:01:52 signing message
   225      2017/02/28 17:01:52 signing message
   226      2017/02/28 17:01:52 Writing new channel tx
   227  
   228  The script generates two files - ``orderer.block`` and ``channel.tx`` and outputs
   229  them into the ``crypto/orderer directory``.
   230  
   231  ``orderer.block`` is the genesis block for the ordering service.  ``channel.tx``
   232  contains the configuration information for the new channel.  As mentioned
   233  earlier, both are derived from ``configtx.yaml`` and contain data such as crypto
   234  material and network endpoint information.
   235  
   236  .. note:: You also have the option to manually exercise the embedded commands within
   237            the ``generateCfgTrx.sh`` script.  Open the script and inspect the syntax for the
   238            two commands.  If you do elect to pursue this route, you must
   239            replace the default ``configtx.yaml`` in the fabric source tree.  Navigate to the
   240            ``sampleconfig`` directory and replace the ``configtx.yaml`` file with
   241            the supplied yaml file in the ``e2e_cli`` directory. Then return to the ``fabric``
   242            directory to execute the commands (you will run these manual commands from ``fabric``,
   243            NOT from ``e2e_cli``).  Be sure to remove any existing artifacts from
   244            previous runs of the ``generateCfgTrx.sh`` script before commencing.
   245  
   246  Start the network
   247  -----------------
   248  
   249  We will use docker-compose to launch our network with the images that we pulled
   250  earlier.  If you have not yet pulled the Fabric images, return to the **All in one**
   251  section and follow the instructions to retrieve the images.
   252  
   253  Embedded within the docker-compose file is a script, ``script.sh``, which joins
   254  the peers to a channel and sends read/write requests to the peers.  As a result,
   255  you are able to see the transaction flow without manually submitting the commands.
   256  Skip down to the **Manually execute the transactions** section if you don't want
   257  to leverage the script.
   258  
   259  Make sure you are in the ``e2e_cli`` directory. Then use docker-compose
   260  to spawn the network entities and kick off the embedded script.
   261  
   262  .. code:: bash
   263  
   264      CHANNEL_NAME=<channel-id> docker-compose up -d
   265  
   266  If you created a unique channel name, be sure to pass in that parameter.
   267  Otherwise, pass in the default ``mychannel`` string.  For example:
   268  
   269  .. code:: bash
   270  
   271      CHANNEL_NAME=mychannel docker-compose up -d
   272  
   273  Wait, 30 seconds. Behind the scenes, there are transactions being sent
   274  to the peers. Execute a ``docker ps`` to view your active containers.
   275  You should see an output identical to the following:
   276  
   277  .. code:: bash
   278  
   279      vagrant@hyperledger-devenv:v0.3.0-4eec836:/opt/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli$ docker ps
   280      CONTAINER ID        IMAGE                        COMMAND                  CREATED              STATUS              PORTS                                              NAMES
   281      45e3e114f7a2        dev-peer3-mycc-1.0           "chaincode -peer.a..."   4 seconds ago        Up 4 seconds                                                           dev-peer3-mycc-1.0
   282      5970f740ad2b        dev-peer0-mycc-1.0           "chaincode -peer.a..."   24 seconds ago       Up 23 seconds                                                          dev-peer0-mycc-1.0
   283      b84808d66e99        dev-peer2-mycc-1.0           "chaincode -peer.a..."   48 seconds ago       Up 47 seconds                                                          dev-peer2-mycc-1.0
   284      16d7d94c8773        hyperledger/fabric-peer      "peer node start -..."   About a minute ago   Up About a minute   0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer3
   285      3561a99e35e6        hyperledger/fabric-peer      "peer node start -..."   About a minute ago   Up About a minute   0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer2
   286      0baad3047d92        hyperledger/fabric-peer      "peer node start -..."   About a minute ago   Up About a minute   0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1
   287      1216896b7b4f        hyperledger/fabric-peer      "peer node start -..."   About a minute ago   Up About a minute   0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0
   288      155ff8747b4d        hyperledger/fabric-orderer   "orderer"                About a minute ago   Up About a minute   0.0.0.0:7050->7050/tcp                             orderer
   289  
   290  What happened behind the scenes?
   291  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   292  
   293  -  A script - ``script.sh`` - is baked inside the CLI container. The
   294     script drives the ``createChannel`` command against the default
   295     ``mychannel`` name.  The ``createChannel`` command uses the ``channel.tx``
   296     artifact created previously with the configtxgen tool.
   297  
   298  -  The output of ``createChannel`` is a genesis block -
   299     ``mychannel.block`` - which is stored on the file system.
   300  
   301  -  The ``joinChannel`` command is exercised for all four peers who will
   302     pass in the genesis block - ``mychannel.block``.
   303  
   304  -  Now we have a channel consisting of four peers, and two
   305     organizations.
   306  
   307  -  ``PEER0`` and ``PEER1`` belong to Org0; ``PEER2`` and ``PEER3``
   308     belong to Org1
   309  
   310  -  Recall that these relationships are defined in the ``configtx.yaml``
   311  
   312  -  A chaincode - **chaincode_example02** - is installed on ``PEER0`` and
   313     ``PEER2``
   314  
   315  -  The chaincode is then "instantiated" on ``PEER2``. Instantiate simply
   316     refers to starting the container and initializing the key value pairs
   317     associated with the chaincode. The initial values for this example
   318     are ["a","100" "b","200"]. This "instantiation" results in a container
   319     by the name of ``dev-peer2-mycc-1.0`` starting.  Notice that this container
   320     is specific to ``PEER2``.
   321  
   322  -  The instantiation also passes in an argument for the endorsement
   323     policy. The policy is defined as
   324     ``-P "OR ('Org0MSP.member','Org1MSP.member')"``, meaning that any
   325     transaction must be endorsed by a peer tied to Org0 or Org1.
   326  
   327  -  A query against the value of "a" is issued to ``PEER0``. The
   328     chaincode was previously installed on ``PEER0``, so this will start
   329     another container by the name of ``dev-peer0-mycc-1.0``. The result
   330     of the query is also returned. No write operations have occurred, so
   331     a query against "a" will still return a value of "100"
   332  
   333  -  An invoke is sent to ``PEER0`` to move "10" from "a" to "b"
   334  
   335  -  The chaincode is installed on ``PEER3``
   336  
   337  -  A query is sent to ``PEER3`` for the value of "a". This starts a
   338     third chaincode container by the name of ``dev-peer3-mycc-1.0``. A
   339     value of 90 is returned, correctly reflecting the previous
   340     transaction during which the value for key "a" was modified by 10.
   341  
   342  What does this demonstrate?
   343  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   344  
   345  Chaincode **MUST** be installed on a peer in order for it to
   346  successfully perform read/write operations against the ledger.
   347  Furthermore, a chaincode container is not started for a peer until a
   348  read/write operation is performed against that chaincode (e.g. query for
   349  the value of "a"). The transaction causes the container to start. Also,
   350  all peers in a channel maintain an exact copy of the ledger which
   351  comprises the blockchain to store the immutable, sequenced record in
   352  blocks, as well as a state database to maintain current fabric state.
   353  This includes those peers that do not have chaincode installed on them
   354  (like ``Peer3`` in the above example) . Finally, the chaincode is accessible
   355  after it is installed (like ``Peer3`` in the above example) because it
   356  already has been instantiated.
   357  
   358  How do I see these transactions?
   359  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   360  
   361  Check the logs for the CLI docker container.
   362  
   363  .. code:: bash
   364  
   365      docker logs -f cli
   366  
   367  You should see the following output:
   368  
   369  .. code:: bash
   370  
   371      2017-02-28 04:31:20.841 UTC [logging] InitFromViper -> DEBU 001 Setting default logging level to DEBUG for command 'chaincode'
   372      2017-02-28 04:31:20.842 UTC [msp] GetLocalMSP -> DEBU 002 Returning existing local MSP
   373      2017-02-28 04:31:20.842 UTC [msp] GetDefaultSigningIdentity -> DEBU 003 Obtaining default signing identity
   374      2017-02-28 04:31:20.843 UTC [msp] Sign -> DEBU 004 Sign: plaintext: 0A8F050A59080322096D796368616E6E...6D7963631A0A0A0571756572790A0161
   375      2017-02-28 04:31:20.843 UTC [msp] Sign -> DEBU 005 Sign: digest: 52F1A41B7B0B08CF3FC94D9D7E916AC4C01C54399E71BC81D551B97F5619AB54
   376      Query Result: 90
   377      2017-02-28 04:31:30.425 UTC [main] main -> INFO 006 Exiting.....
   378      ===================== Query on chaincode on PEER3 on channel 'mychannel' is successful =====================
   379  
   380      ===================== All GOOD, End-2-End execution completed =====================
   381  
   382  You also have the option of viewing the logs in real time.  You will need two
   383  terminals for this.  First, kill your docker containers:
   384  
   385  .. code:: bash
   386  
   387     docker rm -f $(docker ps -aq)
   388  
   389  In the first terminal launch your docker-compose script:
   390  
   391  .. code:: bash
   392  
   393     # add the appropriate CHANNEL_NAME parm
   394     CHANNEL_NAME=<channel-id> docker-compose up -d
   395  
   396  In your second terminal view the logs:
   397  
   398  .. code:: bash
   399  
   400      docker logs -f cli
   401  
   402  This will show you the live output for the transactions being driven by ``script.sh``.
   403  
   404  How can I see the chaincode logs?
   405  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   406  
   407  Inspect the individual chaincode containers to see the separate
   408  transactions executed against each container. Here is the combined
   409  output from each container:
   410  
   411  .. code:: bash
   412  
   413      $ docker logs dev-peer2-mycc-1.0
   414      04:30:45.947 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW]
   415      ex02 Init
   416      Aval = 100, Bval = 200
   417  
   418      $ docker logs dev-peer0-mycc-1.0
   419      04:31:10.569 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW]
   420      ex02 Invoke
   421      Query Response:{"Name":"a","Amount":"100"}
   422      ex02 Invoke
   423      Aval = 90, Bval = 210
   424  
   425      $ docker logs dev-peer3-mycc-1.0
   426      04:31:30.420 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW]
   427      ex02 Invoke
   428      Query Response:{"Name":"a","Amount":"90"}
   429  
   430  
   431  Manually execute the transactions
   432  ---------------------------------
   433  
   434  The following section caters towards a more advanced chaincode developer.  It involves
   435  wide usage of global environment variables and requires exact syntax in order
   436  for commands to work properly.  Fully-functional sample commands are provided,
   437  however it is still recommended that you have a fundamental understanding of
   438  Fabric before continuing with this section.
   439  
   440  From your current working directory, kill your containers:
   441  
   442  .. code:: bash
   443  
   444          docker rm -f $(docker ps -aq)
   445  
   446  Next, execute a ``docker images`` command in your terminal to view the
   447  **chaincode** images.  They will look similar to the following:
   448  
   449  .. code:: bash
   450  
   451    REPOSITORY                     TAG                  IMAGE ID            CREATED             SIZE
   452    dev-peer3-mycc-1.0             latest               13f6c8b042c6        5 minutes ago       176 MB
   453    dev-peer0-mycc-1.0             latest               e27456b2bd92        5 minutes ago       176 MB
   454    dev-peer2-mycc-1.0             latest               111098a7c98c        5 minutes ago       176 MB
   455  
   456    Remove these images:
   457  
   458    .. code:: bash
   459  
   460        docker rmi <IMAGE ID> <IMAGE ID> <IMAGE ID>
   461  
   462    For example:
   463  
   464    .. code:: bash
   465  
   466        docker rmi -f 13f e27 111
   467  
   468  Ensure you have the configuration artifacts. If you deleted them, run
   469  the shell script again:
   470  
   471  .. code:: bash
   472  
   473      ./generateCfgTrx.sh <channel-ID>
   474  
   475  Or manually generate the artifacts using the commands within the script.
   476  
   477  Modify the docker-compose file
   478  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   479  
   480  Open the docker-compose file and comment out the command to run
   481  ``script.sh``. Navigate down to the cli image and place a ``#`` to the
   482  left of the command. For example:
   483  
   484  .. code:: bash
   485  
   486          working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
   487        # command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}'
   488  
   489  Save the file.  Now restart your network:
   490  
   491  .. code:: bash
   492  
   493      # make sure you are in the e2e_cli directory where you docker-compose script resides
   494      # add the appropriate CHANNEL_NAME parm
   495      CHANNEL_NAME=<channel-id> docker-compose up -d
   496  
   497  Command syntax
   498  ^^^^^^^^^^^^^^
   499  
   500  Refer to the create and join commands in the ``script.sh``.  The file is
   501  located in the ``scripts`` directory.
   502  
   503  For the following cli commands against ``PEER0`` to work, you need to set the
   504  values for the four global environment variables, given below. Please make sure to override
   505  the values accordingly when calling commands against other peers and the
   506  orderer.
   507  
   508  .. code:: bash
   509  
   510      # Environment variables for PEER0
   511      CORE_PEER_MSPCONFIGPATH=$GOPATH/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig
   512      CORE_PEER_ADDRESS=peer0:7051
   513      CORE_PEER_LOCALMSPID="Org0MSP"
   514      CORE_PEER_TLS_ROOTCERT_FILE=$GOPATH/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig/cacerts/peerOrg0.pem
   515  
   516  These environment variables for each peer are defined in the supplied
   517  docker-compose file.
   518  
   519  Create channel
   520  ^^^^^^^^^^^^^^
   521  
   522  Exec into the cli container:
   523  
   524  .. code:: bash
   525  
   526      docker exec -it cli bash
   527  
   528  If successful you should see the following:
   529  
   530  .. code:: bash
   531  
   532      root@0d78bb69300d:/opt/gopath/src/github.com/hyperledger/fabric/peer#
   533  
   534  Specify your channel name with the ``-c`` flag. Specify your channel
   535  configuration transaction with the ``-f`` flag. In this case it is
   536  ``channeltx``, however you can mount your own configuration transaction
   537  with a different name.
   538  
   539  .. code:: bash
   540  
   541      # the channel.tx and orderer.block are mounted in the crypto/orderer directory within your cli container
   542      # as a result, we pass the full path for the file
   543       peer channel create -o orderer0:7050 -c mychannel -f crypto/orderer/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $GOPATH/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem
   544  
   545  Since the ``channel create`` command runs against the orderer, we need to override the
   546  four environment variables set before. So the above command in its entirety would be:
   547  
   548  .. code:: bash
   549  
   550      CORE_PEER_MSPCONFIGPATH=$GOPATH/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig CORE_PEER_LOCALMSPID="OrdererMSP" peer channel create -o orderer0:7050 -c mychannel -f crypto/orderer/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $GOPATH/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem
   551  
   552  
   553  .. note:: You will remain in the CLI container for the remainder of
   554            these manual commands. You must also remember to preface all commands
   555            with the corresponding environment variables for targeting a peer other than
   556            ``PEER0``.
   557  
   558  Join channel
   559  ^^^^^^^^^^^^
   560  
   561  Join specific peers to the channel
   562  
   563  .. code:: bash
   564  
   565      # By default, this joins PEER0 only
   566      # the mychannel.block is also mounted in the crypto/orderer directory
   567       peer channel join -b mychannel.block
   568  
   569  This full command in its entirety would be:
   570  
   571  .. code:: bash
   572  
   573      CORE_PEER_MSPCONFIGPATH=$GOPATH/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig CORE_PEER_ADDRESS=peer0:7051 CORE_PEER_LOCALMSPID="Org0MSP" CORE_PEER_TLS_ROOTCERT_FILE=$GOPATH/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig/cacerts/peerOrg0.pem peer channel join -b mychannel.block
   574  
   575  You can make other peers join the channel as necessary by making appropriate
   576  changes in the four environment variables.
   577  
   578  Install chaincode onto a remote peer
   579  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   580  
   581  Install the sample go code onto one of the four peer nodes
   582  
   583  .. code:: bash
   584  
   585      # remember to preface this command with the global environment variables for the appropriate peer
   586      peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
   587  
   588  Instantiate chaincode and define the endorsement policy
   589  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   590  
   591  Instantiate the chaincode on a peer. This will launch a chaincode
   592  container for the targeted peer and set the endorsement policy for the
   593  chaincode. In this snippet, we define the policy as requiring an
   594  endorsement from one peer node that is a part of either ``Org0`` or ``Org1``.
   595  The command is:
   596  
   597  .. code:: bash
   598  
   599      # remember to preface this command with the global environment variables for the appropriate peer
   600      # remember to pass in the correct string for the -C argument.  The default is mychannel
   601      peer chaincode instantiate -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $GOPATH/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org0MSP.member','Org1MSP.member')"
   602  
   603  See the `endorsement
   604  policies <endorsement-policies>` documentation for more details on policy implementation.
   605  
   606  Invoke chaincode
   607  ^^^^^^^^^^^^^^^^
   608  
   609  .. code:: bash
   610  
   611      # remember to preface this command with the global environment variables for the appropriate peer
   612      peer chaincode invoke -o orderer0:7050  --tls $CORE_PEER_TLS_ENABLED --cafile $GOPATH/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem  -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
   613  
   614  **NOTE**: Make sure to wait a few seconds for the operation to complete.
   615  
   616  Query chaincode
   617  ^^^^^^^^^^^^^^^
   618  
   619  .. code:: bash
   620  
   621      # remember to preface this command with the global environment variables for the appropriate peer
   622      peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
   623  
   624  The result of the above command should be as below:
   625  
   626  .. code:: bash
   627  
   628      Query Result: 90
   629  
   630  Manually generate images
   631  ------------------------
   632  
   633  Fabric developers can elect to create the images against the latest iteration
   634  of the code base.  This is a useful method for testing new features that have
   635  not yet been baked into the published images.
   636  
   637  -  Make the peer and orderer images.
   638  
   639  .. code:: bash
   640  
   641       # make sure you are in the fabric directory
   642       # if you are unable to generate the images natively, you may need to be in a vagrant environment
   643       make peer-docker orderer-docker
   644  
   645  Execute a ``docker images`` command in your terminal. If the images
   646  compiled successfully, you should see an output similar to the
   647  following:
   648  
   649  .. code:: bash
   650  
   651                 vagrant@hyperledger-devenv:v0.3.0-4eec836:/opt/gopath/src/github.com/hyperledger/fabric$ docker images
   652                 REPOSITORY                     TAG                             IMAGE ID            CREATED             SIZE
   653                 hyperledger/fabric-orderer     latest                          264e45897bfb        10 minutes ago      180 MB
   654                 hyperledger/fabric-orderer     x86_64-0.7.0-snapshot-a0d032b   264e45897bfb        10 minutes ago      180 MB
   655                 hyperledger/fabric-peer        latest                          b3d44cff07c6        10 minutes ago      184 MB
   656                 hyperledger/fabric-peer        x86_64-0.7.0-snapshot-a0d032b   b3d44cff07c6        10 minutes ago      184 MB
   657                 hyperledger/fabric-javaenv     latest                          6e2a2adb998a        10 minutes ago      1.42 GB
   658                 hyperledger/fabric-javaenv     x86_64-0.7.0-snapshot-a0d032b   6e2a2adb998a        10 minutes ago      1.42 GB
   659                 hyperledger/fabric-ccenv       latest                          0ce0e7dc043f        12 minutes ago      1.29 GB
   660                 hyperledger/fabric-ccenv       x86_64-0.7.0-snapshot-a0d032b   0ce0e7dc043f        12 minutes ago      1.29 GB
   661                 hyperledger/fabric-baseimage   x86_64-0.3.0                    f4751a503f02        4 weeks ago         1.27 GB
   662                 hyperledger/fabric-baseos      x86_64-0.3.0                    c3a4cf3b3350        4 weeks ago         161 MB
   663  
   664  
   665  Use the native binaries
   666  -------------------------------------------------
   667  
   668  Similar to the previous two sections, this is catered towards advanced developers
   669  with a working understanding of the Fabric codebase.
   670  
   671  Open your vagrant environment:
   672  
   673  .. code:: bash
   674  
   675      cd $GOPATH/src/github.com/hyperledger/fabric/devenv
   676  
   677  .. code:: bash
   678  
   679      # you may have to first start your VM with vagrant up
   680      vagrant ssh
   681  
   682  From the ``fabric`` directory issue the following commands to
   683  build the peer and orderer executables:
   684  
   685  .. code:: bash
   686  
   687      make clean
   688      make native
   689  
   690  You will also need the ``ccenv`` image. From the ``fabric`` directory:
   691  
   692  .. code:: bash
   693  
   694      make peer-docker
   695  
   696  Next, open two more terminals and start your vagrant environment in
   697  each. You should now have a total of three terminals, all within
   698  vagrant.
   699  
   700  Before starting, make sure to clear your ledger folder
   701  ``/var/hyperledger/``. You will want to do this after each run to avoid
   702  errors and duplication.
   703  
   704  .. code:: bash
   705  
   706      rm -rf /var/hyperledger/*
   707  
   708  **Vagrant window 1**
   709  
   710  Use the ``configtxgen`` tool to create the orderer genesis block:
   711  
   712  .. code:: bash
   713  
   714      configtxgen -profile SampleSingleMSPSolo -outputBlock orderer.block
   715  
   716  **Vagrant window 2**
   717  
   718  Start the orderer with the genesis block you just generated:
   719  
   720  .. code:: bash
   721  
   722      ORDERER_GENERAL_GENESISMETHOD=file ORDERER_GENERAL_GENESISFILE=./orderer.block orderer
   723  
   724  **Vagrant window 1**
   725  
   726  Create the channel configuration transaction:
   727  
   728  .. code:: bash
   729  
   730      configtxgen -profile SampleSingleMSPSolo -outputCreateChannelTx channel.tx -channelID <channel-ID>
   731  
   732  This will generate a ``channel.tx`` file in your current directory
   733  
   734  **Vagrant window 3**
   735  
   736  Start the peer in "chainless" mode
   737  
   738  .. code:: bash
   739  
   740      peer node start --peer-defaultchain=false
   741  
   742  **Note**: Use Vagrant window 1 for the remainder of commands
   743  
   744  Create channel
   745  ^^^^^^^^^^^^^^
   746  
   747  Ask peer to create a channel with the configuration parameters in
   748  ``channel.tx``
   749  
   750  .. code:: bash
   751  
   752      peer channel create -o 127.0.0.1:7050 -c mychannel -f channel.tx
   753  
   754  This will return a channel genesis block - ``mychannel.block`` - in your
   755  current directory.
   756  
   757  Join channel
   758  ^^^^^^^^^^^^
   759  
   760  Ask peer to join the channel by passing in the channel genesis block:
   761  
   762  .. code:: bash
   763  
   764      peer channel join -b mychannel.block
   765  
   766  Install
   767  ^^^^^^^
   768  
   769  Install chaincode on the peer:
   770  
   771  .. code:: bash
   772  
   773      peer chaincode install -o 127.0.0.1:7050 -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
   774  
   775  Make sure the chaincode is in the filesystem:
   776  
   777  .. code:: bash
   778  
   779      ls /var/hyperledger/production/chaincodes
   780  
   781  You should see ``mycc.1.0``
   782  
   783  Instantiate
   784  ^^^^^^^^^^^
   785  
   786  Instantiate the chaincode:
   787  
   788  .. code:: bash
   789  
   790      peer chaincode instantiate -o 127.0.0.1:7050 -C mychannel -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Args":["init","a", "100", "b","200"]}'
   791  
   792  Check your active containers:
   793  
   794  .. code:: bash
   795  
   796      docker ps
   797  
   798  If the chaincode container started successfully, you should see:
   799  
   800  .. code:: bash
   801  
   802      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
   803      bd9c6bda7560        dev-jdoe-mycc-1.0   "chaincode -peer.a..."   5 seconds ago       Up 5 seconds                            dev-jdoe-mycc-1.0
   804  
   805  Invoke
   806  ^^^^^^
   807  
   808  Issue an invoke to move "10" from "a" to "b":
   809  
   810  .. code:: bash
   811  
   812      peer chaincode invoke -o 127.0.0.1:7050 -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
   813  
   814  Wait a few seconds for the operation to complete
   815  
   816  Query
   817  ^^^^^
   818  
   819  Query for the value of "a":
   820  
   821  .. code:: bash
   822  
   823      # this should return 90
   824      peer chaincode query -o 127.0.0.1:7050 -C mychannel -n mycc -c '{"Args":["query","a"]}'
   825  
   826  Don't forget to clear ledger folder ``/var/hyperledger/`` after each
   827  run!
   828  
   829  .. code:: bash
   830  
   831      rm -rf /var/hyperledger/*
   832  
   833  Using CouchDB
   834  -------------
   835  
   836  The state database can be switched from the default (goleveldb) to CouchDB.
   837  The same chaincode functions are available with CouchDB, however, there is the
   838  added ability to perform rich and complex queries against the state database
   839  data content contingent upon the chaincode data being modeled as JSON.
   840  
   841  To use CouchDB instead of the default database (goleveldb), follow the same
   842  procedure in the **Prerequisites** section, and additionally perform the
   843  following two steps to enable the CouchDB containers and associate each peer
   844  container with a CouchDB container:
   845  
   846  -  Make the CouchDB image.
   847  
   848  .. code:: bash
   849  
   850         # make sure you are in the fabric directory
   851         make couchdb
   852  
   853  -  Open the ``fabric/examples/e2e_cli/docker-compose.yaml`` and un-comment
   854     all commented statements relating to CouchDB containers and peer container
   855     use of CouchDB. These instructions are are also outlined in the
   856     same ``docker-compose.yaml`` file. Search the file for 'couchdb' (case insensitive) references.
   857  
   858  **chaincode_example02** should now work using CouchDB underneath.
   859  
   860  .. note:: If you choose to implement mapping of the fabric-couchdb container
   861            port to a host port, please make sure you are aware of the security
   862            implications. Mapping of the port in a development environment allows the
   863            visualization of the database via the CouchDB web interface (Fauxton).
   864            Production environments would likely refrain from implementing port mapping in
   865            order to restrict outside access to the CouchDB containers.
   866  
   867  You can use **chaincode_example02** chaincode against the CouchDB state database
   868  using the steps outlined above, however in order to exercise the query
   869  capabilities you will need to use a chaincode that has data modeled as JSON,
   870  (e.g. **marbles02**). You can locate the **marbles02** chaincode in the
   871  ``fabric/examples/chaincode/go`` directory.
   872  
   873  Install, instantiate, invoke, and query **marbles02** chaincode by following the
   874  same general steps outlined above for **chaincode_example02** in the
   875  **Manually execute transactions** section. After the **Join channel** step, use the
   876  following commands to interact with the **marbles02** chaincode:
   877  
   878  -  Install and instantiate the chaincode on ``PEER0``:
   879  
   880  .. code:: bash
   881  
   882         peer chaincode install -o orderer0:7050 -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02
   883         peer chaincode instantiate -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02 -c '{"Args":["init"]}' -P "OR ('Org0MSP.member','Org1MSP.member')"
   884  
   885  -  Create some marbles and move them around:
   886  
   887  .. code:: bash
   888  
   889          peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["initMarble","marble1","blue","35","tom"]}'
   890          peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["initMarble","marble2","red","50","tom"]}'
   891          peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["initMarble","marble3","blue","70","tom"]}'
   892          peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["transferMarble","marble2","jerry"]}'
   893          peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["transferMarblesBasedOnColor","blue","jerry"]}'
   894          peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["delete","marble1"]}'
   895  
   896  
   897  -  If you chose to activate port mapping, you can now view the state database
   898     through the CouchDB web interface (Fauxton) by opening a browser and
   899     navigating to one of the two URLs below.
   900  
   901     For containers running in a vagrant environment:
   902  
   903     ``http://localhost:15984/_utils``
   904  
   905     For non-vagrant environment, use the port address that was mapped in CouchDB
   906     container specification:
   907  
   908     ``http://localhost:5984/_utils``
   909  
   910     You should see a database named ``mychannel`` and the documents
   911     inside it.
   912  
   913  -  You can run regular queries from the cli (e.g. reading ``marble2``):
   914  
   915  .. code:: bash
   916  
   917        peer chaincode query -C mychannel -n marbles -c '{"Args":["readMarble","marble2"]}'
   918  
   919  You should see the details of ``marble2``:
   920  
   921  .. code:: bash
   922  
   923         Query Result: {"color":"red","docType":"marble","name":"marble2","owner":"jerry","size":50}
   924  
   925  Retrieve the history of ``marble1``:
   926  
   927  .. code:: bash
   928  
   929        peer chaincode query -C mychannel -n marbles -c '{"Args":["getHistoryForMarble","marble1"]}'
   930  
   931  You should see the transactions on ``marble1``:
   932  
   933  .. code:: bash
   934  
   935        Query Result: [{"TxId":"1c3d3caf124c89f91a4c0f353723ac736c58155325f02890adebaa15e16e6464", "Value":{"docType":"marble","name":"marble1","color":"blue","size":35,"owner":"tom"}},{"TxId":"755d55c281889eaeebf405586f9e25d71d36eb3d35420af833a20a2f53a3eefd", "Value":{"docType":"marble","name":"marble1","color":"blue","size":35,"owner":"jerry"}},{"TxId":"819451032d813dde6247f85e56a89262555e04f14788ee33e28b232eef36d98f", "Value":}]
   936  
   937  You can also perform rich queries on the data content, such as querying marble fields by owner ``jerry``:
   938  
   939  .. code:: bash
   940  
   941        peer chaincode query -C mychannel -n marbles -c '{"Args":["queryMarblesByOwner","jerry"]}'
   942  
   943  The output should display the two marbles owned by ``jerry``:
   944  
   945  .. code:: bash
   946  
   947         Query Result: [{"Key":"marble2", "Record":{"color":"red","docType":"marble","name":"marble2","owner":"jerry","size":50}},{"Key":"marble3", "Record":{"color":"blue","docType":"marble","name":"marble3","owner":"jerry","size":70}}]
   948  
   949  Query by field ``owner`` where the value is ``jerry``:
   950  
   951  .. code:: bash
   952  
   953        peer chaincode query -C mychannel -n marbles -c '{"Args":["queryMarbles","{\"selector\":{\"owner\":\"jerry\"}}"]}'
   954  
   955  The output should display:
   956  
   957  .. code:: bash
   958  
   959         Query Result: [{"Key":"marble2", "Record":{"color":"red","docType":"marble","name":"marble2","owner":"jerry","size":50}},{"Key":"marble3", "Record":{"color":"blue","docType":"marble","name":"marble3","owner":"jerry","size":70}}]
   960  
   961  A Note on Data Persistence
   962  --------------------------
   963  
   964  If data persistence is desired on the peer container or the CouchDB container,
   965  one option is to mount a directory in the docker-host into a relevant directory
   966  in the container. For example, you may add the following two lines in
   967  the peer container specification in the ``docker-compose.yaml`` file:
   968  
   969  .. code:: bash
   970  
   971         volumes:
   972          - /var/hyperledger/peer0:/var/hyperledger/production
   973  
   974  
   975  For the CouchDB container, you may add the following two lines in the CouchDB
   976  container specification:
   977  
   978  .. code:: bash
   979  
   980         volumes:
   981          - /var/hyperledger/couchdb0:/opt/couchdb/data
   982  
   983  
   984  Troubleshooting
   985  ---------------
   986  
   987  -  Ensure you clear the file system after each run
   988  
   989  -  If you see docker errors, remove your images and start from scratch.
   990  
   991  .. code:: bash
   992  
   993         make clean
   994         make peer-docker orderer-docker
   995  
   996  -  If you see the below error:
   997  
   998  .. code:: bash
   999  
  1000         Error: Error endorsing chaincode: rpc error: code = 2 desc = Error installing chaincode code mycc:1.0(chaincode /var/hyperledger/production/chaincodes/mycc.1.0 exits)
  1001  
  1002  You likely have chaincode images (e.g. ``dev-peer0-mycc-1.0`` or ``dev-peer1-mycc-1.0``)
  1003  from prior runs. Remove them and try again.
  1004  
  1005  .. code:: bash
  1006  
  1007      docker rmi -f $(docker images | grep peer[0-9]-peer[0-9] | awk '{print $3}')
  1008  
  1009  -  To cleanup the network, use the ``down`` option:
  1010  
  1011  .. code:: bash
  1012  
  1013         ./network_setup.sh down