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

     1  Setting Up a Network
     2  --------------------
     3  
     4  This document covers setting up a network on your local machine for
     5  various development and testing activities. Unless you are intending to
     6  contribute to the development of the Hyperledger Fabric project, you'll
     7  probably want to follow the more commonly used approach below -
     8  `leveraging published Docker
     9  images <#leveraging-published-docker-images>`__ for the various
    10  Hyperledger Fabric components, directly. Otherwise, skip down to the
    11  `secondary approach <#building-your-own-images>`__ below.
    12  
    13  Leveraging published Docker images
    14  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    15  
    16  This approach simply leverages the Docker images that the Hyperledger
    17  Fabric project publishes to
    18  `DockerHub <https://hub.docker.com/u/hyperledger/>`__ and either Docker
    19  commands or Docker Compose descriptions of the network one wishes to
    20  create.
    21  
    22  Installing Docker
    23  ^^^^^^^^^^^^^^^^^
    24  
    25  **Note:** When running Docker *natively* on Mac and Windows, there is no
    26  IP forwarding support available. Hence, running more than one
    27  fabric-peer image is not advised because you do not want to have
    28  multiple processes binding to the same port. For most application and
    29  chaincode development/testing running with a single fabric peer should
    30  not be an issue unless you are interested in performance and resilience
    31  testing the fabric's capabilities, such as consensus. For more advanced
    32  testing, we strongly recommend using the fabric's Vagrant `development
    33  environment <../dev-setup/devenv.md>`__.
    34  
    35  With this approach, there are multiple choices as to how to run Docker:
    36  using `Docker Toolbox <https://docs.docker.com/toolbox/overview/>`__ or
    37  one of the new native Docker runtime environments for `Mac
    38  OSX <https://docs.docker.com/engine/installation/mac/>`__ or
    39  `Windows <https://docs.docker.com/engine/installation/windows/>`__.
    40  There are some subtle differences between how Docker runs natively on
    41  Mac and Windows versus in a virtualized context on Linux. We'll call
    42  those out where appropriate below, when we get to the point of actually
    43  running the various components.
    44  
    45  Pulling the images from DockerHub
    46  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    47  
    48  Once you have Docker (1.11 or greater) installed and running, prior to
    49  starting any of the fabric components, you will need to first pull the
    50  fabric images from DockerHub.
    51  
    52  ::
    53  
    54        docker pull hyperledger/fabric-peer:latest
    55        docker pull hyperledger/fabric-membersrvc:latest
    56  
    57  Building your own images
    58  ~~~~~~~~~~~~~~~~~~~~~~~~
    59  
    60  **Note:** *This approach is not necessarily recommended for most users*.
    61  If you have pulled images from DockerHub as described in the previous
    62  section, you may proceed to the `next
    63  step <#starting-up-validating-peers>`__.
    64  
    65  The second approach would be to leverage the `development
    66  environment <../dev-setup/devenv.md>`__ setup (which we will assume you
    67  have already established) to build and deploy your own binaries and/or
    68  Docker images from a clone of the
    69  `hyperledger/fabric <https://github.com/hyperledger/fabric>`__ GitHub
    70  repository. This approach is suitable for developers that might wish to
    71  contribute directly to the Hyperledger Fabric project, or that wish to
    72  deploy from a fork of the Hyperledger code base.
    73  
    74  The following commands should be run from *within* the Vagrant
    75  environment described in `Setting Up Development
    76  Environment <../dev-setup/devenv.md>`__.
    77  
    78  To create the Docker image for the ``hyperledger/fabric-peer``:
    79  
    80  ::
    81  
    82      cd $GOPATH/src/github.com/hyperledger/fabric
    83      make peer-image
    84  
    85  To create the Docker image for the ``hyperledger/fabric-membersrvc``:
    86  
    87  ::
    88  
    89      make membersrvc-image
    90  
    91  Starting up validating peers
    92  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    93  
    94  Check the available images again with ``docker images``. You should see
    95  ``hyperledger/fabric-peer`` and ``hyperledger/fabric-membersrvc``
    96  images. For example,
    97  
    98  ::
    99  
   100      $ docker images
   101      REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
   102      hyperledger/fabric-membersrvc   latest              7d5f6e0bcfac        12 days ago         1.439 GB
   103      hyperledger/fabric-peer         latest              82ef20d7507c        12 days ago         1.445 GB
   104  
   105  If you don't see these, go back to the previous step.
   106  
   107  With the relevant Docker images in hand, we can start running the peer
   108  and membersrvc services.
   109  
   110  Determine value for CORE\_VM\_ENDPOINT variable
   111  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   112  
   113  Next, we need to determine the address of your docker daemon for the
   114  CORE\_VM\_ENDPOINT. If you are working within the Vagrant development
   115  environment, or a Docker Toolbox environment, you can determine this
   116  with the ``ip add`` command. For example,
   117  
   118  ::
   119  
   120      $ ip add
   121  
   122      <<< detail removed >>>
   123  
   124      3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
   125          link/ether 02:42:ad:be:70:cb brd ff:ff:ff:ff:ff:ff
   126          inet 172.17.0.1/16 scope global docker0
   127             valid_lft forever preferred_lft forever
   128          inet6 fe80::42:adff:febe:70cb/64 scope link
   129             valid_lft forever preferred_lft forever
   130  
   131  Your output might contain something like
   132  ``inet 172.17.0.1/16 scope global docker0``. That means the docker0
   133  interface is on IP address 172.17.0.1. Use that IP address for the
   134  ``CORE_VM_ENDPOINT`` option. For more information on the environment
   135  variables, see ``core.yaml`` configuration file in the ``fabric``
   136  repository.
   137  
   138  If you are using the native Docker for Mac or Windows, the value for
   139  ``CORE_VM_ENDPOINT`` should be set to ``unix:///var/run/docker.sock``.
   140  [TODO] double check this. I believe that ``127.0.0.1:2375`` also works.
   141  
   142  Assigning a value for CORE\_PEER\_ID
   143  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   144  
   145  The ID value of ``CORE_PEER_ID`` must be unique for each validating
   146  peer, and it must be a lowercase string. We often use a convention of
   147  naming the validating peers vpN where N is an integer starting with 0
   148  for the root node and incrementing N by 1 for each additional peer node
   149  started. e.g. vp0, vp1, vp2, ...
   150  
   151  Consensus
   152  ^^^^^^^^^
   153  
   154  By default, we are using a consensus plugin called ``NOOPS``, which
   155  doesn't really do consensus. If you are running a single peer node,
   156  running anything other than ``NOOPS`` makes little sense. If you want to
   157  use some other consensus plugin in the context of multiple peer nodes,
   158  please see the `Using a Consensus Plugin <#using-a-consensus-plugin>`__
   159  section, below.
   160  
   161  Docker Compose
   162  ^^^^^^^^^^^^^^
   163  
   164  We'll be using Docker Compose to launch our various Fabric component
   165  containers, as this is the simplest approach. You should have it
   166  installed from the initial setup steps. Installing Docker Toolbox or any
   167  of the native Docker runtimes should have installed Compose.
   168  
   169  Start up a validating peer:
   170  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   171  
   172  Let's launch the first validating peer (the root node). We'll set
   173  CORE\_PEER\_ID to vp0 and CORE\_VM\_ENDPOINT as above. Here's the
   174  docker-compose.yml for launching a single container within the
   175  **Vagrant** `development environment <../dev-setup/devenv.md>`__:
   176  
   177  ::
   178  
   179      vp0:
   180        image: hyperledger/fabric-peer
   181        environment:
   182          - CORE_PEER_ID=vp0
   183          - CORE_PEER_ADDRESSAUTODETECT=true
   184          - CORE_VM_ENDPOINT=http://172.17.0.1:2375
   185          - CORE_LOGGING_LEVEL=DEBUG
   186        command: peer node start
   187  
   188  You can launch this Compose file as follows, from the same directory as
   189  the docker-compose.yml file:
   190  
   191  ::
   192  
   193      $ docker-compose up
   194  
   195  Here's the corresponding Docker command:
   196  
   197  ::
   198  
   199      $ docker run --rm -it -e CORE_VM_ENDPOINT=http://172.17.0.1:2375 -e CORE_LOGGING_LEVEL=DEBUG -e CORE_PEER_ID=vp0 -e CORE_PEER_ADDRESSAUTODETECT=true hyperledger/fabric-peer peer node start
   200  
   201  If you are running Docker for Mac or Windows, we'll need to explicitly
   202  map the ports, and we will need a different value for CORE\_VM\_ENDPOINT
   203  as we discussed above.
   204  
   205  Here's the docker-compose.yml for Docker on Mac or Windows:
   206  
   207  ::
   208  
   209      vp0:
   210        image: hyperledger/fabric-peer
   211        ports:
   212          - "7050:7050"
   213          - "7051:7051"
   214          - "7052:7052"
   215        environment:
   216          - CORE_PEER_ADDRESSAUTODETECT=true
   217          - CORE_VM_ENDPOINT=unix:///var/run/docker.sock
   218          - CORE_LOGGING_LEVEL=DEBUG
   219        command: peer node start
   220  
   221  This single peer configuration, running the ``NOOPS`` 'consensus'
   222  plugin, should satisfy many development/test scenarios. ``NOOPS`` is not
   223  really providing consensus, it is essentially a no-op that simulates
   224  consensus. For instance, if you are simply developing and testing
   225  chaincode; this should be adequate unless your chaincode is leveraging
   226  membership services for identity, access control, confidentiality and
   227  privacy.
   228  
   229  Running with the CA
   230  ^^^^^^^^^^^^^^^^^^^
   231  
   232  If you want to take advantage of security (authentication and
   233  authorization), privacy and confidentiality, then you'll need to run the
   234  Fabric's certificate authority (CA). Please refer to the `CA
   235  Setup <ca-setup.md>`__ instructions.
   236  
   237  Start up additional validating peers:
   238  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   239  
   240  Following the pattern we established
   241  `above <#assigning-a-value-for-core_peer_id>`__ we'll use ``vp1`` as the
   242  ID for the second validating peer. If using Docker Compose, we can
   243  simply link the two peer nodes. Here's the docker-compose.yml for a
   244  **Vagrant** environment with two peer nodes - vp0 and vp1:
   245  
   246  ::
   247  
   248      vp0:
   249        image: hyperledger/fabric-peer
   250        environment:
   251          - CORE_PEER_ADDRESSAUTODETECT=true
   252          - CORE_VM_ENDPOINT=http://172.17.0.1:2375
   253          - CORE_LOGGING_LEVEL=DEBUG
   254        command: peer node start
   255      vp1:
   256        extends:
   257          service: vp0
   258        environment:
   259          - CORE_PEER_ID=vp1
   260          - CORE_PEER_DISCOVERY_ROOTNODE=vp0:7051
   261        links:
   262          - vp0
   263  
   264  If we wanted to use the docker command line to launch another peer, we
   265  need to get the IP address of the first validating peer, which will act
   266  as the root node to which the new peer(s) will connect. The address is
   267  printed out on the terminal window of the first peer (e.g. 172.17.0.2)
   268  and should be passed in with the ``CORE_PEER_DISCOVERY_ROOTNODE``
   269  environment variable.
   270  
   271  ::
   272  
   273      docker run --rm -it -e CORE_VM_ENDPOINT=http://172.17.0.1:2375 -e CORE_PEER_ID=vp1 -e CORE_PEER_ADDRESSAUTODETECT=true -e CORE_PEER_DISCOVERY_ROOTNODE=172.17.0.2:7051 hyperledger/fabric-peer peer node start
   274  
   275  .. raw:: html
   276  
   277     <!-- This needs to be sorted out with a revamped security section
   278  
   279     Again, the validating peer `enrollID` and `enrollSecret` (`vp1` and `vp1_secret`) has to be added to [membersrvc.yaml](https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml).
   280  
   281     You can start up a few more validating peers in a similar manner if you wish. Remember to change the peer ID and add the enrollID/enrollSecret to the [membersrvc.yaml](https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml).
   282  
   283     ### Enroll/Login a test user (if security is enabled):
   284     If security is enabled, you must enroll a user with the certificate authority before sending requests. Choose a user that is already registered, i.e. added to the [membersrvc.yaml](https://github.com/hyperledger/fabric/blob/master/membersrvc/membersrvc.yaml). Then, execute the command below to log in the user on the target validating peer. `CORE_PEER_ADDRESS` specifies the target validating peer for which the user is to be logged in.
   285  
   286     ```
   287     CORE_PEER_ADDRESS=172.17.0.2:7051 peer network login jim
   288     ```
   289  
   290     **Note:** The certificate authority allows the enrollID and enrollSecret credentials to be used only *once*. Therefore, login by the same user from any other validating peer will result in an error. Currently, the application layer is responsible for duplicating the crypto material returned from the CA to other peer nodes. If you want to test secure transactions from more than one peer node without replicating the returned key and certificate, you can log in with a different user on other peer nodes.
   291  
   292     ### Deploy, Invoke, and Query a Chaincode
   293  
   294  
   295     **Note:** When security is enabled, modify the CLI commands to deploy, invoke, or query a chaincode to pass the username of a logged in user. To log in a registered user through the CLI, execute the login command from the section above. On the CLI the username is passed with the -u parameter.
   296  
   297     We can use the sample chaincode to test the network. You may find the chaincode here `$GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02`.
   298  
   299     Deploy the chaincode to the network. We can deploy to any validating peer by specifying `CORE_PEER_ADDRESS`:
   300  
   301     ```
   302     CORE_PEER_ADDRESS=172.17.0.2:7051 peer chaincode deploy -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Function":"init", "Args": ["a","100", "b", "200"]}'
   303     ```
   304  
   305     With security enabled, modify the command as follows:
   306  
   307     ```
   308     CORE_PEER_ADDRESS=172.17.0.2:7051 CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode deploy -u jim -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Function":"init", "Args": ["a","100", "b", "200"]}'
   309     ```
   310  
   311     You can watch for the message "Received build request for chaincode spec" on the output screen of all validating peers.
   312  
   313     **Note:** If your GOPATH environment variable contains more than one element, the chaincode must be found in the first one or deployment will fail.
   314  
   315     On successful completion, the above command will print the "name" assigned to the deployed chaincode. This "name" is used as the value of the "-n" parameter in invoke and query commands described below. For example the value of "name" could be
   316  
   317         bb540edfc1ee2ac0f5e2ec6000677f4cd1c6728046d5e32dede7fea11a42f86a6943b76a8f9154f4792032551ed320871ff7b7076047e4184292e01e3421889c
   318  
   319     In a script the name can be captured for subsequent use. For example, run
   320  
   321         NAME=`CORE_PEER_ADDRESS=172.17.0.2:7051 CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode deploy ...`
   322  
   323     and then replace `<name_value_returned_from_deploy_command>` in the examples below with `$NAME`.
   324  
   325     We can run an invoke transaction to move 10 units from the value of `a` to the value of `b`:
   326  
   327     ```
   328     CORE_PEER_ADDRESS=172.17.0.2:7051 peer chaincode invoke -n <name_value_returned_from_deploy_command> -c '{"Function": "invoke", "Args": ["a", "b", "10"]}'
   329     ```
   330  
   331     With security enabled, modify the command as follows:
   332  
   333     ```
   334     CORE_PEER_ADDRESS=172.17.0.2:7051 CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode invoke -u jim -n <name_value_returned_from_deploy_command> -c '{"Function": "invoke", "Args": ["a", "b", "10"]}'
   335     ```
   336  
   337     We can also run a query to see the current value `a` has:
   338  
   339     ```
   340     CORE_PEER_ADDRESS=172.17.0.2:7051 peer chaincode query -l golang -n <name_value_returned_from_deploy_command> -c '{"Function": "query", "Args": ["a"]}'
   341     ```
   342  
   343     With security enabled, modify the command as follows:
   344  
   345     ```
   346     CORE_PEER_ADDRESS=172.17.0.2:7051 CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode query -u jim -l golang -n <name_value_returned_from_deploy_command> -c '{"Function": "query", "Args": ["a"]}'
   347     ```
   348     -->
   349  
   350  Using a Consensus Plugin
   351  ~~~~~~~~~~~~~~~~~~~~~~~~
   352  
   353  A consensus plugin might require some specific configuration that you
   354  need to set up. For example, to use the Practical Byzantine Fault
   355  Tolerant (PBFT) consensus plugin provided as part of the fabric, perform
   356  the following configuration:
   357  
   358  1. In ``core.yaml``, set the ``peer.validator.consensus`` value to
   359     ``pbft``
   360  2. In ``core.yaml``, make sure the ``peer.id`` is set sequentially as
   361     ``vpN`` where ``N`` is an integer that starts from ``0`` and goes to
   362     ``N-1``. For example, with 4 validating peers, set the ``peer.id``
   363     to\ ``vp0``, ``vp1``, ``vp2``, ``vp3``.
   364  3. In ``consensus/pbft/config.yaml``, set the ``general.mode`` value to
   365     ``batch`` and the ``general.N`` value to the number of validating
   366     peers on the network, also set ``general.batchsize`` to the number of
   367     transactions per batch.
   368  4. In ``consensus/pbft/config.yaml``, optionally set timer values for
   369     the batch period (``general.timeout.batch``), the acceptable delay
   370     between request and execution (``general.timeout.request``), and for
   371     view-change (``general.timeout.viewchange``)
   372  
   373  See ``core.yaml`` and ``consensus/pbft/config.yaml`` for more detail.
   374  
   375  All of these setting may be overridden via the command line environment
   376  variables, e.g. ``CORE_PEER_VALIDATOR_CONSENSUS_PLUGIN=pbft`` or
   377  ``CORE_PBFT_GENERAL_MODE=batch``
   378  
   379  Logging control
   380  ~~~~~~~~~~~~~~~
   381  
   382  See `Logging Control <logging-control.md>`__ for information on
   383  controlling logging output from the ``peer`` and deployed chaincodes.
   384  
   385  .. raw:: html
   386  
   387     <!--
   388     **Note:** When running with security enabled, follow the security setup instructions described in [Chaincode Development](../Setup/Chaincode-setup.md#security-setup-optional) to set up the CA server and log in registered users before sending chaincode transactions. In this case peers started using Docker images need to point to the correct CA address (default is localhost). CA addresses have to be specified in `peer/core.yaml` variables paddr of eca, tca and tlsca. Furthermore, if you are enabling security and privacy on the peer process with environment variables, it is important to include these environment variables in the command when executing all subsequent peer operations (e.g. deploy, invoke, or query).
   389     -->