一、下载官方sample
参考前一章节7.3
二、chaincode测试转账
2.1 生成公钥和证书信息、创世区块和通道相关信息
生成公私钥和证书信息、创世区块和通道相关信息
进入first-network
cd first-network
./byfn.sh -m generate
公私钥和证书信息基于crypto-config.yaml生成,保存在crypto-config文件夹中。
创世区块和通道相关信息基于configtx.yaml生成,保存在channel-artifacts文件夹中。
下面是创建过程中的日志,从中可以 看出创世区块和通道生成的相关命令
Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
/user/fabric-samples/bin/cryptogen
##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x
/user/fabric-samples/bin/configtxgen
##########################################################
######### Generating Orderer Genesis block ##############
##########################################################
+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2018-07-13 04:29:14.935 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-07-13 04:29:14.952 CST [msp] getMspConfig -> INFO 002 Loading NodeOUs
2018-07-13 04:29:14.952 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs
2018-07-13 04:29:14.952 CST [common/tools/configtxgen] doOutputBlock -> INFO 004 Generating genesis block
2018-07-13 04:29:14.954 CST [common/tools/configtxgen] doOutputBlock -> INFO 005 Writing genesis block
+ res=0
+ set +x
#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2018-07-13 04:29:15.001 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-07-13 04:29:15.022 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2018-07-13 04:29:15.023 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs
2018-07-13 04:29:15.023 CST [msp] getMspConfig -> INFO 004 Loading NodeOUs
2018-07-13 04:29:15.057 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 005 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
2018-07-13 04:29:15.084 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-07-13 04:29:15.098 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-07-13 04:29:15.098 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x
2.2 启动网络
./byfn.sh -m up
./byfn.sh -m up -l node #可以通过指定-l node部署node版本的chaincode
启动成功后,终端会提示
========= All GOOD, BYFN execution completed ===========
2.3 转账测试
进入client客户端
docker exec -it cli bash
查询a账户的余额
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
将a账户余额转10元给b账户
peer chaincode invoke -o orderer.example.com:7050 --tls --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 -c '{"Args":["invoke","a","b","10"]}'
再次查询两个账户
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
如果查询成功的话,能在Query Result中看到每个项目的金额变动
2018-07-12 20:14:15.714 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-07-12 20:14:15.714 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 80
2018-07-12 20:14:15.725 UTC [main] main -> INFO 003 Exiting.....
2018-07-12 20:19:06.199 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-07-12 20:19:06.199 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 220
2018-07-12 20:19:06.204 UTC [main] main -> INFO 003 Exiting.....
2.4 关闭fabric网络
./byfn.sh -m down