建立第一個fabric網路
-
Generate Network Artifacts
cd first-network
先關閉服務
./byfn.sh -m down
然後建立
./byfn.sh -m generate
-
建立創世區塊
../bin/cryptogen generate --config=./crypto-config.yaml
該檔案是first-network下面的配置檔案 -
生成初始區塊
export FABRIC_CFG_PATH=$PWD
f
../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
-
生成應用通道的配置資訊
export CHANNEL_NAME=mychannel
鏈的一個標識
../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
-
生成錨節點配置更新檔案
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
錨節點用於和對等節點進行通訊
- 操作網路
編輯 docker-compose-cli.yaml 註釋掉command命令
路徑:/goWorkPlace/src/github.com/hyperledger/fabric/peer
CHANNEL_NAME=$CHANNEL_NAME TIMEOUT=600 docker-compose -f docker-compose-cli.yaml up -d
建立和加入通道
-
進入docker容器
docker exec -it cli bash
-
建立通道
export CHANNEL_NAME=mychannel
- 建立區塊鏈
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile /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 join -b mychannel.block
鏈上程式碼
- 安裝鏈碼
peer chaincode install -n mycc -v 1.4 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
用以上程式碼安裝,會被認為是本地路徑,檢視本地掛載位置docker-compser-cli.yaml
故本文使用以下程式碼
peer chaincode install -n mycc -v 1.4 -p github.com/chaincode/chaincode_example02/go
- 例項化鏈碼
peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --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 $CHANNEL_NAME -n mycc -v 1.4 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
初始化a,b兩個節點,a有一百塊,b有兩百塊
- 查詢
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
- 轉賬
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --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 $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'
以上為一次基本流程