HyperLedger Fabric环境搭建

环境准备

  • 准备好hyperledger-fabric的代码
  • go环境
  • docker环境

准备镜像

运行 bootstrap.sh脚本,这个脚本首先会去拉取fabric-samples的代码,然后会下载需要的binary文件和docker镜像

Installing hyperledger/fabric-samples repo

===> Checking out v1.4.4 of hyperledger/fabric-samples
HEAD is now at bc72f3e... Remove Stalebot

Installing Hyperledger Fabric binaries

===> Downloading version 1.4.4 platform specific fabric binaries
===> Downloading:  https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.4.4/hyperledger-fabric-linux-amd64-1.4.4.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 79.6M  100 79.6M    0     0  8739k      0  0:00:09  0:00:09 --:--:-- 12.3M
==> Done.
===> Downloading version 1.4.4 platform specific fabric-ca-client binary
===> Downloading:  https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/linux-amd64-1.4.4/hyperledger-fabric-ca-linux-amd64-1.4.4.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1142    0  1142    0     0    968      0 --:--:--  0:00:01 --:--:--   968

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
==> There was an error downloading the binary file. Switching to incremental download.
==> Downloading file...

------> 1.4.4 fabric-ca-client binary is not available to download  (Available from 1.1.0-rc1) <----


Installing Hyperledger Fabric docker images

===> Pulling fabric Images
==> FABRIC IMAGE: peer

1.4.4: Pulling from hyperledger/fabric-peer
b8f262c62ec6: Already exists
9c2dad781fe8: Already exists
7e48a048d895: Already exists
4b5f2a9ec0f7: Pull complete
9332c2443003: Pull complete
f8823b03421b: Pull complete
Digest: sha256:92c2bef91e80f54f6d73a89b796eab1b616f372e2258431f17d50dd0c2ce316b
Status: Downloaded newer image for hyperledger/fabric-peer:1.4.4
docker.io/hyperledger/fabric-peer:1.4.4
==> FABRIC IMAGE: orderer

1.4.4: Pulling from hyperledger/fabric-orderer
b8f262c62ec6: Already exists
9c2dad781fe8: Already exists
7e48a048d895: Already exists
4b5f2a9ec0f7: Already exists
a38d313ebb74: Pull complete
391a02b02202: Pull complete
Digest: sha256:f7715923e2ca67cee526c21638d86b21480e179419e9d6c20078caabb241b20e
Status: Downloaded newer image for hyperledger/fabric-orderer:1.4.4
docker.io/hyperledger/fabric-orderer:1.4.4
==> FABRIC IMAGE: ccenv

1.4.4: Pulling from hyperledger/fabric-ccenv
7ddbc47eeb70: Already exists
c1bbdc448b72: Already exists
8c3b70e39044: Already exists
45d437916d57: Already exists
b5035666b1cd: Already exists
94c898b5fdef: Already exists
bee7bd3eb18f: Already exists
9dc56c5637b5: Already exists
33954343759c: Pull complete
09a5b37c7f4f: Pull complete
947f6f3dd9ef: Pull complete
d68f5147d7dd: Pull complete
Digest: sha256:a4fb800d54ace75e26736d4789af7d27552774e781cf2513a5c83756808b8441
Status: Downloaded newer image for hyperledger/fabric-ccenv:1.4.4
docker.io/hyperledger/fabric-ccenv:1.4.4
==> FABRIC IMAGE: javaenv

