end-to-end.rst 31 KB

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