开始
创建fabric-liuhao目录
$ mkdir fabric-liuhao
准备crypto-config.yaml配置文件
$ cd fabric-liuhao
$ cryptogen showtemplate > crypto-config.yaml
编辑 crypto-config.yaml模板如下
OrdererOrgs:
- Name: Orderer
Domain: liuhao.com
EnableNodeOUs: true
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.liuhao.com
EnableNodeOUs: true
Template:
Count: 2
Users:
Count: 1
- Name: Org2
Domain: org2.liuhao.com
EnableNodeOUs: true
Template:
Count: 2
Users:
Count: 1
生成证书文件
$ cryptogen generate --config ./crypto-config.yaml
准备configtx.yaml
生成创世区块
$ mkdir channel-artifacts
$ configtxgen -profile TwoOrgsOrdererGenesis -channelID syschannel -outputBlock ./channel-artifacts/genesis.block
$ ls channel-artifacts/
genesis.block
查看创世区块
$ configtxgen -inspectBlock channel-artifacts/genesis.block > genesis.block.json
生成通道文件
$ configtxgen -profile TwoOrgsChannel -channelID mychannel -outputCreateChannelTx ./channel-artifacts/channel.tx
$ ls channel-artifacts/
channel.tx genesis.block
生成锚点配置
## 生成组织1的锚点配置文件
$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
## 生成组织1的锚点配置文件
$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
$ ls channel-artifacts/
channel.tx genesis.block Org1MSPanchors.tx Org2MSPanchors.tx
准备docker-compose-cli.yaml文件
执行docker-compose-cli.yaml,启动网络
$ export IMAGE_TAG="latest"
$ docker-compose -f docker-compose-cli.yaml up -d
进入fabric客户端
docker exec -it cli bash
创建通道
$ peer channel create -c mychannel -f ./channel-artifacts/channel.tx -o orderer.liuhao.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/liuhao.com/orderers/orderer.liuhao.com/msp/tlscacerts/tlsca.liuhao.com-cert.pem
加入通道
$ peer channel join -b mychannel.block
更新通道锚节点
$ peer channel update -o orderer.liuhao.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/liuhao.com/orderers/orderer.liuhao.com/msp/tlscacerts/tlsca.liuhao.com-cert.pem
安装链码
$ peer chaincode install -n mycc -v 1.0.0 -l golang -p github.com/chaincode/go/mycc/
注意:-p 命令会默认寻找$GOPATH/src
目录下的相对路径
实例化链码
$ peer chaincode instantiate -o orderer.liuhao.com:7050 -C mychannel -n mycc -l golang -v 1.0.0 -c '{"Args":["init","liuhao","100"]}' -P "OR('Org1MSP.peer','Org2MSP.peer')" --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/liuhao.com/orderers/orderer.liuhao.com/msp/tlscacerts/tlsca.liuhao.com-cert.pem
调用链码
$ peer chaincode invoke -o orderer.liuhao.com:7050 -C mychannel -n mycc -c '{"Args":["increase","liuhao","50"]}' --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/liuhao.com/orderers/orderer.liuhao.com/msp/tlscacerts/tlsca.liuhao.com-cert.pem
升级链码
需要先安装更新后的链码
$ peer chaincode install -n mycc -v 1.1.0 -l golang -p github.com/chaincode/go/mycc/
再升级
$ export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/liuhao.com/orderers/orderer.liuhao.com/msp/tlscacerts/tlsca.liuhao.com-cert.pem
$ peer chaincode upgrade -o orderer.liuhao.com:7050 --tls --cafile $ORDERER_CA -C mychannel -n mycc -v 1.1.0 -c '{"Args":["init","xixi","100"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"