1.4.4: Pulling from hyperledger/fabric-javaenv
5667fdb72017: Pull complete
d83811f270d5: Pull complete
ee671aafb583: Pull complete
7fc152dfb3a6: Pull complete
3ab6e950cac5: Pull complete
5376778093d1: Pull complete
43a7285ac27f: Pull complete
67c5b361775c: Pull complete
7a9b4d622867: Pull complete
6d6187ff91b2: Pull complete
226ce6507c2d: Pull complete
3b5744e9eb4a: Pull complete
612ae6bdb688: Pull complete
2c0af2fc994a: Pull complete
594ab6cf73a0: Pull complete
9a77075403a7: Pull complete
3a153e9e6bcb: Pull complete
b2e75a1b75e5: Pull complete
32055256389b: Pull complete
03e36de7e531: Pull complete
d8515073d6bd: Pull complete
ae113aae7b23: Pull complete
0b7aba7b70e9: Pull complete
dba5f7f27672: Pull complete
e187107a4f2d: Pull complete
7092dcf5ae00: Pull complete
d825619a86d2: Pull complete
aeee0a2c59db: Pull complete
76a0bd84f506: Pull complete
80a98336b964: Pull complete
7d6ee8a0058b: Pull complete
cf18382c2cf7: Pull complete
75a167f9814e: Pull complete
cb5809320473: Pull complete
41d39db8657e: Pull complete
Digest: sha256:9eee210e4874b6e1a6d6a7594f0659ef37686ad572bc6273466b93b61956d23f
Status: Downloaded newer image for hyperledger/fabric-javaenv:1.4.4
docker.io/hyperledger/fabric-javaenv:1.4.4
==> FABRIC IMAGE: tools

1.4.4: Pulling from hyperledger/fabric-tools
7ddbc47eeb70: Already exists
c1bbdc448b72: Already exists
8c3b70e39044: Already exists
45d437916d57: Already exists
b5035666b1cd: Already exists
94c898b5fdef: Already exists
bee7bd3eb18f: Already exists
9dc56c5637b5: Already exists
31f2b9e8b256: Pull complete
89c4701a4f5f: Pull complete
18467825df3e: Pull complete
Digest: sha256:822418c5ad61473ecfa911dfe4b100264500c504c78def52730106a717be71cb
Status: Downloaded newer image for hyperledger/fabric-tools:1.4.4
docker.io/hyperledger/fabric-tools:1.4.4
===> Pulling fabric ca Image
==> FABRIC CA IMAGE

1.4.4: Pulling from hyperledger/fabric-ca
b8f262c62ec6: Already exists
9c2dad781fe8: Already exists
7e48a048d895: Already exists
9d96ace3877c: Pull complete
a2d035916e73: Pull complete
998b88105ae5: Pull complete
ecca03945551: Pull complete
0eea958ac479: Pull complete
Digest: sha256:e506e3ce041fd6c4fca5d12492caf44597818d425a8ecc73cf2b135303365244
Status: Downloaded newer image for hyperledger/fabric-ca:1.4.4
docker.io/hyperledger/fabric-ca:1.4.4
===> Pulling thirdparty docker images
==> THIRDPARTY DOCKER IMAGE: couchdb

0.4.18: Pulling from hyperledger/fabric-couchdb
8f91359f1fff: Pull complete
d99273889f3e: Pull complete
3a248732f3d2: Pull complete
941ee2be0fa2: Pull complete
991f59ffd446: Pull complete
f22ab95319bb: Pull complete
29334908bdc9: Pull complete
63e8abdcb623: Pull complete
83c77489c2cf: Pull complete
9fc4375990c7: Pull complete
348abe1ba56c: Pull complete
Digest: sha256:048b7c44c1deaabd0f3d84fbf2f7b649d7b10c54a3241c7354f078ee2eff077c
Status: Downloaded newer image for hyperledger/fabric-couchdb:0.4.18
docker.io/hyperledger/fabric-couchdb:0.4.18
==> THIRDPARTY DOCKER IMAGE: kafka

0.4.18: Pulling from hyperledger/fabric-kafka
7ddbc47eeb70: Already exists
c1bbdc448b72: Already exists
8c3b70e39044: Already exists
45d437916d57: Already exists
b5035666b1cd: Already exists
454f364fa8f9: Pull complete
0f71dc83554e: Pull complete
bed0281cb779: Pull complete
73033e3a7473: Pull complete
Digest: sha256:b043b8b91fe858bfd51ad662e5816f9471f120e5ec3e77d29bf6c70bcec5c917
Status: Downloaded newer image for hyperledger/fabric-kafka:0.4.18
docker.io/hyperledger/fabric-kafka:0.4.18
==> THIRDPARTY DOCKER IMAGE: zookeeper

