区块链中文技术社区

Hyperledger Fabric 超级账本

Hyperledger Fabric 超级账本

Hyperledger Fabric 称为联盟链,是分布式账本技术(DLT),采用模块化架构来提供高度机密性,伸缩性,灵活性和可扩展性。 它旨在支持不同组件的可插拔实现,并适应整个经济生态系统中存在的复杂性和复杂性。

Hyperledger Fabric 架构

Hyperledger开发环境

    2018-08-23 04:18:50.971 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
    2018-08-23 04:18:50.985 UTC [cli/common] readBlock -> INFO 002 Got status: &{NOT_FOUND}
    2018-08-23 04:18:50.987 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
    2018-08-23 04:18:51.193 UTC [cli/common] readBlock -> INFO 004 Received block: 0
    ===================== Channel 'mychannel' created ===================== 
    
    Having all peers join the channel...
    + peer channel join -b mychannel.block
    + res=0
    + set +x
    2018-08-23 04:18:51.282 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
    2018-08-23 04:18:51.386 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
    ===================== peer0.org1 joined channel 'mychannel' ===================== 
    
    + peer channel join -b mychannel.block
    + res=0
    + set +x
    2018-08-23 04:18:54.468 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
    2018-08-23 04:18:54.577 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
    ===================== peer1.org1 joined channel 'mychannel' ===================== 
    
    + peer channel join -b mychannel.block
    + res=0
    + set +x
    2018-08-23 04:18:57.680 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
    2018-08-23 04:18:57.790 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
    ===================== peer0.org2 joined channel 'mychannel' ===================== 
    
    + peer channel join -b mychannel.block
    + res=0
    + set +x
    2018-08-23 04:19:00.881 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
    2018-08-23 04:19:00.991 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
    ===================== peer1.org2 joined channel 'mychannel' ===================== 
    
    Updating anchor peers for org1...
    + peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.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
    + res=0
    + set +x
    2018-08-23 04:19:04.082 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
    2018-08-23 04:19:04.089 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
    ===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' ===================== 
    
    Updating anchor peers for org2...
    + peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.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
    + res=0
    + set +x
    2018-08-23 04:19:07.175 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
    2018-08-23 04:19:07.183 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
    ===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' ===================== 
    
    Installing chaincode on peer0.org1...
    + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
    + res=0
    + set +x
    2018-08-23 04:19:10.273 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
    2018-08-23 04:19:10.273 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
    2018-08-23 04:19:10.401 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > 
    ===================== Chaincode is installed on peer0.org1 ===================== 
    
    Install chaincode on peer0.org2...
    + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
    + res=0
    + set +x
    2018-08-23 04:19:10.461 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
    2018-08-23 04:19:10.461 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
    2018-08-23 04:19:10.573 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > 
    ===================== Chaincode is installed on peer0.org2 ===================== 
    
    Instantiating chaincode on peer0.org2...
    + 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 mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'
    + res=0
    + set +x
    2018-08-23 04:19:10.634 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
    2018-08-23 04:19:10.634 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
    ===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' ===================== 
    
    Querying chaincode on peer0.org1...
    ===================== Querying on peer0.org1 on channel 'mychannel'... ===================== 
    Attempting to Query peer0.org1 ...3 secs
    + peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
    + res=0
    + set +x
    
    100
    ===================== Query successful on peer0.org1 on channel 'mychannel' ===================== 
    Sending invoke transaction on peer0.org1 peer0.org2...
    + 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 mycc --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:7051 --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"]}'
    + res=0
    + set +x
    2018-08-23 04:19:31.656 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 
    ===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' ===================== 
    
    Installing chaincode on peer1.org2...
    + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/
    + res=0
    + set +x
    2018-08-23 04:19:31.708 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
    2018-08-23 04:19:31.708 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
    2018-08-23 04:19:31.815 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > 
    ===================== Chaincode is installed on peer1.org2 ===================== 
    
    Querying chaincode on peer1.org2...
    ===================== Querying on peer1.org2 on channel 'mychannel'... ===================== 
    + peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
    Attempting to Query peer1.org2 ...3 secs
    + res=0
    + set +x
    
    90
    ===================== Query successful on peer1.org2 on channel 'mychannel' ===================== 
    
    ========= All GOOD, BYFN execution completed =========== 
    
    
     _____   _   _   ____   
    | ____| | \ | | |  _ \  
    |  _|   |  \| | | | | | 
    | |___  | |\  | | |_| | 
    |_____| |_| \_| |____/  
    

    ```
    这部分其实是利用 docker exec 命令 进入 cli 容器中执行 scripts/script.sh 脚本,其参数为 mychannel 3 golang 10 false,具体命令为:
    ```
    docker exec cli scripts/script.sh mychannel 3 golang 10 false
    ```
    * 接下来,继续细分 docker exec cli scripts/script.sh mychannel 3 golang 10 false 这个命令等同于以下操作:
      * 首先进入 cli 容器:
        ```
        docker exec -it cli bash
        ```
      * 接下来在容器里操作,配置环境变量:
        ```
        export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
        export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
        export CORE_PEER_LOCALMSPID="Org1MSP"
        export 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
        ```
      * 创建 mychannel 通道(相当于通过创世区块创建区块链,每个通道相当于每条独立的区块链):
        ``` 
        export CHANNEL_NAME=mychannel
        peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --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
        ```
        输出日志:
        ```
        2018-08-23 06:56:14.257 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
        2018-08-23 06:56:14.273 UTC [cli/common] readBlock -> INFO 002 Got status: &{NOT_FOUND}
        2018-08-23 06:56:14.274 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
        2018-08-23 06:56:14.480 UTC [cli/common] readBlock -> INFO 004 Received block: 0
        ```
      * 节点加入 mychannel 通道(相当于节点加入区块链网络):
        加入通道需要用到环境变量的值,默认为 peer0.org1 节点加入通道
        ```
        peer channel join -b mychannel.block 
        ```
        输出日志:
        ```
        2018-08-23 07:10:09.620 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
        2018-08-23 07:10:09.729 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel

        ```
        要其他节点也加入 mychannel 通道,需要修改环境变量的值,也很简单,举个例子,如果 peer1.org2 节点加入通道,只要把所有环境变量值出现 *org1* 替换成 *org2* ,并把 CORE_PEER_ADDRESS值中的 *peer0* 替换成 *peer1* ,如下:
        ```
        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=peer1.org2.example.com:7051
        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           
        ```     
        修改后继续执行:peer channel join -b mychannel.block,同理完成 peer0.org2,peer1.org1,peer1.org2 加入 mychannel 通道
      * 更新通道的锚节点,实质上是在通道的创世块之上添加了额外的配置信息(所谓锚节点就是某联盟在通道中公开链接的一个或多个节点)
        更新通道中 Org1 组织的锚节点为 peer0.org1.example.com:
        首先确保环境变量是 peer0.org1 的信息:
        ```
        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    
        ```
        然后更新
        ```
        peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --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
        ```
        更新通道中 Org2 组织的锚节点为 peer0.org2.example.com:
        首先确保环境变量是 peer0.org2 的信息:
        ```
        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
        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    
        ```
        然后更新
        ```
        peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx --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
        ```
      * 安装和实例化Chaincode
        安装 golang版 Chaincode ,安装到的节点为环境变量的配置节点:
        ```
        peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
        ```
        日志输出:
        ```
        2018-08-23 11:25:20.439 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
        2018-08-23 11:25:20.439 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
        2018-08-23 11:25:20.831 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
        ```
        接下来,在通道上实例化链码。这将初始化通道上的链代码,设置链代码的认可策略,并为目标节点启动链代码容器:
        ```
        peer chaincode instantiate -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 $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"
        ```
        日志输出:
        ```
        2018-08-23 11:31:33.050 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
        2018-08-23 11:31:33.050 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 
        ```
      * 查询以确保链代码已正确实例化并填充状态数据库
        ```
        peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' 
        ```
        日志输出:
        ```
        100
        ```
      * 调用,a取10给b
        ```
        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 $CHANNEL_NAME -n mycc --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:7051 --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"]}' 
        ```
        输出
        ```
        INFO 001 Chaincode invoke successful. result: status:200  
        ```
        查询结果得到: a:90,b:210,成功结束 

通过 hyperledger fabric-ca 搭建生产环境的联盟链

相关资料

转载自:https://gitee.com/jonluo/blockchain/blob/master/hyperledger-fabric/README.md

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »