|
| 1 | +# Install and Instantiate |
| 2 | + |
| 3 | +This tutorial requires the latest builds for `hyperledger/fabric-baseimage`, `hyperledger/fabric-peer` |
| 4 | +and `hyperledger/fabric-orderer`. Rather than pull from docker hub, you can compile |
| 5 | +these images locally to ensure they are up to date. It is up to the user how to build |
| 6 | +the images, although a typical approach is through vagrant. If you do choose to build |
| 7 | +through vagrant, make sure you have followed the steps outlined in |
| 8 | +[setting up the development environment](dev-setup/devenv.md). Then from the |
| 9 | +fabric directory within your vagrant environment, execute the `make peer-docker` |
| 10 | +and `make orderer-docker` commands. |
| 11 | + |
| 12 | +### Start the network of 2 peers, an orderer, and a CLI |
| 13 | +Navigate to the fabric/docs directory in your vagrant environment and start your network: |
| 14 | +```bash |
| 15 | +docker-compose -f docker-2peer.yml up |
| 16 | +``` |
| 17 | +View all your containers: |
| 18 | +```bash |
| 19 | +# active and non-active |
| 20 | +docker ps -a |
| 21 | +``` |
| 22 | + |
| 23 | +### Get into the CLI container |
| 24 | +Now, open a second terminal and navigate once again to your vagrant environment. |
| 25 | +```bash |
| 26 | +docker exec -it cli bash |
| 27 | +``` |
| 28 | +You should see the following in your terminal: |
| 29 | +```bash |
| 30 | +root@ccd3308afc73:/opt/gopath/src/github.com/hyperledger/fabric/peer# |
| 31 | +``` |
| 32 | + |
| 33 | +### Create and join channel from the remote CLI |
| 34 | +From your second terminal, lets create a channel by the name of "myc": |
| 35 | +```bash |
| 36 | +peer channel create -c myc |
| 37 | +``` |
| 38 | +This will generate a genesis block - `myc.block` - and place it into the same directory |
| 39 | +from which you issued your `peer channel create` command. Now, from the same directory, |
| 40 | +direct both peers to join channel - `myc` - by passing in the genesis block |
| 41 | +- `myc.block` - with a `peer channel join` command: |
| 42 | +```bash |
| 43 | +CORE_PEER_ADDRESS=peer0:7051 peer channel join -b myc.block |
| 44 | +CORE_PEER_ADDRESS=peer1:7051 peer channel join -b myc.block |
| 45 | +``` |
| 46 | + |
| 47 | +### Install the chaincode on peer0 from the remote CLI |
| 48 | +From your second terminal, and still within the CLI container, issue the following |
| 49 | +command to install a chaincode named `mycc` with a version of `v0` onto `peer0`. |
| 50 | +```bash |
| 51 | +CORE_PEER_ADDRESS=peer0:7051 peer chaincode install -n mycc -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -v v0 |
| 52 | +``` |
| 53 | + |
| 54 | +### Instantiate the chaincode on the channel from the remote CLI |
| 55 | +Now, still within the cli container in your second terminal, instantiate the chaincode |
| 56 | +`mycc` with version `v0` onto `peer0`. This instantiation will initialize the chaincode |
| 57 | +with key value pairs of ["a","100"] and ["b","200"]. |
| 58 | +```bash |
| 59 | +CORE_PEER_ADDRESS=peer0:7051 peer chaincode instantiate -C myc -n mycc -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -v v0 -c '{"Args":["init","a","100","b","200"]}' |
| 60 | +``` |
| 61 | +__Continue operating within your second terminal for the remainder of the commands__ |
| 62 | + |
| 63 | +### Query for the value of "a" to make sure the chaincode container has successfully started |
| 64 | +Send a query to `peer0` for the value of key `"a"`: |
| 65 | +```bash |
| 66 | +CORE_PEER_ADDRESS=peer0:7051 peer chaincode query -C myc -n mycc -v v0 -c '{"Args":["query","a"]}' |
| 67 | +``` |
| 68 | +This query should return "100". |
| 69 | + |
| 70 | +### Invoke to make a state change |
| 71 | +Send an invoke request to `peer0` to move 10 units from "a" to "b": |
| 72 | +```bash |
| 73 | +CORE_PEER_ADDRESS=peer0:7051 peer chaincode invoke -C myc -n mycc -v v0 -c '{"Args":["invoke","a","b","10"]}' |
| 74 | +``` |
| 75 | + |
| 76 | +### Query on the second peer |
| 77 | +Issue a query against the key "a" to `peer1`. Recall that `peer1` has successfully |
| 78 | +joined the channel. |
| 79 | +```bash |
| 80 | +CORE_PEER_ADDRESS=peer1:7051 peer chaincode query -C myc -n mycc -v v0 -c '{"Args":["query","a"]}' |
| 81 | +``` |
| 82 | +This will return an error response because `peer1` does not have the chaincode installed. |
| 83 | + |
| 84 | +### Install on the second peer |
| 85 | +Now add the chaincode to `peer1` so that you can successfully perform read/write operations. |
| 86 | +```bash |
| 87 | +CORE_PEER_ADDRESS=peer1:7051 peer chaincode install -n mycc -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -v v0 |
| 88 | +``` |
| 89 | +__Note__: The initial instantiation applies to all peers in the channel, and is |
| 90 | +affected upon any peer that has the chaincode installed. Therefore, we installed |
| 91 | +the chaincode on `peer0` in order to execute the instantiate command through it. |
| 92 | +Now that we want to access the chaincode on `peer1`, we must install the chaincode |
| 93 | +on `peer1` as well. In general, a chaincode has to be installed only on those peers |
| 94 | +through which the chaincode needs to be accessed from. In particular, the |
| 95 | +chaincode must be installed on any peer receiving endorsement requests for that chaincode. |
| 96 | + |
| 97 | +### Query on the second peer |
| 98 | +Now issue the same query request to `peer1`. |
| 99 | +```bash |
| 100 | +CORE_PEER_ADDRESS=peer1:7051 peer chaincode query -C myc -n mycc -v v0 -c '{"Args":["query","a"]}' |
| 101 | +``` |
| 102 | +Query will now succeed. |
| 103 | + |
| 104 | +### What does this demonstrate? |
| 105 | +- The ability to invoke (alter key value states) is restricted to peers that have the chaincode installed. |
| 106 | +- Just as state changes due to invoke on a peer affects all peers in the channel, the instantiate on a peer will |
| 107 | +likewise affect all peers in the channel. |
| 108 | +- The world state of the chaincode is available to all peers on the channel - even those that do not have the chaincode installed. |
| 109 | +- Once the chaincode is installed on a peer, invokes and queries can access those states normally. |
0 commit comments