0.4.18: Pulling from hyperledger/fabric-zookeeper
7ddbc47eeb70: Already exists
c1bbdc448b72: Already exists
8c3b70e39044: Already exists
45d437916d57: Already exists
b5035666b1cd: Already exists
454f364fa8f9: Already exists
8d2edf9568d6: Pull complete
34539f6faa69: Pull complete
ce8d3daa1c23: Pull complete
aa613546ea7a: Pull complete
8fad74705649: Pull complete
f942fd545d8a: Pull complete
6dddb55bad94: Pull complete
Digest: sha256:ff5f9893355a56ac7dbc53d0b6a68c18cf7adfcc0c34a55a717866cb9c00a442
Status: Downloaded newer image for hyperledger/fabric-zookeeper:0.4.18
docker.io/hyperledger/fabric-zookeeper:0.4.18

===> List out hyperledger docker images
hyperledger/fabric-tools                                                                              amd64-1.4.5-snapshot-c4f834d   099176a5b1dc        3 weeks ago         1.49GB
hyperledger/fabric-tools                                                                              amd64-latest                   099176a5b1dc        3 weeks ago         1.49GB
hyperledger/fabric-orderer                                                                            amd64-1.4.5-snapshot-c4f834d   225cc1a6d075        3 weeks ago         120MB
hyperledger/fabric-orderer                                                                            amd64-latest                   225cc1a6d075        3 weeks ago         120MB
hyperledger/fabric-peer                                                                               amd64-1.4.5-snapshot-c4f834d   7826f9327eb1        3 weeks ago         128MB
hyperledger/fabric-peer                                                                               amd64-latest                   7826f9327eb1        3 weeks ago         128MB
hyperledger/fabric-javaenv                                                                            1.4.4                          4648059d209e        4 weeks ago         1.7GB
hyperledger/fabric-javaenv                                                                            latest                         4648059d209e        4 weeks ago         1.7GB
hyperledger/fabric-ca                                                                                 1.4.4                          62a60c5459ae        4 weeks ago         150MB
hyperledger/fabric-ca                                                                                 latest                         62a60c5459ae        4 weeks ago         150MB
hyperledger/fabric-tools                                                                              1.4.4                          7552e1968c0b        5 weeks ago         1.49GB
hyperledger/fabric-tools                                                                              latest                         7552e1968c0b        5 weeks ago         1.49GB
hyperledger/fabric-ccenv                                                                              1.4.4                          ca4780293e4c        5 weeks ago         1.37GB
hyperledger/fabric-ccenv                                                                              latest                         ca4780293e4c        5 weeks ago         1.37GB
hyperledger/fabric-orderer                                                                            1.4.4                          dbc9f65443aa        5 weeks ago         120MB
hyperledger/fabric-orderer                                                                            latest                         dbc9f65443aa        5 weeks ago         120MB
hyperledger/fabric-peer                                                                               1.4.4                          9756aed98c6b        5 weeks ago         128MB
hyperledger/fabric-peer                                                                               latest                         9756aed98c6b        5 weeks ago         128MB
hyperledger/fabric-zookeeper                                                                          0.4.18                         ede9389347db        6 weeks ago         276MB
hyperledger/fabric-zookeeper                                                                          latest                         ede9389347db        6 weeks ago         276MB
hyperledger/fabric-kafka                                                                              0.4.18                         caaae0474ef2        6 weeks ago         270MB
hyperledger/fabric-kafka                                                                              latest                         caaae0474ef2        6 weeks ago         270MB
hyperledger/fabric-couchdb                                                                            0.4.18                         d369d4eaa0fd        6 weeks ago         261MB
hyperledger/fabric-couchdb                                                                            latest                         d369d4eaa0fd        6 weeks ago         261MB
hyperledger/fabric-baseimage                                                                          amd64-0.4.18                   9e353eca480f        6 weeks ago         1.3GB
hyperledger/fabric-baseos                                                                             amd64-0.4.18                   c256a6aad46f        6 weeks ago         80.8MB
hyperledger/fabric-tools                                                                              1.4.0                          0a44f4261a55        11 months ago       1.56GB
hyperledger/fabric-ccenv                                                                              1.4.0                          5b31d55f5f3a        11 months ago       1.43GB
hyperledger/fabric-orderer                                                                            1.4.0                          54f372205580        11 months ago       150MB
hyperledger/fabric-peer                                                                               1.4.0                          304fac59b501        11 months ago       157MB
hyperledger/fabric-ca                                                                                 1.4.0                          1a804ab74f58        11 months ago       244MB
hyperledger/fabric-zookeeper                                                                          0.4.14                         d36da0db87a4        14 months ago       1.43GB
hyperledger/fabric-kafka                                                                              0.4.14                         a3b095201c66        14 months ago       1.44GB
hyperledger/fabric-couchdb                                                                            0.4.14                         f14f97292b4c        14 months ago       1.5GB

