1、生成證書:
#路徑需要更改為自己的路徑
cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/
#在這里可能會報錯,通常是權限問題,可以添加sudo重新執行
cryptogen generate --config=./crypto-config.yaml
#執行完畢后,當前文件夾下會出現一個新的文件夾:crypto-config,在該文件夾下就是剛剛生成的證書.
2、Orderer服務 生成創世區塊,通道配置,錨節點配置文件
#路徑需要更改為自己的路徑 cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/first-network/
生成創世區塊
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
生成通道配置信息
export CHANNEL_NAME=mychannel
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
生成兩個組織的錨節點配置文件
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
成功后在channel-artifacts有四個文件:
channel.tx genesis.block Org1MSPanchors.tx Org2MSPanchors.tx
-- 初始區塊(genesis.block)和通道配置事務(channel.tx、
Org1MSPanchors.tx..
)
-profile :configtx.yaml中的Profiles配置項,用於指定生成初始區塊還是通道交易配置文件。
-asOrg:用於指定有權設置的寫集中的值的Org組織名稱。
3、啟動fabric網絡
sudo docker-compose -f docker-compose-cli.yaml up -d
進入cli容器
sudo docker exec -it cli bash
4、創建通道
export CHANNEL_NAME=mychannel
export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls true --cafile $ORDERER_CA
-o 指定Orderer節點地址
-c 指定要創建的應用通道名稱
-f 指定創建應用通道時所使用的應用通道交易配置文件
--tls 開啟tls驗證
--cafile 指定tls_ca證書的所在路徑
5、將節點加入應用通道
peer channel join -b mychannel.block
6、更新錨節點
Org1更新錨節點: 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 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 ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile $ORDERER_CA
Org2更新錨節點: CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA
7、安裝示例鏈碼
先切換到peer0.org1這個節點 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 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
使用peer chaincode install命令可以安裝指定的ChainCode並對其命名:
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go
8、實例化鏈碼
peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR('Org1MSP.member','Org2MSP.member')"
9、查詢並發起交易
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
在另一個節點查詢交易 CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
10、查看日志
docker logs -f peer0.org1.example.com