github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/examples/e2e_cli/end-to-end.rst (about) 1 End-to-End Flow 2 =============== 3 4 The end-to-end verification demonstrates usage of the configuration 5 transaction tool for orderer bootstrap and channel creation. The tool 6 consumes a configuration transaction yaml file, which defines the 7 cryptographic material (certs) used for member identity and network 8 authentication. In other words, the MSP information for each member and 9 its corresponding network entities. 10 11 *Currently the crypto material is baked into the directory. However, 12 there will be a tool in the near future to organically generate the 13 certificates* 14 15 Prerequisites 16 ------------- 17 18 - Follow the steps for setting up a `development 19 environment <http://hyperledger-fabric.readthedocs.io/en/latest/dev-setup/devenv.html>`__ 20 21 - Clone the Fabric code base. 22 23 .. code:: bash 24 25 git clone http://gerrit.hyperledger.org/r/fabric 26 27 or though a mirrored repository in github: 28 29 :: 30 31 git clone https://github.com/hyperledger/fabric.git 32 33 - Make the ``configtxgen`` tool. 34 35 .. code:: bash 36 37 cd $GOPATH/src/github.com/hyperledger/fabric/devenv 38 vagrant up 39 vagrant ssh 40 # ensure sure you are in the /fabric directory where the Makefile resides 41 make configtxgen 42 43 - Make the peer and orderer images. 44 45 .. code:: bash 46 47 # make sure you are in vagrant and in the /fabric directory 48 make docker 49 50 Execute a ``docker images`` command in your terminal. If the images 51 compiled successfully, you should see an output similar to the 52 following: 53 54 .. code:: bash 55 56 vagrant@hyperledger-devenv:v0.3.0-4eec836:/opt/gopath/src/github.com/hyperledger/fabric$ docker images 57 REPOSITORY TAG IMAGE ID CREATED SIZE 58 hyperledger/fabric-orderer latest 264e45897bfb 10 minutes ago 180 MB 59 hyperledger/fabric-orderer x86_64-0.7.0-snapshot-a0d032b 264e45897bfb 10 minutes ago 180 MB 60 hyperledger/fabric-peer latest b3d44cff07c6 10 minutes ago 184 MB 61 hyperledger/fabric-peer x86_64-0.7.0-snapshot-a0d032b b3d44cff07c6 10 minutes ago 184 MB 62 hyperledger/fabric-javaenv latest 6e2a2adb998a 10 minutes ago 1.42 GB 63 hyperledger/fabric-javaenv x86_64-0.7.0-snapshot-a0d032b 6e2a2adb998a 10 minutes ago 1.42 GB 64 hyperledger/fabric-ccenv latest 0ce0e7dc043f 12 minutes ago 1.29 GB 65 hyperledger/fabric-ccenv x86_64-0.7.0-snapshot-a0d032b 0ce0e7dc043f 12 minutes ago 1.29 GB 66 hyperledger/fabric-baseimage x86_64-0.3.0 f4751a503f02 4 weeks ago 1.27 GB 67 hyperledger/fabric-baseos x86_64-0.3.0 c3a4cf3b3350 4 weeks ago 161 MB 68 69 Configuration Transaction Generator 70 ----------------------------------- 71 72 The `configtxgen 73 tool <https://github.com/hyperledger/fabric/blob/master/docs/source/configtxgen.rst>`__ 74 is used to create two artifacts: - orderer **bootstrap block** - fabric 75 **channel configuration transaction** 76 77 The orderer block is the genesis block for the ordering service, and the 78 channel transaction file is broadcast to the orderer at channel creation 79 time. 80 81 The ``configtx.yaml`` contains the definitions for the sample network. 82 There are two members, each managing and maintaining two peer nodes. 83 Inspect this file to better understand the corresponding cryptographic 84 material tied to the member components. The ``/crypto`` directory 85 contains the admin certs, ca certs, private keys for each entity, and 86 the signing certs for each entity. 87 88 For ease of use, a script - ``generateCfgTrx.sh`` - is provided. The 89 script will generate the two configuration artifacts. 90 91 Run the shell script 92 ^^^^^^^^^^^^^^^^^^^^ 93 94 Make sure you are in the ``examples/e2e_cli`` directory and in your 95 vagrant environment. You can elect to pass in a unique name for your 96 channel or simply execute the script without the ``channel-ID`` 97 parameter. If you choose not to pass in a unique name, then a channel 98 with the default name of ``mychannel`` will be generated. 99 100 .. code:: bash 101 102 cd examples/e2e_cli 103 # note the <channel-ID> parm is optional 104 ./generateCfgTrx.sh <channel-ID> 105 106 After you run the shell script, you should see an output in your 107 terminal similar to the following: 108 109 .. code:: bash 110 111 2017/02/28 17:01:52 Generating new channel configtx 112 2017/02/28 17:01:52 Creating no-op MSP instance 113 2017/02/28 17:01:52 Obtaining default signing identity 114 2017/02/28 17:01:52 Creating no-op signing identity instance 115 2017/02/28 17:01:52 Serializing identity 116 2017/02/28 17:01:52 signing message 117 2017/02/28 17:01:52 signing message 118 2017/02/28 17:01:52 Writing new channel tx 119 120 These configuration transactions will bundle the crypto material for the 121 participating members and their network components and output an orderer 122 genesis block and channel transaction artifact. These two artifacts are 123 required for a functioning transactional network with 124 sign/verify/authenticate capabilities. 125 126 Manually generate the artifacts (optional) 127 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 128 129 In your vagrant environment, navigate to the ``/sampleconfig`` 130 directory and replace the ``configtx.yaml`` file with the supplied yaml 131 file in the ``/e2e_cli`` directory. Then return to the ``/e2e_cli`` 132 directory. 133 134 .. code:: bash 135 136 # Generate orderer bootstrap block 137 configtxgen -profile TwoOrgs -outputBlock <block-name> 138 # example: configtxgen -profile TwoOrgs -outputBlock orderer.block 139 140 # Generate channel configuration transaction 141 configtxgen -profile TwoOrgs -outputCreateChannelTx <cfg txn name> -channelID <channel-id> 142 # example: configtxgen -profile TwoOrgs -outputCreateChannelTx channel.tx -channelID mychannel 143 144 Run the end-to-end test with Docker 145 ----------------------------------- 146 147 Make sure you are in the ``/e2e_cli`` directory. Then use docker-compose 148 to spawn the network entities and drive the tests. 149 150 .. code:: bash 151 152 [CHANNEL_NAME=<channel-id>] docker-compose up -d 153 154 If you created a unique channel name, be sure to pass in that parameter. 155 For example, 156 157 .. code:: bash 158 159 CHANNEL_NAME=abc docker-compose up -d 160 161 Wait, 30 seconds. Behind the scenes, there are transactions being sent 162 to the peers. Execute a ``docker ps`` to view your active containers. 163 You should see an output identical to the following: 164 165 .. code:: bash 166 167 vagrant@hyperledger-devenv:v0.3.0-4eec836:/opt/gopath/src/github.com/hyperledger/fabric/examples/e2e_cli$ docker ps 168 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 169 45e3e114f7a2 dev-peer3-mycc-1.0 "chaincode -peer.a..." 4 seconds ago Up 4 seconds dev-peer3-mycc-1.0 170 5970f740ad2b dev-peer0-mycc-1.0 "chaincode -peer.a..." 24 seconds ago Up 23 seconds dev-peer0-mycc-1.0 171 b84808d66e99 dev-peer2-mycc-1.0 "chaincode -peer.a..." 48 seconds ago Up 47 seconds dev-peer2-mycc-1.0 172 16d7d94c8773 hyperledger/fabric-peer "peer node start -..." About a minute ago Up About a minute 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp peer3 173 3561a99e35e6 hyperledger/fabric-peer "peer node start -..." About a minute ago Up About a minute 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp peer2 174 0baad3047d92 hyperledger/fabric-peer "peer node start -..." About a minute ago Up About a minute 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp peer1 175 1216896b7b4f hyperledger/fabric-peer "peer node start -..." About a minute ago Up About a minute 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0 176 155ff8747b4d hyperledger/fabric-orderer "orderer" About a minute ago Up About a minute 0.0.0.0:7050->7050/tcp orderer 177 178 All in one 179 ^^^^^^^^^^ 180 181 You can also generate the artifacts and drive the tests using a single 182 shell script. The ``configtxgen`` and ``docker-compose`` commands are 183 embedded in the script. 184 185 .. code:: bash 186 187 ./network_setup.sh up <channel-ID> 188 189 Once again, if you choose not to pass the ``channel-ID`` parameter, then 190 your channel will default to ``mychannel``. 191 192 What's happening behind the scenes? 193 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 194 195 - A script - ``script.sh`` - is baked inside the CLI container. The 196 script drives the ``createChannel`` command against the default 197 ``mychannel`` name. 198 199 - The output of ``createChannel`` is a genesis block - 200 ``mychannel.block`` - which is stored on the file system. 201 202 - the ``joinChannel`` command is exercised for all four peers who will 203 pass in the genesis block. 204 205 - Now we have a channel consisting of four peers, and two 206 organizations. 207 208 - ``PEER0`` and ``PEER1`` belong to Org0; ``PEER2`` and ``PEER3`` 209 belong to Org1 210 211 - Recall that these relationships are defined in the ``configtx.yaml`` 212 213 - A chaincode - *chaincode\_example02* is installed on ``PEER0`` and 214 ``PEER2`` 215 216 - The chaincode is then "instantiated" on ``PEER2``. Instantiate simply 217 refers to starting the container and initializing the key value pairs 218 associated with the chaincode. The initial values for this example 219 are "a","100" "b","200". This "instantiation" results in a container 220 by the name of ``dev-peer2-mycc-1.0`` starting. 221 222 - The instantiation also passes in an argument for the endorsement 223 policy. The policy is defined as 224 ``-P "OR ('Org0MSP.member','Org1MSP.member')"``, meaning that any 225 transaction must be endorsed by a peer tied to Org0 or Org1. 226 227 - A query against the value of "a" is issued to ``PEER0``. The 228 chaincode was previously installed on ``PEER0``, so this will start 229 another container by the name of ``dev-peer0-mycc-1.0``. The result 230 of the query is also returned. No write operations have occurred, so 231 a query against "a" will still return a value of "100" 232 233 - An invoke is sent to ``PEER0`` to move "10" from "a" to "b" 234 235 - The chaincode is installed on ``PEER3`` 236 237 - A query is sent to ``PEER3`` for the value of "a". This starts a 238 third chaincode container by the name of ``dev-peer3-mycc-1.0``. A 239 value of 90 is returned, correctly reflecting the previous 240 transaction during which the value for key "a" was modified by 10. 241 242 What does this demonstrate? 243 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 244 245 Chaincode **MUST** be installed on a peer in order for it to 246 successfully perform read/write operations against the ledger. 247 Furthermore, a chaincode container is not started for a peer until a 248 read/write operation is performed against that chaincode (e.g. query for 249 the value of "a"). The transaction causes the container to start. Also, 250 all peers in a channel maintain an exact copy of the ledger which 251 comprises the blockchain to store the immutable, sequenced record in 252 blocks, as well as a state database to maintain current fabric state. 253 This includes those peers that do not have chaincode installed on them 254 (like ``Peer1`` in the above example) . Finally, the chaincode is accessible 255 after it is installed (like ``Peer3`` in the above example) because it 256 already has been instantiated. 257 258 How do I see these transactions? 259 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 260 261 Check the logs for the CLI docker container. 262 263 :: 264 265 docker logs -f cli 266 267 You should see the following output: 268 269 .. code:: bash 270 271 2017-02-28 04:31:20.841 UTC [logging] InitFromViper -> DEBU 001 Setting default logging level to DEBUG for command 'chaincode' 272 2017-02-28 04:31:20.842 UTC [msp] GetLocalMSP -> DEBU 002 Returning existing local MSP 273 2017-02-28 04:31:20.842 UTC [msp] GetDefaultSigningIdentity -> DEBU 003 Obtaining default signing identity 274 2017-02-28 04:31:20.843 UTC [msp] Sign -> DEBU 004 Sign: plaintext: 0A8F050A59080322096D796368616E6E...6D7963631A0A0A0571756572790A0161 275 2017-02-28 04:31:20.843 UTC [msp] Sign -> DEBU 005 Sign: digest: 52F1A41B7B0B08CF3FC94D9D7E916AC4C01C54399E71BC81D551B97F5619AB54 276 Query Result: 90 277 2017-02-28 04:31:30.425 UTC [main] main -> INFO 006 Exiting..... 278 ===================== Query on chaincode on PEER3 on channel 'mychannel' is successful ===================== 279 280 ===================== All GOOD, End-2-End execution completed ===================== 281 282 How can I see the chaincode logs? 283 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 284 285 Inspect the individual chaincode containers to see the separate 286 transactions executed against each container. Here is the combined 287 output from each container: 288 289 .. code:: bash 290 291 $ docker logs dev-peer2-mycc-1.0 292 04:30:45.947 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW] 293 ex02 Init 294 Aval = 100, Bval = 200 295 296 $ docker logs dev-peer0-mycc-1.0 297 04:31:10.569 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW] 298 ex02 Invoke 299 Query Response:{"Name":"a","Amount":"100"} 300 ex02 Invoke 301 Aval = 90, Bval = 210 302 303 $ docker logs dev-peer3-mycc-1.0 304 04:31:30.420 [BCCSP_FACTORY] DEBU : Initialize BCCSP [SW] 305 ex02 Invoke 306 Query Response:{"Name":"a","Amount":"90"} 307 308 Run the end-to-end test manually with Docker 309 -------------------------------------------- 310 311 From your vagrant environment exit the currently running containers: 312 313 .. code:: bash 314 315 docker rm -f $(docker ps -aq) 316 317 Execute a ``docker images`` command in your terminal to view the 318 chaincode images. They will look similar to the following: 319 320 .. code:: bash 321 322 REPOSITORY TAG IMAGE ID CREATED SIZE 323 dev-peer3-mycc-1.0 latest 3415bc2e146c 5 hours ago 176 MB 324 dev-peer0-mycc-1.0 latest 140d7ee3e911 5 hours ago 176 MB 325 dev-peer2-mycc-1.0 latest 6e4fc412969e 5 hours ago 176 MB 326 327 Remove these images: 328 329 .. code:: bash 330 331 docker rmi <IMAGE ID> <IMAGE ID> <IMAGE ID> 332 333 For example: 334 335 .. code:: bash 336 337 docker rmi -f 341 140 6e4 338 339 Ensure you have the configuration artifacts. If you deleted them, run 340 the shell script again: 341 342 .. code:: bash 343 344 ./generateCfgTrx.sh <channel-ID> 345 346 Modify the docker-compose file 347 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 348 349 Open the docker-compose file and comment out the command to run 350 ``script.sh``. Navigate down to the cli image and place a ``#`` to the 351 left of the command. For example: 352 353 .. code:: bash 354 355 working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer 356 # command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}' 357 358 Save the file and return to the ``/e2e_cli`` directory. 359 360 Now restart your network: 361 362 .. code:: bash 363 364 # make sure you are in the /e2e_cli directory where you docker-compose script resides 365 docker-compose up -d 366 367 Command syntax 368 ^^^^^^^^^^^^^^ 369 370 Refer to the create and join commands in the ``script.sh``. 371 372 For the following CLI commands against `peer0` to work, you need to set the 373 values for four environment variables, given below. Please make sure to override 374 the values accordingly when calling commands against other peers and the 375 orderer. 376 377 .. code:: bash 378 379 # Environment variables for PEER0 380 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig 381 CORE_PEER_ADDRESS=peer0:7051 382 CORE_PEER_LOCALMSPID="Org0MSP" 383 CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peer/peer0/localMspConfig/cacerts/peerOrg0.pem 384 385 These environment variables for each peer are defined in the supplied 386 docker-compose file. 387 388 Create channel 389 ^^^^^^^^^^^^^^ 390 391 Exec into the cli container: 392 393 .. code:: bash 394 395 docker exec -it cli bash 396 397 If successful you should see the following: 398 399 .. code:: bash 400 401 root@0d78bb69300d:/opt/gopath/src/github.com/hyperledger/fabric/peer# 402 403 Specify your channel name with the ``-c`` flag. Specify your channel 404 configuration transaction with the ``-f`` flag. In this case it is 405 ``channeltx``, however you can mount your own configuration transaction 406 with a different name. 407 408 .. code:: bash 409 410 # the channel.tx and orderer.block are mounted in the crypto/orderer folder within your cli container 411 # as a result, we pass the full path for the file 412 peer channel create -o orderer0:7050 -c mychannel -f crypto/orderer/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem 413 414 Since the `channel create` runs against the orderer, we need to override the 415 four environment variables set before. So the above command in its entirety would be: 416 417 .. code:: bash 418 419 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig CORE_PEER_LOCALMSPID="OrdererMSP" peer channel create -o orderer0:7050 -c mychannel -f crypto/orderer/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem 420 421 422 **Note**: You will remain in the CLI container for the remainder of 423 these manual commands. You must also remember to preface all commands 424 with the corresponding environment variables for targetting a peer other than 425 `peer0`. 426 427 Join channel 428 ^^^^^^^^^^^^ 429 430 Join specific peers to the channel 431 432 .. code:: bash 433 434 # By default, this joins PEER0 only 435 # the mychannel.block is also mounted in the crypto/orderer directory 436 peer channel join -b mychannel.block 437 438 You can make other peers join the channel as necessary by making appropriate 439 changes in the four environment variables. 440 441 Install chaincode onto a remote peer 442 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 443 444 Install the sample go code onto one of the four peer nodes 445 446 .. code:: bash 447 448 peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 449 450 Instantiate chaincode and define the endorsement policy 451 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 452 453 Instantiate the chaincode on a peer. This will launch a chaincode 454 container for the targeted peer and set the endorsement policy for the 455 chaincode. In this snippet, we define the policy as requiring an 456 endorsement from one peer node that is a part of either `Org0` or `Org1`. 457 The command is: 458 459 .. code:: bash 460 461 peer chaincode instantiate -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Args":["init","a", "100", "b","200"]}' -P "OR ('Org0MSP.member','Org1MSP.member')" 462 463 See the `endorsement 464 policies <http://hyperledger-fabric.readthedocs.io/en/latest/endorsement-policies.html>`__ 465 documentation for more details on policy implementation. 466 467 Invoke chaincode 468 ^^^^^^^^^^^^^^^^ 469 470 .. code:: bash 471 472 peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}' 473 474 **NOTE**: Make sure to wait a few seconds for the operation to complete. 475 476 Query chaincode 477 ^^^^^^^^^^^^^^^ 478 479 .. code:: bash 480 481 peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' 482 483 The result of the above command should be as below: 484 485 .. code:: bash 486 487 Query Result: 90 488 489 Run the end-to-end test using the native binaries 490 ------------------------------------------------- 491 492 Open your vagrant environment: 493 494 .. code:: bash 495 496 cd $GOPATH/src/github.com/hyperledger/fabric/devenv 497 498 .. code:: bash 499 500 # you may have to first start your VM with vagrant up 501 vagrant ssh 502 503 From the ``fabric`` directory build the issue the following commands to 504 build the peer and orderer executables: 505 506 .. code:: bash 507 508 make clean 509 make native 510 511 You will also need the ``ccenv`` image. From the ``fabric`` directory: 512 513 .. code:: bash 514 515 make peer-docker 516 517 Next, open two more terminals and start your vagrant environment in 518 each. You should now have a total of three terminals, all within 519 vagrant. 520 521 Before starting, make sure to clear your ledger folder 522 ``/var/hyperledger/``. You will want to do this after each run to avoid 523 errors and duplication. 524 525 :: 526 527 rm -rf /var/hyperledger/* 528 529 **Vagrant window 1** 530 531 Use the ``configtxgen`` tool to create the orderer genesis block: 532 533 .. code:: bash 534 535 configtxgen -profile SampleSingleMSPSolo -outputBlock orderer.block 536 537 **Vagrant window 2** 538 539 Start the orderer with the genesis block you just generated: 540 541 .. code:: bash 542 543 ORDERER_GENERAL_GENESISMETHOD=file ORDERER_GENERAL_GENESISFILE=./orderer.block orderer 544 545 **Vagrant window 1** 546 547 Create the channel configuration transaction: 548 549 .. code:: bash 550 551 configtxgen -profile SampleSingleMSPSolo -outputCreateChannelTx channel.tx -channelID <channel-ID> 552 553 This will generate a ``channel.tx`` file in your current directory 554 555 **Vagrant window 3** 556 557 Start the peer in *"chainless"* mode 558 559 .. code:: bash 560 561 peer node start --peer-defaultchain=false 562 563 **Note**: Use Vagrant window 1 for the remainder of commands 564 565 Create channel 566 ^^^^^^^^^^^^^^ 567 568 Ask peer to create a channel with the configuration parameters in 569 ``channel.tx`` 570 571 .. code:: bash 572 573 peer channel create -o 127.0.0.1:7050 -c mychannel -f channel.tx 574 575 This will return a channel genesis block - ``mychannel.block`` - in your 576 current directory. 577 578 Join channel 579 ^^^^^^^^^^^^ 580 581 Ask peer to join the channel by passing in the channel genesis block: 582 583 .. code:: bash 584 585 peer channel join -b mychannel.block 586 587 Install 588 ^^^^^^^ 589 590 Install chaincode on the peer: 591 592 .. code:: bash 593 594 peer chaincode install -o 127.0.0.1:7050 -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 595 596 Make sure the chaincode is in the filesystem: 597 598 .. code:: bash 599 600 ls /var/hyperledger/production/chaincodes 601 602 You should see ``mycc.1.0`` 603 604 Instantiate 605 ^^^^^^^^^^^ 606 607 Instantiate the chaincode: 608 609 .. code:: bash 610 611 peer chaincode instantiate -o 127.0.0.1:7050 -C mychannel -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 -c '{"Args":["init","a", "100", "b","200"]}' 612 613 Check your active containers: 614 615 .. code:: bash 616 617 docker ps 618 619 If the chaincode container started successfully, you should see: 620 621 .. code:: bash 622 623 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 624 bd9c6bda7560 dev-jdoe-mycc-1.0 "chaincode -peer.a..." 5 seconds ago Up 5 seconds dev-jdoe-mycc-1.0 625 626 Invoke 627 ^^^^^^ 628 629 Issue an invoke to move "10" from "a" to "b": 630 631 .. code:: bash 632 633 peer chaincode invoke -o 127.0.0.1:7050 -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}' 634 635 Wait a few seconds for the operation to complete 636 637 Query 638 ^^^^^ 639 640 Query for the value of "a": 641 642 .. code:: bash 643 644 # this should return 90 645 peer chaincode query -o 127.0.0.1:7050 -C mychannel -n mycc -c '{"Args":["query","a"]}' 646 647 Don't forget to clear ledger folder ``/var/hyperledger/`` after each 648 run! 649 650 :: 651 652 rm -rf /var/hyperledger/* 653 654 Using CouchDB 655 ------------- 656 657 The state database can be switched from the default (goleveldb) to CouchDB. 658 The same chaincode functions are available with CouchDB, however, there is the 659 added ability to perform rich and complex queries against the state database 660 data content contingent upon the chaincode data being modeled as JSON. 661 662 To use CouchDB instead of the default database (goleveldb), follow the same 663 procedure in the **Prerequisites** section, and additionally perform the 664 following two steps to enable the CouchDB containers and associate each peer 665 container with a CouchDB container: 666 667 - Make the CouchDB image. 668 669 .. code:: bash 670 671 # make sure you are in the /fabric directory 672 make couchdb 673 674 - Open the ``fabric/examples/e2e_cli/docker-compose.yaml`` and un-comment 675 all commented statements relating to CouchDB containers and peer container 676 use of CouchDB. These instructions are are also outlined in the 677 same ``docker-compose.yaml`` file. Search the file for 'couchdb' (case insensitive) references. 678 679 *chaincode_example02* should now work using CouchDB underneath. 680 681 ***Note***: If you choose to implement mapping of the fabric-couchdb container 682 port to a host port, please make sure you are aware of the security 683 implications. Mapping of the port in a development environment allows the 684 visualization of the database via the CouchDB web interface (Fauxton). 685 Production environments would likely refrain from implementing port mapping in 686 order to restrict outside access to the CouchDB containers. 687 688 You can use *chaincode_example02* chaincode against the CouchDB state database 689 using the steps outlined above, however in order to exercise the query 690 capabilities you will need to use a chaincode that has data modeled as JSON, 691 (e.g. *marbles02*). You can locate the *marbles02* chaincode in the 692 ``fabric/examples/chaincode/go`` directory. 693 694 Install, instantiate, invoke, and query *marbles02* chaincode by following the 695 same general steps outlined above for *chaincode_example02* in the **Manually 696 create the channel and join peers through CLI** section. After the **Join 697 channel** step, use the following steps to interact with the *marbles02* 698 chaincode: 699 700 - Install and instantiate the chaincode in ``peer0``: 701 702 .. code:: bash 703 704 peer chaincode install -o orderer0:7050 -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02 705 peer chaincode instantiate -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02 -c '{"Args":["init"]}' -P "OR ('Org0MSP.member','Org1MSP.member')" 706 707 - Create some marbles and move them around: 708 709 .. code:: bash 710 711 peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["initMarble","marble1","blue","35","tom"]}' 712 peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["initMarble","marble2","red","50","tom"]}' 713 peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["initMarble","marble3","blue","70","tom"]}' 714 peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["transferMarble","marble2","jerry"]}' 715 peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["transferMarblesBasedOnColor","blue","jerry"]}' 716 peer chaincode invoke -o orderer0:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/orderer/localMspConfig/cacerts/ordererOrg0.pem -C mychannel -n marbles -c '{"Args":["delete","marble1"]}' 717 718 719 720 - If you chose to activate port mapping, you can now view the state database 721 through the CouchDB web interface (Fauxton) by opening a browser and 722 navigating to one of the two URLs below. 723 724 For containers running in a vagrant environment: 725 726 ``http://localhost:15984/_utils`` 727 728 For non-vagrant environment, use the port address that was mapped in CouchDB 729 container specification: 730 731 ``http://localhost:5984/_utils`` 732 733 You should see a database named ``mychannel`` and the documents 734 inside it. 735 736 - You can run regular queries from the `cli` (e.g. reading ``marble2``): 737 738 .. code:: bash 739 740 peer chaincode query -C mychannel -n marbles -c '{"Args":["readMarble","marble2"]}' 741 742 743 You should see the details of ``marble2``: 744 745 .. code:: bash 746 747 Query Result: {"color":"red","docType":"marble","name":"marble2","owner":"jerry","size":50} 748 749 750 Retrieve the history of ``marble1``: 751 752 .. code:: bash 753 754 peer chaincode query -C mychannel -n marbles -c '{"Args":["getHistoryForMarble","marble1"]}' 755 756 You should see the transactions on ``marble1``: 757 758 .. code:: bash 759 760 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":}] 761 762 763 764 - You can also perform rich queries on the data content, such as querying marble fields by owner ``jerry``: 765 766 .. code:: bash 767 768 peer chaincode query -C mychannel -n marbles -c '{"Args":["queryMarblesByOwner","jerry"]}' 769 770 The output should display the two marbles owned by ``jerry``: 771 772 .. code:: bash 773 774 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}}] 775 776 Query by field ``owner`` where the value is ``jerry``: 777 778 .. code:: bash 779 780 peer chaincode query -C mychannel -n marbles -c '{"Args":["queryMarbles","{\"selector\":{\"owner\":\"jerry\"}}"]}' 781 782 The output should display: 783 784 .. code:: bash 785 786 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}}] 787 788 A Note on Data Persistence 789 -------------------------- 790 791 If data persistence is desired on the peer container or the CouchDB container, 792 one option is to mount a directory in the docker-host into a relevant directory 793 in the container. For example, you may add the following two lines in 794 the peer container specification in the ``docker-compose.yaml`` file: 795 796 .. code:: bash 797 798 volumes: 799 - /var/hyperledger/peer0:/var/hyperledger/production 800 801 802 For the CouchDB container, you may add the following two lines in the CouchDB 803 container specification: 804 805 .. code:: bash 806 807 volumes: 808 - /var/hyperledger/couchdb0:/opt/couchdb/data 809 810 811 Troubleshooting 812 --------------- 813 814 - Ensure you clear the file system after each run 815 816 - If you see docker errors, remove your images and start from scratch. 817 818 .. code:: bash 819 820 make clean 821 make peer-docker orderer-docker 822 823 - If you see the below error: 824 825 .. code:: bash 826 827 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) 828 829 You likely have chaincode images (e.g. ``peer0-peer0-mycc-1.0`` or 830 ``peer1-peer0-mycc1-1.0``) from prior runs. Remove them and try 831 again. 832 833 .. code:: bash 834 835 docker rmi -f $(docker images | grep peer[0-9]-peer[0-9] | awk '{print $3}') 836 837 - To cleanup the network, use the ``down`` option: 838 839 .. code:: bash 840 841 ./network_setup.sh down