生成证书和配置文件

进入script/fabric-sample目录下,里面有很多样例工程,首选进入一个最简单的工程first-network。
首先执行一下byfn.sh,可以看到支持generate,up,和down的命令
首先看下generate生成的是哪些东西,执行一下

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x

Generate CCP files for Org1 and Org2
/data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/../bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
CONSENSUS_TYPE=solo
+ '[' solo == solo ']'
+ configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
2019-12-20 10:22:46.744 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-12-20 10:22:46.855 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 002 orderer type: solo
2019-12-20 10:22:46.855 CST [common.tools.configtxgen.localconfig] Load -> INFO 003 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:46.967 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 004 orderer type: solo
2019-12-20 10:22:46.967 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 005 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:46.968 CST [common.tools.configtxgen] doOutputBlock -> INFO 006 Generating genesis block
2019-12-20 10:22:46.968 CST [common.tools.configtxgen] doOutputBlock -> INFO 007 Writing genesis block
+ res=0
+ set +x

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2019-12-20 10:22:47.000 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-12-20 10:22:47.112 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:47.222 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-12-20 10:22:47.222 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:47.222 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 005 Generating new channel configtx
2019-12-20 10:22:47.224 CST [common.tools.configtxgen] doOutputChannelCreateTx -> INFO 006 Writing new channel tx
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2019-12-20 10:22:47.255 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-12-20 10:22:47.367 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:47.478 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-12-20 10:22:47.478 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:47.478 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-12-20 10:22:47.478 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2019-12-20 10:22:47.509 CST [common.tools.configtxgen] main -> INFO 001 Loading configuration
2019-12-20 10:22:47.620 CST [common.tools.configtxgen.localconfig] Load -> INFO 002 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:47.731 CST [common.tools.configtxgen.localconfig] completeInitialization -> INFO 003 orderer type: solo
2019-12-20 10:22:47.731 CST [common.tools.configtxgen.localconfig] LoadTopLevel -> INFO 004 Loaded configuration: /data/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/configtx.yaml
2019-12-20 10:22:47.731 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 005 Generating anchor peer update
2019-12-20 10:22:47.731 CST [common.tools.configtxgen] doOutputAnchorPeersUpdate -> INFO 006 Writing anchor peer update
+ res=0
+ set +x
  • 根据crypto-config.yaml生成证书
  • 根据configtx.yaml生成创世区块genesis.block
  • 根据configtx.yaml生成创世区块channel交易的配置文件channel.tx
  • 根据configtx.yaml对Org1MSP和Org2MSP生成锚节点

启动网络

  • ./byfn.sh up
