github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/docs/Setup/Network-setup.md (about) 1 ## Setting Up a Network 2 3 This document covers setting up a network on your local machine for various development and testing activities. Unless you are intending to contribute to the development of the Hyperledger Fabric project, you'll probably want to follow the more commonly used approach below - [leveraging published Docker images](#leveraging-published-docker-images) for the various Hyperledger Fabric components, directly. Otherwise, skip down to the [secondary approach](#building-your-own-images) below. 4 5 ### Leveraging published Docker images 6 7 This approach simply leverages the Docker images that the Hyperledger Fabric project publishes to [DockerHub](https://hub.docker.com/u/hyperledger/) and either Docker commands or Docker Compose descriptions of the network one wishes to create. 8 9 #### Installing Docker 10 11 **Note:** When running Docker _natively_ on Mac and Windows, there is no IP forwarding support available. Hence, running more than one fabric-peer image is not advised because you do not want to have multiple processes binding to the same port. For most application and chaincode development/testing running with a single fabric peer should not be an issue unless you are interested in performance and resilience testing the fabric's capabilities, such as consensus. For more advanced testing, we strongly recommend using the fabric's Vagrant [development environment](../dev-setup/devenv.md). 12 13 With this approach, there are multiple choices as to how to run Docker: using [Docker Toolbox](https://docs.docker.com/toolbox/overview/) or one of the new native Docker runtime environments for [Mac OSX](https://docs.docker.com/engine/installation/mac/) or [Windows](https://docs.docker.com/engine/installation/windows/). There are some subtle differences between how Docker runs natively on Mac and Windows versus in a virtualized context on Linux. We'll call those out where appropriate below, when we get to the point of actually running the various components. 14 15 #### Pulling the images from DockerHub 16 17 Once you have Docker (1.11 or greater) installed and running, 18 prior to starting any of the fabric components, you will need to first pull the fabric images from DockerHub. 19 20 ``` 21 docker pull hyperledger/fabric-peer:latest 22 docker pull hyperledger/fabric-membersrvc:latest 23 ``` 24 25 ### Building your own images 26 27 **Note:** _This approach is not necessarily recommended for most users_. If you have pulled images from DockerHub as described in the previous section, you may proceed to the [next step](#starting-up-validating-peers). 28 29 The second approach would be to leverage the [development environment](../dev-setup/devenv.md) setup (which we will assume you have already established) to build and deploy your own binaries and/or Docker images from a clone of the [hyperledger/fabric](https://github.com/hyperledger/fabric) GitHub repository. This approach is suitable for developers that might wish to contribute directly to the Hyperledger Fabric project, or that wish to deploy from a fork of the Hyperledger code base. 30 31 The following commands should be run from _within_ the Vagrant environment described in [Setting Up Development Environment](../dev-setup/devenv.md). 32 33 To create the Docker image for the `hyperledger/fabric-peer`: 34 35 ``` 36 cd $GOPATH/src/github.com/hyperledger/fabric 37 make peer-image 38 ``` 39 40 To create the Docker image for the `hyperledger/fabric-membersrvc`: 41 42 ``` 43 make membersrvc-image 44 ``` 45 46 ### Starting up validating peers 47 48 Check the available images again with `docker images`. You should see `hyperledger/fabric-peer` and `hyperledger/fabric-membersrvc` images. For example, 49 50 ``` 51 $ docker images 52 REPOSITORY TAG IMAGE ID CREATED SIZE 53 hyperledger/fabric-membersrvc latest 7d5f6e0bcfac 12 days ago 1.439 GB 54 hyperledger/fabric-peer latest 82ef20d7507c 12 days ago 1.445 GB 55 ``` 56 If you don't see these, go back to the previous step. 57 58 With the relevant Docker images in hand, we can start running the peer and membersrvc services. 59 60 #### Determine value for CORE_VM_ENDPOINT variable 61 62 Next, we need to determine the address of your docker daemon for the CORE_VM_ENDPOINT. If you are working within the Vagrant development environment, or a Docker Toolbox environment, you can determine this with the `ip add` command. For example, 63 64 ``` 65 $ ip add 66 67 <<< detail removed >>> 68 69 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 70 link/ether 02:42:ad:be:70:cb brd ff:ff:ff:ff:ff:ff 71 inet 172.17.0.1/16 scope global docker0 72 valid_lft forever preferred_lft forever 73 inet6 fe80::42:adff:febe:70cb/64 scope link 74 valid_lft forever preferred_lft forever 75 ``` 76 77 Your output might contain something like `inet 172.17.0.1/16 scope global docker0`. That means the docker0 interface is on IP address 172.17.0.1. Use that IP address for the `CORE_VM_ENDPOINT` option. For more information on the environment variables, see `core.yaml` configuration file in the `fabric` repository. 78 79 If you are using the native Docker for Mac or Windows, the value for `CORE_VM_ENDPOINT` should be set to `unix:///var/run/docker.sock`. \[TODO] double check this. I believe that `127.0.0.1:2375` also works. 80 81 #### Assigning a value for CORE_PEER_ID 82 83 The ID value of `CORE_PEER_ID` must be unique for each validating peer, and it must be a lowercase string. We often use a convention of naming the validating peers vpN where N is an integer starting with 0 for the root node and incrementing N by 1 for each additional peer node started. e.g. vp0, vp1, vp2, ... 84 85 #### Consensus 86 87 By default, we are using a consensus plugin called `NOOPS`, which doesn't really do consensus. If you are running a single peer node, running anything other than `NOOPS` makes little sense. If you want to use some other consensus plugin in the context of multiple peer nodes, please see the [Using a Consensus Plugin](#using-a-consensus-plugin) section, below. 88 89 #### Docker Compose 90 91 We'll be using Docker Compose to launch our various Fabric component containers, as this is the simplest approach. You should have it installed from the initial setup steps. Installing Docker Toolbox or any of the native Docker runtimes should have installed Compose. 92 93 #### Start up a validating peer: 94 95 Let's launch the first validating peer (the root node). We'll set CORE_PEER_ID to vp0 and CORE_VM_ENDPOINT as above. Here's the docker-compose.yml for launching a single container within the **Vagrant** [development environment](../dev-setup/devenv.md): 96 97 ``` 98 vp0: 99 image: hyperledger/fabric-peer 100 environment: 101 - CORE_PEER_ID=vp0 102 - CORE_PEER_ADDRESSAUTODETECT=true 103 - CORE_VM_ENDPOINT=http://172.17.0.1:2375 104 - CORE_LOGGING_LEVEL=DEBUG 105 command: peer node start 106 ``` 107 You can launch this Compose file as follows, from the same directory as the docker-compose.yml file: 108 109 ``` 110 $ docker-compose up 111 ``` 112 113 Here's the corresponding Docker command: 114 ``` 115 $ 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 116 ``` 117 118 If you are running Docker for Mac or Windows, we'll need to explicitly map the ports, and we will need a different value for CORE_VM_ENDPOINT as we discussed above. 119 120 Here's the docker-compose.yml for Docker on Mac or Windows: 121 122 ``` 123 vp0: 124 image: hyperledger/fabric-peer 125 ports: 126 - "7050:7050" 127 - "7051:7051" 128 - "7052:7052" 129 environment: 130 - CORE_PEER_ADDRESSAUTODETECT=true 131 - CORE_VM_ENDPOINT=unix:///var/run/docker.sock 132 - CORE_LOGGING_LEVEL=DEBUG 133 command: peer node start 134 ``` 135 136 This single peer configuration, running the `NOOPS` 'consensus' plugin, should satisfy many development/test scenarios. `NOOPS` is not really providing consensus, it is essentially a no-op that simulates consensus. For instance, if you are simply developing and testing chaincode; this should be adequate unless your chaincode is leveraging membership services for identity, access control, confidentiality and privacy. 137 138 #### Running with the CA 139 140 If you want to take advantage of security (authentication and authorization), privacy and confidentiality, then you'll need to run the Fabric's certificate authority (CA). Please refer to the [CA Setup](ca-setup.md) instructions. 141 142 #### Start up additional validating peers: 143 144 Following the pattern we established [above](#assigning-a-value-for-core_peer_id) we'll use `vp1` as the ID for the second validating peer. If using Docker Compose, we can simply link the two peer nodes. 145 Here's the docker-compose.yml for a **Vagrant** environment with two peer nodes - vp0 and vp1: 146 ``` 147 vp0: 148 image: hyperledger/fabric-peer 149 environment: 150 - CORE_PEER_ADDRESSAUTODETECT=true 151 - CORE_VM_ENDPOINT=http://172.17.0.1:2375 152 - CORE_LOGGING_LEVEL=DEBUG 153 command: peer node start 154 vp1: 155 extends: 156 service: vp0 157 environment: 158 - CORE_PEER_ID=vp1 159 - CORE_PEER_DISCOVERY_ROOTNODE=vp0:7051 160 links: 161 - vp0 162 ``` 163 164 If we wanted to use the docker command line to launch another peer, we need to get the IP address of the first validating peer, which will act as the root node to which the new peer(s) will connect. The address is printed out on the terminal window of the first peer (e.g. 172.17.0.2) and should be passed in with the `CORE_PEER_DISCOVERY_ROOTNODE` environment variable. 165 166 ``` 167 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 168 ``` 169 <!-- This needs to be sorted out with a revamped security section 170 171 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). 172 173 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). 174 175 ### Enroll/Login a test user (if security is enabled): 176 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. 177 178 ``` 179 CORE_PEER_ADDRESS=172.17.0.2:7051 peer network login jim 180 ``` 181 182 **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. 183 184 ### Deploy, Invoke, and Query a Chaincode 185 186 187 **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. 188 189 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`. 190 191 Deploy the chaincode to the network. We can deploy to any validating peer by specifying `CORE_PEER_ADDRESS`: 192 193 ``` 194 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"]}' 195 ``` 196 197 With security enabled, modify the command as follows: 198 199 ``` 200 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"]}' 201 ``` 202 203 You can watch for the message "Received build request for chaincode spec" on the output screen of all validating peers. 204 205 **Note:** If your GOPATH environment variable contains more than one element, the chaincode must be found in the first one or deployment will fail. 206 207 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 208 209 bb540edfc1ee2ac0f5e2ec6000677f4cd1c6728046d5e32dede7fea11a42f86a6943b76a8f9154f4792032551ed320871ff7b7076047e4184292e01e3421889c 210 211 In a script the name can be captured for subsequent use. For example, run 212 213 NAME=`CORE_PEER_ADDRESS=172.17.0.2:7051 CORE_SECURITY_ENABLED=true CORE_SECURITY_PRIVACY=true peer chaincode deploy ...` 214 215 and then replace `<name_value_returned_from_deploy_command>` in the examples below with `$NAME`. 216 217 We can run an invoke transaction to move 10 units from the value of `a` to the value of `b`: 218 219 ``` 220 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"]}' 221 ``` 222 223 With security enabled, modify the command as follows: 224 225 ``` 226 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"]}' 227 ``` 228 229 We can also run a query to see the current value `a` has: 230 231 ``` 232 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"]}' 233 ``` 234 235 With security enabled, modify the command as follows: 236 237 ``` 238 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"]}' 239 ``` 240 --> 241 242 ### Using a Consensus Plugin 243 A consensus plugin might require some specific configuration that you need to set up. For example, to use the Practical Byzantine Fault Tolerant (PBFT) consensus plugin provided as part of the fabric, perform the following configuration: 244 245 1. In `core.yaml`, set the `peer.validator.consensus` value to `pbft` 246 2. In `core.yaml`, make sure the `peer.id` is set sequentially as `vpN` where `N` is an integer that starts from `0` and goes to `N-1`. For example, with 4 validating peers, set the `peer.id` to`vp0`, `vp1`, `vp2`, `vp3`. 247 3. In `consensus/pbft/config.yaml`, set the `general.mode` value to `batch` and the `general.N` value to the number of validating peers on the network, also set `general.batchsize` to the number of transactions per batch. 248 4. In `consensus/pbft/config.yaml`, optionally set timer values for the batch period (`general.timeout.batch`), the acceptable delay between request and execution (`general.timeout.request`), and for view-change (`general.timeout.viewchange`) 249 250 See `core.yaml` and `consensus/pbft/config.yaml` for more detail. 251 252 All of these setting may be overridden via the command line environment variables, e.g. `CORE_PEER_VALIDATOR_CONSENSUS_PLUGIN=pbft` or `CORE_PBFT_GENERAL_MODE=batch` 253 254 ### Logging control 255 256 See [Logging Control](logging-control.md) for information on controlling 257 logging output from the `peer` and deployed chaincodes. 258 259 <!-- 260 **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). 261 -->