github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/docs/source/getting_started.rst (about)

     1  Getting Started
     2  ===============
     3  
     4  .. note:: These instructions have been verified to work against the
     5            version "1.0.0-beta" tagged docker images and the pre-compiled
     6            setup utilities within the supplied tar file. If you run
     7            these commands with images or tools from the current master
     8            branch, it is possible that you will see configuration and panic
     9            errors.
    10  
    11  The getting started scenario provisions a sample Fabric network consisting of
    12  two organizations, each maintaining two peer nodes, and a "solo" ordering
    13  service.
    14  
    15  Install prerequisites
    16  ---------------------
    17  
    18  Before we begin, if you haven't already done so, you may wish to check that
    19  you have all the :doc:`prereqs` installed on the platform(s)
    20  on which you'll be developing blockchain applications and/or operating
    21  Hyperledger Fabric.
    22  
    23  Download the artifacts and binaries & pull the docker images
    24  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    25  
    26  .. note:: If you are running on Windows you will want to make use of the ``Git
    27            Bash shell`` extension for the upcoming terminal commands. Visit the
    28            :doc:`prereqs` if you haven't previously installed
    29            it.
    30  
    31  Determine a location on your machine where you want to place the artifacts
    32  and binaries.
    33  
    34  .. code:: bash
    35  
    36    mkdir fabric-sample
    37    cd fabric-sample
    38  
    39  Next, execute the following command:
    40  
    41  .. code:: bash
    42  
    43    curl -sSL https://goo.gl/LQkuoh | bash
    44  
    45  This command downloads and executes a bash script that will
    46  extract all of the necessary artifacts to set up your network and place them
    47  into a folder named ``release``.
    48  
    49  It also retrieves the two platform-specific binaries - ``cryptogen``,
    50  ``configtxgen`` and ``configtxlator`` - which we'll use later. Finally, the
    51  script will download the Hyperledger Fabric docker images from
    52  `DockerHub <https://hub.docker.com/u/hyperledger/>`__ into
    53  your local Docker registry.
    54  
    55  The script lists out the docker images installed upon conclusion.
    56  
    57  Look at the names for each image; these are the components that will ultimately
    58  comprise our Fabric network.  You will also notice that you have two instances
    59  of the same image ID - one tagged as "x86_64-1.0.0-beta" and one tagged as "latest".
    60  (Note that on different architectures, the x86_64 would be replaced with the string
    61  identifying your architecture).
    62  
    63  Want to run it now?
    64  ^^^^^^^^^^^^^^^^^^^
    65  
    66  We provide a script that leverages these docker images to quickly bootstrap
    67  a Fabric network, join peers to a channel, and drive transactions.  If you're
    68  already familiar with Fabric or just want to see it in action, we have provided
    69  a script that runs an end-to-end sample application.
    70  
    71  This script literally does it all.  It calls ``generateArtifacts.sh`` to exercise
    72  the ``cryptogen`` and ``configtxgen`` tools, followed by ``script.sh`` which
    73  launches the network, joins peers to a generated channel and then drives
    74  transactions.  If you choose not to supply a channel ID, then the
    75  script will use a default name of ``mychannel``.  The cli timeout parameter
    76  is an optional value; if you choose not to set it, then your cli container
    77  will exit upon conclusion of the script.
    78  
    79  Change to the release subdirectory corresponding to your architecture
    80  that was created in the first step, such as ``release/windows-amd64``,
    81  then execute one of the following commands:
    82  
    83  .. code:: bash
    84  
    85                ./network_setup.sh up
    86  
    87  OR
    88  
    89  .. code:: bash
    90  
    91                ./network_setup.sh up <channel-ID> <timeout-value>
    92  
    93  Once the demo has completed execution, run it again to clean up...
    94  
    95  The following will kill your containers, remove the crypto material and
    96  four artifacts, and remove our the created chaincode images:
    97  
    98  .. code:: bash
    99  
   100                ./network_setup.sh down
   101  
   102  If you'd like to learn more about the underlying tooling and bootstrap mechanics,
   103  continue reading.  In these next sections we'll walk through the various steps
   104  and requirements to build a fully-functional Fabric.
   105  
   106  Crypto Generator
   107  ----------------
   108  
   109  We will use the ``cryptogen`` tool to generate the cryptographic material
   110  (x509 certs) for our various network entities.  The certificates are based on
   111  a standard PKI implementation where validation is achieved by reaching a
   112  common trust anchor.
   113  
   114  How does it work?
   115  ^^^^^^^^^^^^^^^^^
   116  
   117  Cryptogen consumes a file - ``crypto-config.yaml`` - that contains the network
   118  topology and allows us to generate a library of certificates for both the
   119  Organizations and the components that belong to those Organizations.  Each
   120  Organization is provisioned a unique root certificate (``ca-cert``), that binds
   121  specific components (peers and orderers) to that Org.  Transactions and communications
   122  within Fabric are signed by an entity's private key (``keystore``), and then verified
   123  by means of a public key (``signcerts``).  You will notice a "count" variable within
   124  this file.  We use this to specify the number of peers per Organization; in our
   125  case it's two peers per Org.  The rest of this template is extremely
   126  self-explanatory.
   127  
   128  After we run the tool, the certs will be parked in a folder titled ``crypto-config``.
   129  
   130  Configuration Transaction Generator
   131  -----------------------------------
   132  
   133  The `configtxgen
   134  tool <https://github.com/hyperledger/fabric/blob/master/docs/source/configtxgen.rst>`__
   135  is used to create four configuration artifacts: orderer **bootstrap block**, fabric
   136  **channel configuration transaction**, and two **anchor peer transactions** - one
   137  for each Peer Org.
   138  
   139  The orderer block is the genesis block for the ordering service, and the
   140  channel transaction file is broadcast to the orderer at channel creation
   141  time.  The anchor peer transactions, as the name might suggest, specify each
   142  Org's anchor peer on this channel.
   143  
   144  How does it work?
   145  ^^^^^^^^^^^^^^^^^
   146  
   147  Configtxgen consumes a file - ``configtx.yaml`` - that contains the definitions
   148  for the sample network. There are three members - one Orderer Org (``OrdererOrg``)
   149  and two Peer Orgs (``Org1`` & ``Org2``) each managing and maintaining two peer nodes.
   150  This file also specifies a consortium - ``SampleConsortium`` - consisting of our
   151  two Peer Orgs.  Pay specific attention to the "Profiles" section at the top of
   152  this file.  You will notice that we have two unique headers. One for the orderer genesis
   153  block - ``TwoOrgsOrdererGenesis`` - and one for our channel - ``TwoOrgsChannel``.
   154  These headers are important, as we will pass them in as arguments when we create
   155  our artifacts.  This file also contains two additional specifications that are worth
   156  noting.  Firstly, we specify the anchor peers for each Peer Org
   157  (``peer0.org1.example.com`` & ``peer0.org2.example.com``).  Secondly, we point to
   158  the location of the MSP directory for each member, in turn allowing us to store the
   159  root certificates for each Org in the orderer genesis block.  This is a critical
   160  concept. Now any network entity communicating with the ordering service can have
   161  its digital signature verified.
   162  
   163  For ease of use, a script - ``generateArtifacts.sh`` - is provided. The
   164  script will generate the crypto material and our four configuration artifacts, and
   165  subsequently output these files into the ``channel-artifacts`` folder.
   166  
   167  Run the tools
   168  -------------
   169  
   170  We offer two approaches here.  You can manually generate the certificates/keys
   171  and the various configuration artifacts using the commands exposed below.
   172  Alternatively, we provide a script which will generate everything in just a few
   173  seconds.  It's recommended to run through the manual approach initially, as it
   174  will better familiarize you with the tools and command syntax.  However, if you just want
   175  to spin up your network, jump down to the :ref:`Run-the-shell-script` section.
   176  
   177  Manually generate the artifacts
   178  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   179  
   180  You can refer to the ``generateArtifacts.sh`` script for the commands, however
   181  for the sake of convenience we will also provide them here.
   182  
   183  First let's run the cryptogen tool.  Our binary is in the ``bin``
   184  directory, so we need to provide the relative path to where the tool resides.
   185  Make sure you are in the directory correlated to your platform.  For example,
   186  OSX users would be in ``release/darwin-amd64`` when running the following commands:
   187  
   188  .. code:: bash
   189  
   190      ./bin/cryptogen generate --config=./crypto-config.yaml
   191  
   192  You will likely see the following warning.  It's innocuous, ignore it:
   193  
   194  .. code:: bash
   195  
   196      [bccsp] GetDefault -> WARN 001 Before using BCCSP, please call InitFactories(). Falling back to bootBCCSP.
   197  
   198  Next, we need to tell the ``configtxgen`` tool where to look for the
   199  ``configtx.yaml`` file that it needs to ingest.  We will tell it look in our
   200  present working directory:
   201  
   202  .. code:: bash
   203  
   204      export FABRIC_CFG_PATH=$PWD
   205  
   206  Create the orderer genesis block:
   207  
   208  .. code:: bash
   209  
   210      ./bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
   211  
   212  You can ignore the logs regarding intermediate certs, we are not using them in
   213  this crypto implementation.
   214  
   215  Create the channel transaction artifact:
   216  
   217  .. code:: bash
   218  
   219      # make sure to set the <channel-ID> parm
   220  
   221      ./bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID <channel-ID>
   222  
   223  Define the anchor peer for Org1 on the channel:
   224  
   225  .. code:: bash
   226  
   227      # make sure to set the <channel-ID> parm
   228  
   229      ./bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID <channel-ID> -asOrg Org1MSP
   230  
   231  Define the anchor peer for Org2 on the channel:
   232  
   233  .. code:: bash
   234  
   235      # make sure to set the <channel-ID> parm
   236  
   237      ./bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID <channel-ID> -asOrg Org2MSP
   238  
   239  .. _Run-the-shell-script:
   240  
   241  Run the shell script
   242  ^^^^^^^^^^^^^^^^^^^^
   243  
   244  You can skip this step if you just manually generated the crypto and artifacts.
   245  However, if you want to see this script in action, delete your crypto material
   246  and channel artifacts with the following command:
   247  
   248  .. code:: bash
   249  
   250      ./network_setup.sh down
   251  
   252  Now proceed...
   253  
   254  Make sure you are in the ``<your_platform>`` directory where the script resides.
   255  Decide upon a unique name for your channel and replace the <channel-ID> parm
   256  with a name of your choice.  The script will fail if you do not supply a name.
   257  
   258  .. code:: bash
   259  
   260      ./generateArtifacts.sh <channel-ID>
   261  
   262  The output of the script is somewhat verbose, as it generates the crypto
   263  libraries and multiple artifacts.  However, you will notice five distinct
   264  and self-explanatory messages in your terminal.  They are as follows:
   265  
   266  .. code:: bash
   267  
   268    ##########################################################
   269    ##### Generate certificates using cryptogen tool #########
   270    ##########################################################
   271  
   272    ##########################################################
   273    #########  Generating Orderer Genesis block ##############
   274    ##########################################################
   275  
   276    #################################################################
   277    ### Generating channel configuration transaction 'channel.tx' ###
   278    #################################################################
   279  
   280    #################################################################
   281    #######    Generating anchor peer update for Org0MSP   ##########
   282    #################################################################
   283  
   284    #################################################################
   285    #######    Generating anchor peer update for Org1MSP   ##########
   286    #################################################################
   287  
   288  
   289  These configuration transactions will bundle the crypto material for the
   290  participating members and their network components and output an orderer
   291  genesis block and three channel transaction artifacts. These artifacts are
   292  required to successfully bootstrap a Fabric network and create a channel to
   293  transact upon.
   294  
   295  Start the network
   296  -----------------
   297  
   298  We will leverage a docker-compose script to spin up our network. The docker-compose
   299  points to the images that we have already downloaded, and bootstraps the orderer
   300  with our previously generated ``orderer.block``. Before launching the network, open
   301  the ``docker-compose-cli.yaml`` file and comment out the script.sh in the CLI
   302  container. Your docker-compose should look like this:
   303  
   304  .. code:: bash
   305  
   306    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
   307    # command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'
   308    volumes
   309  
   310  If left uncommented, the script will exercise all of the CLI commands when the
   311  network is started. However, we want to go through the commands manually in order
   312  to expose the syntax and functionality of each call.
   313  
   314  Pass in a moderately high value for the ``TIMEOUT`` variable (specified in seconds);
   315  otherwise the CLI container, by default, will exit after 60 seconds.
   316  
   317  Start your network:
   318  
   319  .. code:: bash
   320  
   321            # make sure you are in the <your_platform> directory where your docker-compose script resides
   322  
   323            CHANNEL_NAME=<channel-id> TIMEOUT=<pick_a_value> docker-compose -f docker-compose-cli.yaml up -d
   324  
   325  If you want to see the realtime logs for your network, then do not supply the ``-d`` flag.
   326  If you let the logs stream, then you will need to open a second terminal to execute the CLI calls.
   327  
   328  Environment variables
   329  ^^^^^^^^^^^^^^^^^^^^^
   330  
   331  For the following CLI commands against ``peer0.org1.example.com`` to work, we need
   332  to preface our commands with the four environment variables given below.  These
   333  variables for ``peer0.org1.example.com`` are baked into the CLI container,
   334  therefore we can operate without passing them.  **HOWEVER**, if you want to send
   335  calls to other peers or the orderer, then you will need to provide these
   336  values accordingly.  Inspect the ``docker-compose-base.yaml`` for the specific
   337  paths:
   338  
   339  .. code:: bash
   340  
   341      # Environment variables for PEER0
   342  
   343      CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
   344      CORE_PEER_ADDRESS=peer0.org1.example.com:7051
   345      CORE_PEER_LOCALMSPID="Org1MSP"
   346      CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
   347  
   348  Create & Join Channel
   349  ^^^^^^^^^^^^^^^^^^^^
   350  
   351  Exec into the cli container:
   352  
   353  .. code:: bash
   354  
   355          docker exec -it cli bash
   356  
   357  If successful you should see the following:
   358  
   359  .. code:: bash
   360  
   361          root@0d78bb69300d:/opt/gopath/src/github.com/hyperledger/fabric/peer#
   362  
   363  Recall that we used the configtxgen tool to generate a channel configuration
   364  artifact - ``channel.tx``. We are going to pass in this artifact to the orderer
   365  as part of the create channel request.
   366  
   367  .. note:: notice the ``-- cafile`` which we pass as part of this command.  It is the
   368            local path to the orderer's root cert, allowing us to verify the TLS handshake.
   369  
   370  We specify our channel name with the ``-c`` flag and our channel configuration
   371  transaction with the ``-f`` flag. In this case it is ``channel.tx``, however
   372  you can mount your own configuration transaction with a different name.
   373  
   374  .. code:: bash
   375  
   376          # the channel.tx file is mounted in the channel-artifacts directory within your cli container
   377          # as a result, we pass the full path for the file
   378          # we also pass the path for the orderer ca-cert in order to verify the TLS handshake
   379          # be sure to replace the $CHANNEL_NAME variable appropriately
   380  
   381          peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem
   382  
   383  This command returns a genesis block - ``<channel-ID.block>`` - which we will use to join the channel.
   384  It contains the configuration information specified in ``channel.tx``.
   385  
   386  
   387  .. note:: You will remain in the CLI container for the remainder of
   388            these manual commands. You must also remember to preface all commands
   389            with the corresponding environment variables when targeting a peer other than
   390            ``peer0.org1.example.com``.
   391  
   392  Now let's join ``peer0.org1.example.com`` to the channel.
   393  
   394  .. code:: bash
   395  
   396          # By default, this joins ``peer0.org1.example.com`` only
   397          # the <channel-ID>.block was returned by the previous command
   398  
   399           peer channel join -b <channel-ID.block>
   400  
   401  You can make other peers join the channel as necessary by making appropriate
   402  changes in the four environment variables.
   403  
   404  Install & Instantiate
   405  ^^^^^^^^^^^^^^^^^^^^^
   406  
   407  Applications interact with the blockchain ledger through chaincode.  As such we need to
   408  install the chaincode on any peer that will execute and endorse transactions, and
   409  then instantiate the chaincode on the channel.
   410  
   411  First, install the sample go code onto one of the four peer nodes.  This command
   412  places the source code onto our peer's filesystem.
   413  
   414  .. code:: bash
   415  
   416      peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
   417  
   418  Next, instantiate the chaincode on the channel. This will initialize the chaincode
   419  on the channel, set the endorsement policy for the chaincode, and launch a chaincode
   420  container for the targeted peer.  Take note of the ``-P`` argument. This is our policy where we specify the required
   421  level of endorsement for a transaction against this chaincode to be validated.
   422  In the command below you’ll notice that we specify our policy as
   423  ``-P "OR ('Org0MSP.member','Org1MSP.member')"``. This means that we need
   424  “endorsement” from a peer belonging to Org1 **OR** Org2 (i.e. only one endorsement).
   425  If we changed the syntax to ``AND`` then we would need two endorsements.
   426  
   427  .. code:: bash
   428  
   429      # be sure to replace the $CHANNEL_NAME environment variable
   430      # if you did not install your chaincode with a name of mycc, then modify that argument as well
   431  
   432      peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
   433  
   434  See the `endorsement
   435  policies <http://hyperledger-fabric.readthedocs.io/en/latest/endorsement-policies.html>`__
   436  documentation for more details on policy implementation.
   437  
   438  Query
   439  ^^^^^
   440  
   441  Let's query for the value of ``a`` to make sure the chaincode was properly
   442  instantiated and the state DB was populated. The syntax for query is as follows:
   443  
   444  .. code:: bash
   445  
   446    # be sure to set the -C and -n flags appropriately
   447  
   448    peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
   449  
   450  
   451  Invoke
   452  ^^^^^^
   453  
   454  Now let's move ``10`` from ``a`` to ``b``.  This transaction will cut a new block and
   455  update the state DB. The syntax for invoke is as follows:
   456  
   457  .. code:: bash
   458  
   459      # be sure to set the -C and -n flags appropriately
   460  
   461      peer chaincode invoke -o orderer.example.com:7050  --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem  -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'
   462  
   463  Query
   464  ^^^^^
   465  
   466  Let's confirm that our previous invocation executed properly. We initialized the
   467  key ``a`` with a value of ``100`` and just removed ``10`` with our previous
   468  invocation. Therefore, a query against ``a`` should reveal ``90``. The syntax
   469  for query is as follows.
   470  
   471  .. code:: bash
   472  
   473    # be sure to set the -C and -n flags appropriately
   474  
   475    peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
   476  
   477  We should see the following:
   478  
   479  .. code:: bash
   480  
   481     Query Result: 90
   482  
   483  Feel free to start over and manipulate the key value pairs and subsequent
   484  invocations.
   485  
   486  Scripts
   487  -------
   488  
   489  We exposed the verbosity of the commands in order to provide some edification on
   490  the underlying flow and the appropriate syntax. Entering the commands manually
   491  through the CLI is quite onerous, therefore we provide a few scripts to do the
   492  entirety of the heavy lifting.
   493  
   494  .. _Network-Setup:
   495  
   496  All in one
   497  ^^^^^^^^^^
   498  
   499  This script literally does it all.  It calls ``generateArtifacts.sh`` to exercise
   500  the ``cryptogen`` and ``configtxgen`` tools, followed by ``script.sh`` which
   501  launches the network, joins peers to a generated channel and then drives
   502  transactions.  If you choose not to supply a channel ID, then the
   503  script will use a default name of ``mychannel``.  The cli timeout parameter
   504  is an optional value; if you choose not to set it, then your cli container
   505  will exit upon conclusion of the script.
   506  
   507  .. code:: bash
   508  
   509                ./network_setup.sh up
   510  
   511  OR
   512  
   513  .. code:: bash
   514  
   515                ./network_setup.sh up <channel-ID> <timeout-value>
   516  
   517  Now clean up...
   518  
   519  The following script will kill our containers, remove the crypto material and
   520  four artifacts, and remove our three chaincode images:
   521  
   522  .. code:: bash
   523  
   524                ./network_setup.sh down
   525  
   526  
   527  Config only
   528  ^^^^^^^^^^^
   529  
   530  The other option is to manually generate your crypto material and configuration
   531  artifacts, and then use the embedded ``script.sh`` in the docker-compose files
   532  to drive your network.  Make sure this script is not commented out in your
   533  CLI container.  Before starting, make sure you've cleaned up your environment:
   534  
   535  .. code:: bash
   536  
   537    ./network_setup.sh down
   538  
   539  Next, open your ``docker-compose-cli.yaml`` and make sure the ``script.sh``
   540  command is not commented out in the CLI container.  It should look exactly like
   541  this:
   542  
   543  .. code:: bash
   544  
   545    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
   546    command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'
   547    volumes
   548  
   549  From the ``<your_platform>`` directory, use docker-compose to spawn the network
   550  entities and drive the tests.  Notice that you can set a ``TIMEOUT`` variable
   551  (specified in seconds) so that your cli container does not exit after the script
   552  completes.  You can choose any value:
   553  
   554  .. code:: bash
   555  
   556          # the TIMEOUT variable is optional
   557  
   558          CHANNEL_NAME=<channel-id> TIMEOUT=<pick_a_value> docker-compose -f docker-compose-cli.yaml up -d
   559  
   560  If you created a unique channel name, be sure to pass in that parameter. For example,
   561  
   562  .. code:: bash
   563  
   564          CHANNEL_NAME=abc TIMEOUT=1000 docker-compose -f docker-compose-cli.yaml up -d
   565  
   566  Wait, 60 seconds or so. Behind the scenes, there are transactions being sent
   567  to the peers. Execute a ``docker ps`` to view your active containers.
   568  You should see an output identical to the following:
   569  
   570  .. code:: bash
   571  
   572        CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
   573        b568de3fe931        dev-peer1.org2.example.com-mycc-1.0   "chaincode -peer.a..."   4 minutes ago       Up 4 minutes                                                           dev-peer1.org2.example.com-mycc-1.0
   574        17c1c82087e7        dev-peer0.org1.example.com-mycc-1.0   "chaincode -peer.a..."   4 minutes ago       Up 4 minutes                                                           dev-peer0.org1.example.com-mycc-1.0
   575        0e1c5034c47b        dev-peer0.org2.example.com-mycc-1.0   "chaincode -peer.a..."   4 minutes ago       Up 4 minutes                                                           dev-peer0.org2.example.com-mycc-1.0
   576        71339e7e1d38        hyperledger/fabric-peer               "peer node start -..."   5 minutes ago       Up 5 minutes        0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
   577        add6113ffdcf        hyperledger/fabric-peer               "peer node start -..."   5 minutes ago       Up 5 minutes        0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
   578        689396c0e520        hyperledger/fabric-peer               "peer node start -..."   5 minutes ago       Up 5 minutes        0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com
   579        65424407a653        hyperledger/fabric-orderer            "orderer"                5 minutes ago       Up 5 minutes        0.0.0.0:7050->7050/tcp                             orderer.example.com
   580        ce14853db660        hyperledger/fabric-peer               "peer node start -..."   5 minutes ago       Up 5 minutes        0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com
   581  
   582  If you set a moderately high ``TIMEOUT`` value, then you will see your cli
   583  container as well.
   584  
   585  What's happening behind the scenes?
   586  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   587  
   588  -  A script - ``script.sh`` - is baked inside the CLI container. The
   589     script drives the ``createChannel`` command against the supplied channel name
   590     and uses the channel.tx file for channel configuration.
   591  
   592  -  The output of ``createChannel`` is a genesis block -
   593     ``<your_channel_name>.block`` - which gets stored on the peers' file systems and contains
   594     the channel configuration specified from channel.tx.
   595  
   596  -  The ``joinChannel`` command is exercised for all four peers, which takes as
   597     input the previously generated genesis block.  This command instructs the
   598     peers to join ``<your_channel_name>`` and create a chain starting with ``<your_channel_name>.block``.
   599  
   600  -  Now we have a channel consisting of four peers, and two
   601     organizations.  This is our ``TwoOrgsChannel`` profile.
   602  
   603  -  ``peer0.org1.example.com`` and ``peer1.org1.example.com`` belong to Org1;
   604     ``peer0.org2.example.com`` and ``peer1.org2.example.com`` belong to Org2
   605  
   606  -  These relationships are defined through the ``crypto-config.yaml`` and
   607     the MSP path is specified in our docker compose.
   608  
   609  -  The anchor peers for Org1MSP (``peer0.org1.example.com``) and
   610     Org2MSP (``peer0.org2.example.com``) are then updated.  We do this by passing
   611     the ``Org1MSPanchors.tx`` and ``Org2MSPanchors.tx`` artifacts to the ordering
   612     service along with the name of our channel.
   613  
   614  -  A chaincode - **chaincode_example02** - is installed on ``peer0.org1.example.com`` and
   615     ``peer0.org2.example.com``
   616  
   617  -  The chaincode is then "instantiated" on ``peer0.org2.example.com``. Instantiation
   618     adds the chaincode to the channel, starts the container for the target peer,
   619     and initializes the key value pairs associated with the chaincode.  The initial
   620     values for this example are ["a","100" "b","200"]. This "instantiation" results
   621     in a container by the name of ``dev-peer0.org2.example.com-mycc-1.0`` starting.
   622  
   623  -  The instantiation also passes in an argument for the endorsement
   624     policy. The policy is defined as
   625     ``-P "OR    ('Org1MSP.member','Org2MSP.member')"``, meaning that any
   626     transaction must be endorsed by a peer tied to Org1 or Org2.
   627  
   628  -  A query against the value of "a" is issued to ``peer0.org1.example.com``. The
   629     chaincode was previously installed on ``peer0.org1.example.com``, so this will start
   630     a container for Org1 peer0 by the name of ``dev-peer0.org1.example.com-mycc-1.0``. The result
   631     of the query is also returned. No write operations have occurred, so
   632     a query against "a" will still return a value of "100".
   633  
   634  -  An invoke is sent to ``peer0.org1.example.com`` to move "10" from "a" to "b"
   635  
   636  -  The chaincode is then installed on ``peer1.org2.example.com``
   637  
   638  -  A query is sent to ``peer1.org2.example.com`` for the value of "a". This starts a
   639     third chaincode container by the name of ``dev-peer1.org2.example.com-mycc-1.0``. A
   640     value of 90 is returned, correctly reflecting the previous
   641     transaction during which the value for key "a" was modified by 10.
   642  
   643  What does this demonstrate?
   644  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   645  
   646  Chaincode **MUST** be installed on a peer in order for it to
   647  successfully perform read/write operations against the ledger.
   648  Furthermore, a chaincode container is not started for a peer until an ``init`` or
   649  traditional transaction - read/write - is performed against that chaincode (e.g. query for
   650  the value of "a"). The transaction causes the container to start. Also,
   651  all peers in a channel maintain an exact copy of the ledger which
   652  comprises the blockchain to store the immutable, sequenced record in
   653  blocks, as well as a state database to maintain current fabric state.
   654  This includes those peers that do not have chaincode installed on them
   655  (like ``peer1.org1.example.com`` in the above example) . Finally, the chaincode is accessible
   656  after it is installed (like ``peer1.org2.example.com`` in the above example) because it
   657  has already been instantiated.
   658  
   659  How do I see these transactions?
   660  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   661  
   662  Check the logs for the CLI docker container.
   663  
   664  .. code:: bash
   665  
   666          docker logs -f cli
   667  
   668  You should see the following output:
   669  
   670  .. code:: bash
   671  
   672        2017-05-16 17:08:01.366 UTC [msp] GetLocalMSP -> DEBU 004 Returning existing local MSP
   673        2017-05-16 17:08:01.366 UTC [msp] GetDefaultSigningIdentity -> DEBU 005 Obtaining default signing identity
   674        2017-05-16 17:08:01.366 UTC [msp/identity] Sign -> DEBU 006 Sign: plaintext: 0AB1070A6708031A0C08F1E3ECC80510...6D7963631A0A0A0571756572790A0161
   675        2017-05-16 17:08:01.367 UTC [msp/identity] Sign -> DEBU 007 Sign: digest: E61DB37F4E8B0D32C9FE10E3936BA9B8CD278FAA1F3320B08712164248285C54
   676        Query Result: 90
   677        2017-05-16 17:08:15.158 UTC [main] main -> INFO 008 Exiting.....
   678        ===================== Query on PEER3 on channel 'mychannel' is successful =====================
   679  
   680        ===================== All GOOD, End-2-End execution completed =====================
   681  
   682  
   683         _____   _   _   ____            _____   ____    _____
   684        | ____| | \ | | |  _ \          | ____| |___ \  | ____|
   685        |  _|   |  \| | | | | |  _____  |  _|     __) | |  _|
   686        | |___  | |\  | | |_| | |_____| | |___   / __/  | |___
   687        |_____| |_| \_| |____/          |_____| |_____| |_____|
   688  
   689  You can scroll through these logs to see the various transactions.
   690  
   691  How can I see the chaincode logs?
   692  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   693  
   694  Inspect the individual chaincode containers to see the separate
   695  transactions executed against each container. Here is the combined
   696  output from each container:
   697  
   698  .. code:: bash
   699  
   700          $ docker logs dev-peer0.org2.example.com-mycc-1.0
   701          04:30:45.947 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW]
   702          ex02 Init
   703          Aval = 100, Bval = 200
   704  
   705          $ docker logs dev-peer0.org1.example.com-mycc-1.0
   706          04:31:10.569 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW]
   707          ex02 Invoke
   708          Query Response:{"Name":"a","Amount":"100"}
   709          ex02 Invoke
   710          Aval = 90, Bval = 210
   711  
   712          $ docker logs dev-peer1.org2.example.com-mycc-1.0
   713          04:31:30.420 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW]
   714          ex02 Invoke
   715          Query Response:{"Name":"a","Amount":"90"}
   716  
   717  Understanding the docker-compose topology
   718  -----------------------------------------
   719  
   720  The ``<your_platform`` directory offers us two flavors of docker-compose files, both of which
   721  are extended from the ``docker-compose-base.yaml`` (located in the ``base`` folder).  Our first flavor,
   722  ``docker-compose-cli.yaml``, provides us with a CLI container, along with an orderer,
   723  four peers, and the optional couchDB containers.  We use this docker-compose for
   724  the entirety of the instructions on this page.
   725  
   726  .. note:: the remainder of this section covers a docker-compose file designed for the
   727            SDK.  Refer to the `Node.js SDK <https://github.com/hyperledger/fabric-sdk-node>`__ repo for details
   728            on running these tests.
   729  
   730  The second flavor, ``docker-compose-e2e.yaml``, is constructed to run end-to-end tests
   731  using the Node.js SDK.  Aside from functioning with the SDK, its primary differentiation
   732  is that there are containers for the fabric-ca servers.  As a result, we are able
   733  to send REST calls to the organizational CAs for user registration and enrollment.
   734  
   735  If you want to use the ``docker-compose-e2e.yaml`` without first running the
   736  **All in one** script, then we will need to make four slight modifications.
   737  We need to point to the private keys for our Organization's CA's.  You can locate
   738  these values in your crypto-config folder.  For example, to locate the private
   739  key for Org1 we would follow this path - ``crypto-config/peerOrganizations/org1.example.com/ca/``.
   740  The private key is a long hash value followed by ``_sk``.  The path for Org2
   741  would be - ``crypto-config/peerOrganizations/org2.example.com/ca/``.
   742  
   743  In the ``docker-compose-e2e.yaml`` update the FABRIC_CA_SERVER_TLS_KEYFILE variable
   744  for ca0 and ca1.  You also need to edit the path that is provided in the command
   745  to start the ca server.  You are providing the same private key twice for each
   746  CA container.
   747  
   748  Using CouchDB
   749  -------------
   750  
   751  The state database can be switched from the default (goleveldb) to CouchDB.
   752  The same chaincode functions are available with CouchDB, however, there is the
   753  added ability to perform rich and complex queries against the state database
   754  data content contingent upon the chaincode data being modeled as JSON.
   755  
   756  To use CouchDB instead of the default database (goleveldb), follow the same
   757  procedures outlined earlier for generating the artifacts, except when starting
   758  the network pass the couchdb docker-compose as well:
   759  
   760  .. code:: bash
   761  
   762      # make sure you are in the /e2e_cli directory where your docker-compose script resides
   763      CHANNEL_NAME=<channel-id> TIMEOUT=<pick_a_value> docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml up -d
   764  
   765  **chaincode_example02** should now work using CouchDB underneath.
   766  
   767  .. note::  If you choose to implement mapping of the fabric-couchdb container
   768             port to a host port, please make sure you are aware of the security
   769             implications. Mapping of the port in a development environment makes the
   770             CouchDB REST API available, and allows the
   771             visualization of the database via the CouchDB web interface (Fauxton).
   772             Production environments would likely refrain from implementing port mapping in
   773             order to restrict outside access to the CouchDB containers.
   774  
   775  You can use **chaincode_example02** chaincode against the CouchDB state database
   776  using the steps outlined above, however in order to exercise the CouchDB query
   777  capabilities you will need to use a chaincode that has data modeled as JSON,
   778  (e.g. **marbles02**). You can locate the **marbles02** chaincode in the
   779  ``fabric/examples/chaincode/go`` directory.
   780  
   781  We will follow the same process to create and join the channel as outlined in the
   782  **Manually exercise the commands** section above.  Once you have joined your
   783  peer(s) to the channel, use the following steps to interact with the **marbles02**
   784  chaincode:
   785  
   786  -  Install and instantiate the chaincode on ``peer0.org1.example.com``:
   787  
   788  .. code:: bash
   789  
   790         # be sure to modify the $CHANNEL_NAME variable accordingly for the instantiate command
   791  
   792         peer chaincode install -o orderer.example.com:7050 -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02
   793         peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02 -c '{"Args":["init"]}' -P "OR ('Org0MSP.member','Org1MSP.member')"
   794  
   795  -  Create some marbles and move them around:
   796  
   797  .. code:: bash
   798  
   799          # be sure to modify the $CHANNEL_NAME variable accordingly
   800  
   801          peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble1","blue","35","tom"]}'
   802          peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble2","red","50","tom"]}'
   803          peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble3","blue","70","tom"]}'
   804          peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -c '{"Args":["transferMarble","marble2","jerry"]}'
   805          peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -c '{"Args":["transferMarblesBasedOnColor","blue","jerry"]}'
   806          peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C $CHANNEL_NAME -n marbles -c '{"Args":["delete","marble1"]}'
   807  
   808  -  If you chose to map the CouchDB ports in docker-compose, you can now view
   809     the state database through the CouchDB web interface (Fauxton) by opening
   810     a browser and navigating to the following URL:
   811  
   812     ``http://localhost:5984/_utils``
   813  
   814  You should see a database named ``mychannel`` (or your unique channel name) and
   815  the documents inside it.
   816  
   817  .. note:: For the below commands, be sure to update the $CHANNEL_NAME variable appropriately.
   818  
   819  You can run regular queries from the CLI (e.g. reading ``marble2``):
   820  
   821  .. code:: bash
   822  
   823        peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["readMarble","marble2"]}'
   824  
   825  The output should display the details of ``marble2``:
   826  
   827  .. code:: bash
   828  
   829         Query Result: {"color":"red","docType":"marble","name":"marble2","owner":"jerry","size":50}
   830  
   831  You can retrieve the history of a specific marble - e.g. ``marble1``:
   832  
   833  .. code:: bash
   834  
   835        peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["getHistoryForMarble","marble1"]}'
   836  
   837  The output should display the transactions on ``marble1``:
   838  
   839  .. code:: bash
   840  
   841        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":}]
   842  
   843  You can also perform rich queries on the data content, such as querying marble fields by owner ``jerry``:
   844  
   845  .. code:: bash
   846  
   847        peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["queryMarblesByOwner","jerry"]}'
   848  
   849  The output should display the two marbles owned by ``jerry``:
   850  
   851  .. code:: bash
   852  
   853         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}}]
   854  
   855  A Note on Data Persistence
   856  --------------------------
   857  
   858  If data persistence is desired on the peer container or the CouchDB container,
   859  one option is to mount a directory in the docker-host into a relevant directory
   860  in the container. For example, you may add the following two lines in
   861  the peer container specification in the ``docker-compose-base.yaml`` file:
   862  
   863  .. code:: bash
   864  
   865         volumes:
   866          - /var/hyperledger/peer0:/var/hyperledger/production
   867  
   868  For the CouchDB container, you may add the following two lines in the CouchDB
   869  container specification:
   870  
   871  .. code:: bash
   872  
   873         volumes:
   874          - /var/hyperledger/couchdb0:/opt/couchdb/data
   875  
   876  Troubleshooting
   877  ---------------
   878  
   879  -  Always start your network fresh.  Use the following command
   880     to remove artifacts, crypto, containers and chaincode images:
   881  
   882  .. code:: bash
   883  
   884        ./network_setup.sh down
   885  
   886  - **YOU WILL SEE ERRORS IF YOU DO NOT REMOVE CONTAINERS AND IMAGES**
   887  
   888  -  If you see docker errors, first check your version (should be 1.12 or above),
   889     and then try restarting your docker process.  Problems with Docker are
   890     oftentimes not immediately recognizable.  For example, you may see errors
   891     resulting from an inability to access crypto material mounted within a
   892     container.
   893  
   894  -  If they persist remove your images and start from scratch:
   895  
   896  .. code:: bash
   897  
   898         docker rm -f $(docker ps -aq)
   899         docker rmi -f $(docker images -q)
   900  
   901  -  If you see errors on your create, instantiate, invoke or query commands, make
   902     sure you have properly updated the channel name and chaincode name.  There
   903     are placeholder values in the supplied sample commands.
   904  
   905  -  If you see the below error:
   906  
   907  .. code:: bash
   908  
   909         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)
   910  
   911  You likely have chaincode images (e.g. ``dev-peer1.org2.example.com-mycc-1.0`` or
   912  ``dev-peer0.org1.example.com-mycc-1.0``) from prior runs. Remove them and try
   913  again.
   914  
   915  .. code:: bash
   916  
   917      docker rmi -f $(docker images | grep peer[0-9]-peer[0-9] | awk '{print $3}')
   918  
   919  - If you see something similar to the following:
   920  
   921  .. code:: bash
   922  
   923        Error connecting: rpc error: code = 14 desc = grpc: RPC failed fast due to transport failure
   924        Error: rpc error: code = 14 desc = grpc: RPC failed fast due to transport failure
   925  
   926  Make sure you are running your network against the "beta" images that have been
   927  retagged as "latest".
   928  
   929  If you see the below error:
   930  
   931  .. code:: bash
   932  
   933    [configtx/tool/localconfig] Load -> CRIT 002 Error reading configuration: Unsupported Config Type ""
   934    panic: Error reading configuration: Unsupported Config Type ""
   935  
   936  Then you did not set the ``FABRIC_CFG_PATH`` environment variable properly.  The
   937  configtxgen tool needs this variable in order to locate the configtx.yaml.  Go
   938  back and recreate your channel artifacts.
   939  
   940  -  To cleanup the network, use the ``down`` option:
   941  
   942  .. code:: bash
   943  
   944         ./network_setup.sh down
   945  
   946  - If you see an error stating that you still have "active endpoints", then prune
   947    your docker networks.  This will wipe your previous networks and start you with a
   948    fresh environment:
   949  
   950  .. code:: bash
   951  
   952          docker network prune
   953  
   954  You will see the following message:
   955  
   956  .. code:: bash
   957  
   958    WARNING! This will remove all networks not used by at least one container.
   959    Are you sure you want to continue? [y/N]
   960  
   961  Select ``y``.
   962  
   963  - If you continue to see errors, share your logs on the **# fabric-questions**
   964    channel on `Hyperledger Rocket Chat <https://chat.hyperledger.org/home>`__.
   965  
   966  .. Licensed under Creative Commons Attribution 4.0 International License
   967     https://creativecommons.org/licenses/by/4.0/