手动搭建基础的fabric网络
(1)使用密钥生成器cryptogen产生Org1和Org2的加密材料
Cryptogen generate –config=./crypto-config.yaml
(2)使用交易配置工具configtxgen产生创世区块
export FABRIC_CFG_PATH=$PWD (用来告诉configtxgen 去哪儿找configtx.yaml 文件)
Configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel(系统通道) -outputBlock ./channel-artifacts/genesis.block
(3)使用交易配置工具configtxgen产生通道配置交易文件channel.tx
Configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel(普通通道)
(4)使用交易配置工具configtxgen产生锚节点
Configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel(普通通道) -asOrg Org1MSP
Configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel(普通通道) -asOrg Org2MSP
(5)使用docker-compose启动网络进入cli容器
docker-compose -f docker-compose-cli.yaml up -d
docker exec -it cli bash
(6)创建通道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(证书路径)
(7)配置不同的peer节点的环境变量,使的所有的peer节点加入channel
Peer channel join -b mychannel.block
切换peer节点环境变量的例子(切换为peer0.org1)
# Environment variables for PEER0
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
(8)分别为“Org1MSP”和“Org2MSP” 更新锚节点
#更新Org1MSP
Peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchor.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(证书路径)
#更新Org2MSP
Peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchor.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(证书路径)
(9)分别在org1和org2上安装、实例化链码
#安装
Peer chaincode install -n mychaincode -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/(chaincode路径)
#实例化
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 mychaincode -v 1.0 -l golang -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
(10)利用chaincode查询数据,调用chaincode
#查询
Peer chaincode query -C mychannel -n mychaincode -c '{"Args":["query","a"]}'
# 调用
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 mychaincode –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"]}'