Starting for channel 'mychannel' with CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
LOCAL_VERSION=1.4.4
DOCKER_IMAGE_VERSION=1.4.4
Creating network "net_byfn" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer1.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating volume "net_peer1.org2.example.com" with default driver
Creating peer0.org1.example.com ... done
Creating orderer.example.com    ... done
Creating peer1.org1.example.com ... done
Creating peer0.org2.example.com ... done
Creating peer1.org2.example.com ... done
Creating cli                    ... done
CONTAINER ID        IMAGE                               COMMAND             CREATED             STATUS                  PORTS                      NAMES
77bd27f8eab0        hyperledger/fabric-tools:latest     "/bin/bash"         1 second ago        Up Less than a second                              cli
5585f0df24d5        hyperledger/fabric-peer:latest      "peer node start"   3 seconds ago       Up Less than a second   0.0.0.0:10051->10051/tcp   peer1.org2.example.com
b1de4a3696d4        hyperledger/fabric-peer:latest      "peer node start"   3 seconds ago       Up 1 second             0.0.0.0:9051->9051/tcp     peer0.org2.example.com
76b08cad0994        hyperledger/fabric-orderer:latest   "orderer"           3 seconds ago       Up 1 second             0.0.0.0:7050->7050/tcp     orderer.example.com
e74d8c32c4e8        hyperledger/fabric-peer:latest      "peer node start"   3 seconds ago       Up 1 second             0.0.0.0:8051->8051/tcp     peer1.org1.example.com
6d8d6e974b43        hyperledger/fabric-peer:latest      "peer node start"   3 seconds ago       Up 2 seconds            0.0.0.0:7051->7051/tcp     peer0.org1.example.com

 ____    _____      _      ____    _____
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |
 ___) |   | |    / ___ \  |  _ <    | |
|____/    |_|   /_/   \_\ |_| \_\   |_|

Build your first network (BYFN) end-to-end test

Channel name : mychannel
Creating channel...
+ peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
+ res=0
+ set +x
2019-12-20 03:03:49.064 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-12-20 03:03:49.092 UTC [cli.common] readBlock -> INFO 002 Received block: 0
===================== Channel 'mychannel' created =====================

Having all peers join the channel...
+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-12-20 03:03:49.148 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-12-20 03:03:49.191 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org1 joined channel 'mychannel' =====================

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-12-20 03:03:52.266 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-12-20 03:03:52.316 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org1 joined channel 'mychannel' =====================

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-12-20 03:03:55.375 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-12-20 03:03:55.424 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org2 joined channel 'mychannel' =====================

+ peer channel join -b mychannel.block
+ res=0
+ set +x
2019-12-20 03:03:58.484 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-12-20 03:03:58.538 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org2 joined channel 'mychannel' =====================

Updating anchor peers for org1...
+ peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
+ res=0
+ set +x
2019-12-20 03:04:01.600 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-12-20 03:04:01.611 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' =====================

Updating anchor peers for org2...
+ peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
+ res=0
+ set +x
2019-12-20 03:04:04.672 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2019-12-20 03:04:04.683 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' =====================

Installing chaincode on peer0.org1...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-12-20 03:04:07.750 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-12-20 03:04:07.750 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-12-20 03:04:09.145 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
===================== Chaincode is installed on peer0.org1 =====================

Install chaincode on peer0.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-12-20 03:04:09.208 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-12-20 03:04:09.208 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-12-20 03:04:09.398 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
===================== Chaincode is installed on peer0.org2 =====================

Instantiating chaincode on peer0.org2...
+ peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
+ res=0
+ set +x
2019-12-20 03:04:09.462 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-12-20 03:04:09.462 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' =====================

Querying chaincode on peer0.org1...
===================== Querying on peer0.org1 on channel 'mychannel'... =====================
Attempting to Query peer0.org1 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x

100
===================== Query successful on peer0.org1 on channel 'mychannel' =====================
Sending invoke transaction on peer0.org1 peer0.org2...
+ peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
+ res=0
+ set +x
2019-12-20 03:05:01.107 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' =====================

Installing chaincode on peer1.org2...
+ peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
+ res=0
+ set +x
2019-12-20 03:05:01.166 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2019-12-20 03:05:01.166 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
2019-12-20 03:05:01.366 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
===================== Chaincode is installed on peer1.org2 =====================

Querying chaincode on peer1.org2...
===================== Querying on peer1.org2 on channel 'mychannel'... =====================
Attempting to Query peer1.org2 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'







+ res=0
+ set +x

90
===================== Query successful on peer1.org2 on channel 'mychannel' =====================

========= All GOOD, BYFN execution completed ===========


 _____   _   _   ____
| ____| | \ | | |  _ \
|  _|   |  \| | | | | |
| |___  | |\  | | |_| |
|_____| |_| \_| |____/

