github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/docs/source/install_instantiate.rst (about)

     1  Install and Instantiate
     2  =======================
     3  
     4  This tutorial requires the latest builds for
     5  ``hyperledger/fabric-baseimage``, ``hyperledger/fabric-peer`` and
     6  ``hyperledger/fabric-orderer``. Rather than pull from Docker Hub, you
     7  can compile these images locally to ensure they are up to date. It is up
     8  to the user how to build the images, although a typical approach is
     9  through vagrant. If you do choose to build through vagrant, make sure
    10  you have followed the steps outlined in `setting up the development
    11  environment <dev-setup/devenv.html>`__. Then from the fabric directory
    12  within your vagrant environment, execute the ``make peer-docker`` and
    13  ``make orderer-docker`` commands.
    14  
    15  Start the network of 2 peers, an orderer, and a CLI
    16  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    17  
    18  Navigate to the fabric/docs directory in your vagrant environment and
    19  start your network:
    20  
    21  .. code:: bash
    22  
    23      docker-compose -f docker-2peer.yml up
    24  
    25  View all your containers:
    26  
    27  .. code:: bash
    28  
    29      # active and non-active
    30      docker ps -a
    31  
    32  Get into the CLI container
    33  ~~~~~~~~~~~~~~~~~~~~~~~~~~
    34  
    35  Now, open a second terminal and navigate once again to your vagrant
    36  environment.
    37  
    38  .. code:: bash
    39  
    40      docker exec -it cli bash
    41  
    42  You should see the following in your terminal:
    43  
    44  .. code:: bash
    45  
    46      root@ccd3308afc73:/opt/gopath/src/github.com/hyperledger/fabric/peer#
    47  
    48  Create and join channel from the remote CLI
    49  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    50  
    51  From your second terminal, lets create a channel by the name of "myc":
    52  
    53  .. code:: bash
    54  
    55      peer channel create -c myc -o orderer:5005
    56  
    57  This will generate a genesis block - ``myc.block`` - and place it into
    58  the same directory from which you issued your ``peer channel create``
    59  command. Now, from the same directory, direct both peers to join channel
    60  - ``myc`` - by passing in the genesis block - ``myc.block`` - with a
    61  ``peer channel join`` command:
    62  
    63  .. code:: bash
    64  
    65      CORE_PEER_ADDRESS=peer0:7051 peer channel join -b myc.block
    66      CORE_PEER_ADDRESS=peer1:7051 peer channel join -b myc.block
    67  
    68  Install the chaincode on peer0 from the remote CLI
    69  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    70  
    71  From your second terminal, and still within the CLI container, issue the
    72  following command to install a chaincode named ``mycc`` with a version
    73  of ``v0`` onto ``peer0``.
    74  
    75  .. code:: bash
    76  
    77      CORE_PEER_ADDRESS=peer0:7051 peer chaincode install -n mycc -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -v v0
    78  
    79  Instantiate the chaincode on the channel from the remote CLI
    80  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    81  
    82  Now, still within the cli container in your second terminal, instantiate
    83  the chaincode ``mycc`` with version ``v0`` onto ``peer0``. This
    84  instantiation will initialize the chaincode with key value pairs of
    85  ["a","100"] and ["b","200"].
    86  
    87  .. code:: bash
    88  
    89      CORE_PEER_ADDRESS=peer0:7051 peer chaincode instantiate -o orderer:5005 -C myc -n mycc -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -v v0 -c '{"Args":["init","a","100","b","200"]}'
    90  
    91  **Continue operating within your second terminal for the remainder of
    92  the commands**
    93  
    94  Query for the value of "a" to make sure the chaincode container has successfully started
    95  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    96  
    97  Send a query to ``peer0`` for the value of key ``"a"``:
    98  
    99  .. code:: bash
   100  
   101      CORE_PEER_ADDRESS=peer0:7051 peer chaincode query -C myc -n mycc -v v0 -c '{"Args":["query","a"]}'
   102  
   103  This query should return "100".
   104  
   105  Invoke to make a state change
   106  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   107  
   108  Send an invoke request to ``peer0`` to move 10 units from "a" to "b":
   109  
   110  .. code:: bash
   111  
   112      CORE_PEER_ADDRESS=peer0:7051 peer chaincode invoke -C myc -n mycc -v v0 -c '{"Args":["invoke","a","b","10"]}'
   113  
   114  Query on the second peer
   115  ~~~~~~~~~~~~~~~~~~~~~~~~
   116  
   117  Issue a query against the key "a" to ``peer1``. Recall that ``peer1``
   118  has successfully joined the channel.
   119  
   120  .. code:: bash
   121  
   122      CORE_PEER_ADDRESS=peer1:7051 peer chaincode query -C myc -n mycc -v v0 -c '{"Args":["query","a"]}'
   123  
   124  This will return an error response because ``peer1`` does not have the
   125  chaincode installed.
   126  
   127  Install on the second peer
   128  ~~~~~~~~~~~~~~~~~~~~~~~~~~
   129  
   130  Now add the chaincode to ``peer1`` so that you can successfully perform
   131  read/write operations.
   132  
   133  .. code:: bash
   134  
   135      CORE_PEER_ADDRESS=peer1:7051 peer chaincode install -n mycc -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -v v0
   136  
   137  | **Note**: The initial instantiation applies to all peers in the
   138    channel, and is affected upon any peer that has the chaincode
   139    installed. Therefore, we installed the chaincode on ``peer0`` in order
   140    to execute the instantiate command through it.
   141  | Now that we want to access the chaincode on ``peer1``, we must install
   142    the chaincode on ``peer1`` as well. In general, a chaincode has to be
   143    installed only on those peers through which the chaincode needs to be
   144    accessed from. In particular, the chaincode must be installed on any
   145    peer receiving endorsement requests for that chaincode.
   146  
   147  Query on the second peer
   148  ~~~~~~~~~~~~~~~~~~~~~~~~
   149  
   150  Now issue the same query request to ``peer1``.
   151  
   152  .. code:: bash
   153  
   154      CORE_PEER_ADDRESS=peer1:7051 peer chaincode query -C myc -n mycc -v v0 -c '{"Args":["query","a"]}'
   155  
   156  Query will now succeed.
   157  
   158  What does this demonstrate?
   159  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
   160  
   161  -  The ability to invoke (alter key value states) is restricted to peers
   162     that have the chaincode installed.
   163  -  Just as state changes due to invoke on a peer affects all peers in
   164     the channel, the instantiate on a peer will likewise affect all peers
   165     in the channel.
   166  -  The world state of the chaincode is available to all peers on the
   167     channel - even those that do not have the chaincode installed.
   168  -  Once the chaincode is installed on a peer, invokes and queries can
   169     access those states normally.
   170  
   171  .. Licensed under Creative Commons Attribution 4.0 International License
   172     https://creativecommons.org/licenses/by/4.0/
   173