测试网络

docker exec -it cli bash
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

附录:合约

  • github.com/chaincode/chaincode_example02/go
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

//WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of
//calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has
//to be modified as well with the new ID of chaincode_example02.
//chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of
//hard-coding.

import (
    "fmt"
    "strconv"

    "github.com/hyperledger/fabric/core/chaincode/shim"
    pb "github.com/hyperledger/fabric/protos/peer"
)

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    fmt.Println("ex02 Init")
    _, args := stub.GetFunctionAndParameters()
    var A, B string    // Entities
    var Aval, Bval int // Asset holdings
    var err error

    if len(args) != 4 {
        return shim.Error("Incorrect number of arguments. Expecting 4")
    }

    // Initialize the chaincode
    A = args[0]
    Aval, err = strconv.Atoi(args[1])
    if err != nil {
        return shim.Error("Expecting integer value for asset holding")
    }
    B = args[2]
    Bval, err = strconv.Atoi(args[3])
    if err != nil {
        return shim.Error("Expecting integer value for asset holding")
    }
    fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

    // Write the state to the ledger
    err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    return shim.Success(nil)
}

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    fmt.Println("ex02 Invoke")
    function, args := stub.GetFunctionAndParameters()
    if function == "invoke" {
        // Make payment of X units from A to B
        return t.invoke(stub, args)
    } else if function == "delete" {
        // Deletes an entity from its state
        return t.delete(stub, args)
    } else if function == "query" {
        // the old "Query" is now implemtned in invoke
        return t.query(stub, args)
    }

    return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
}

// Transaction makes payment of X units from A to B
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    var A, B string    // Entities
    var Aval, Bval int // Asset holdings
    var X int          // Transaction value
    var err error

    if len(args) != 3 {
        return shim.Error("Incorrect number of arguments. Expecting 3")
    }

    A = args[0]
    B = args[1]

    // Get the state from the ledger
    // TODO: will be nice to have a GetAllState call to ledger
    Avalbytes, err := stub.GetState(A)
    if err != nil {
        return shim.Error("Failed to get state")
    }
    if Avalbytes == nil {
        return shim.Error("Entity not found")
    }
    Aval, _ = strconv.Atoi(string(Avalbytes))

    Bvalbytes, err := stub.GetState(B)
    if err != nil {
        return shim.Error("Failed to get state")
    }
    if Bvalbytes == nil {
        return shim.Error("Entity not found")
    }
    Bval, _ = strconv.Atoi(string(Bvalbytes))

    // Perform the execution
    X, err = strconv.Atoi(args[2])
    if err != nil {
        return shim.Error("Invalid transaction amount, expecting a integer value")
    }
    Aval = Aval - X
    Bval = Bval + X
    fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

    // Write the state back to the ledger
    err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
    if err != nil {
        return shim.Error(err.Error())
    }

    return shim.Success(nil)
}

// Deletes an entity from state
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    if len(args) != 1 {
        return shim.Error("Incorrect number of arguments. Expecting 1")
    }

    A := args[0]

    // Delete the key from the state in ledger
    err := stub.DelState(A)
    if err != nil {
        return shim.Error("Failed to delete state")
    }

    return shim.Success(nil)
}

// query callback representing the query of a chaincode
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    var A string // Entities
    var err error

    if len(args) != 1 {
        return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
    }

    A = args[0]

    // Get the state from the ledger
    Avalbytes, err := stub.GetState(A)
    if err != nil {
        jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
        return shim.Error(jsonResp)
    }

    if Avalbytes == nil {
        jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
        return shim.Error(jsonResp)
    }

    jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
    fmt.Printf("Query Response:%s\n", jsonResp)
    return shim.Success(Avalbytes)
}

func main() {
    err := shim.Start(new(SimpleChaincode))
    if err != nil {
        fmt.Printf("Error starting Simple chaincode: %s", err)
    }
}

参考

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,837评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,551评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,417评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,448评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,524评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,554评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,569评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,316评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,766评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,077评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,240评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,912评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,560评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,176评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,425评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,114评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,114评论 2 352

推荐阅读更多精彩内容