【鏈塊技術57期】超級賬本Fabric教程(四):使用docker編譯fabric源碼(下)

原文鏈接:超級賬本Fabric教程(四):使用docker編譯fabric源碼(中)超級賬本Fabric教程(四):使用docker編譯fabric源碼(下)

 

本文繼續介紹如何在docker鏡像中編譯fabric源碼,方便修改代碼後快速測試。

 

7.2 生成交易配置

在主機的工程目錄my-basic-network下新建configtx.yaml,具體內容如下:

 

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
 
---
################################################################################
#
#   Profile
#
#   - Different configuration profiles may be encoded here to be specified
#   as parameters to the configtxgen tool
#
################################################################################
Profiles:
 
    OneOrgOrdererGenesis:
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
    OneOrgChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
 
################################################################################
#
#   Section: Organizations
#
#   - This section defines the different organizational identities which will
#   be referenced later in the configuration.
#
################################################################################
Organizations:
 
    # SampleOrg defines an MSP using the sampleconfig.  It should never be used
    # in production but may be used as a template for other definitions
    - &OrdererOrg
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: OrdererOrg
 
        # ID to load the MSP definition as
        ID: OrdererMSP
 
        # MSPDir is the filesystem path which contains the MSP configuration
        MSPDir: crypto-config/ordererOrganizations/example.com/msp
 
    - &Org1
        # DefaultOrg defines the organization which is used in the sampleconfig
        # of the fabric.git development environment
        Name: Org1MSP
 
        # ID to load the MSP definition as
        ID: Org1MSP
 
        MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
 
        AnchorPeers:
            # AnchorPeers defines the location of peers which can be used
            # for cross org gossip communication.  Note, this value is only
            # encoded in the genesis block in the Application section context
            - Host: peer0.org1.example.com
              Port: 7051
 
################################################################################
#
#   SECTION: Orderer
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for orderer related parameters
#
################################################################################
Orderer: &OrdererDefaults
 
    # Orderer Type: The orderer implementation to start
    # Available types are "solo" and "kafka"
    OrdererType: solo
 
    Addresses:
        - orderer.example.com:7050
 
    # Batch Timeout: The amount of time to wait before creating a batch
    BatchTimeout: 2s
 
    # Batch Size: Controls the number of messages batched into a block
    BatchSize:
 
        # Max Message Count: The maximum number of messages to permit in a batch
        MaxMessageCount: 10
 
        # Absolute Max Bytes: The absolute maximum number of bytes allowed for
        # the serialized messages in a batch.
        AbsoluteMaxBytes: 99 MB
 
        # Preferred Max Bytes: The preferred maximum number of bytes allowed for
        # the serialized messages in a batch. A message larger than the preferred
        # max bytes will result in a batch larger than preferred max bytes.
        PreferredMaxBytes: 512 KB
 
    Kafka:
        # Brokers: A list of Kafka brokers to which the orderer connects
        # NOTE: Use IP:port notation
        Brokers:
            - 127.0.0.1:9092
 
    # Organizations is the list of orgs which are defined as participants on
    # the orderer side of the network
    Organizations:
 
################################################################################
#
#   SECTION: Application
#
#   - This section defines the values to encode into a config transaction or
#   genesis block for application related parameters
#
################################################################################
Application: &ApplicationDefaults
 
    # Organizations is the list of orgs which are defined as participants on
    # the application side of the network
    Organizations:

 

使用~/go/bin/confitxgen 工具來生成交易配置:

 

# 創建一個配置目錄
mkdir config
export FABRIC_CFG_PATH=$PWD
export CHANNEL_NAME=mychannel
# 生成創世區塊
configtxgen -profile OneOrgOrdererGenesis -outputBlock ./config/genesis.block
# 生成創建通道的配置文件
configtxgen -profile OneOrgChannel -outputCreateChannelTx ./config/channel.tx -channelID $CHANNEL_NAME
# 創建錨節點配置文件
configtxgen -profile OneOrgChannel -outputAnchorPeersUpdate ./config/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP

 

成功執行完以上命令後會在~/my-basic-network/config目錄下生成以下三個文件:

channel.tx  genesis.block  Org1MSPanchors.tx

 

7.3 啓動orderer和peer節點

編輯一個docker-compose的配置文件

 

#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
 
networks:
  basic:
 
services:
  orderer.example.com:
    container_name: orderer.example.com
    image: yeasy/hyperledger-fabric:1.0.0
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/etc/hyperledger/configtx/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/msp/orderer/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    ports:
      - 7050:7050
    volumes:
        - ./config/:/etc/hyperledger/configtx
        - ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/:/etc/hyperledger/msp/orderer
        - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/:/etc/hyperledger/msp/peerOrg1
    networks:
      - basic
 
  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    image: yeasy/hyperledger-fabric:1.0.0
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_LOGGING_PEER=debug
      - CORE_CHAINCODE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      # # the following setting starts chaincode containers on the same
      # # bridge network as the peers
      # # https://docs.docker.com/compose/networking/
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_basic
      #- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      #- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
      # The CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME and CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD
      # provide the credentials for ledger to connect to CouchDB.  The username and password must
      # match the username and password set for the associated CouchDB.
      - CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=
      - CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric
    command: peer node start
    # command: peer node start --peer-chaincodedev=true
    ports:
      - 7051:7051
      - 7053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org1.example.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
        - ~/go/bin:/go/bin
    depends_on:
      - orderer.example.com
    networks:
      - basic

 

啓動前關閉網絡,防止之前啓動過

docker-compose  -f docker-compose.yaml down

 

啓動orderer和peer節點

docker-compose -f docker-compose.yaml up -d peer0.org1.example.com orderer.example.com

 

查看容器進程

 

docker ps
CONTAINER ID      IMAGE        COMMAND          CREATED     STATUS      PORTS     NAMES
6f9d4ad6af8b      yeasy/hyperledger-fabric:1.0.0   "peer node start"        56 seconds ago      Up 53 seconds      0.0.0.0:7051->7051/tcp, 7050/tcp, 7054/tcp, 0.0.0.0:7053->7053/tcp   peer0.org1.example.com
0965146445ca       yeasy/hyperledger-fabric:1.0.0   "orderer"          58 seconds ago      Up 56 seconds       7051/tcp, 0.0.0.0:7050->7050/tcp, 7054/tcp                           orderer.example.com
99edaa3a60c5        yeasy/hyperledger-fabric:1.0.0   "/bin/bash -c 'sleep…"   2 hours ago         Up 2 hours          7050-7051/tcp, 7054/tcp                                              compile

 

創建通道

 

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx
----------Welcome to my-basic-network----------
2018-11-14 08:13:05.098 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-11-14 08:13:05.098 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-11-14 08:13:05.107 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-11-14 08:13:05.114 UTC [msp] GetLocalMSP -> DEBU 004 Returning existing local MSP
2018-11-14 08:13:05.116 UTC [msp] GetDefaultSigningIdentity -> DEBU 005 Obtaining default signing identity
2018-11-14 08:13:05.116 UTC [msp] GetLocalMSP -> DEBU 006 Returning existing local MSP
2018-11-14 08:13:05.117 UTC [msp] GetDefaultSigningIdentity -> DEBU 007 Obtaining default signing identity
2018-11-14 08:13:05.117 UTC [msp/identity] Sign -> DEBU 008 Sign: plaintext: 0A8C060A074F7267314D53501280062D...53616D706C65436F6E736F727469756D 
2018-11-14 08:13:05.118 UTC [msp/identity] Sign -> DEBU 009 Sign: digest: 5D1863244822DCB9603797144AD9F1BAA86A12AC3A730C069F510039530F35EC 
2018-11-14 08:13:05.119 UTC [msp] GetLocalMSP -> DEBU 00a Returning existing local MSP
2018-11-14 08:13:05.119 UTC [msp] GetDefaultSigningIdentity -> DEBU 00b Obtaining default signing identity
2018-11-14 08:13:05.119 UTC [msp] GetLocalMSP -> DEBU 00c Returning existing local MSP
2018-11-14 08:13:05.120 UTC [msp] GetDefaultSigningIdentity -> DEBU 00d Obtaining default signing identity
2018-11-14 08:13:05.120 UTC [msp/identity] Sign -> DEBU 00e Sign: plaintext: 0AC3060A1508021A060891B2AFDF0522...B9F3BEFA93B397ABE731E5922EB2AD40 
2018-11-14 08:13:05.120 UTC [msp/identity] Sign -> DEBU 00f Sign: digest: D6292E9E7628D66F3F2F227906C57901F400E361F0BFF889D6AF281EB8464E05 
2018-11-14 08:13:05.167 UTC [msp] GetLocalMSP -> DEBU 010 Returning existing local MSP
2018-11-14 08:13:05.167 UTC [msp] GetDefaultSigningIdentity -> DEBU 011 Obtaining default signing identity
2018-11-14 08:13:05.167 UTC [msp] GetLocalMSP -> DEBU 012 Returning existing local MSP
2018-11-14 08:13:05.167 UTC [msp] GetDefaultSigningIdentity -> DEBU 013 Obtaining default signing identity
2018-11-14 08:13:05.167 UTC [msp/identity] Sign -> DEBU 014 Sign: plaintext: 0AC3060A1508021A060891B2AFDF0522...63451B96B86D12080A021A0012021A00 
2018-11-14 08:13:05.167 UTC [msp/identity] Sign -> DEBU 015 Sign: digest: 660D6E5A80DA1D1C46FB93E63B697A4362E0A3201D08484EDB7EAB15798596E1 
2018-11-14 08:13:05.168 UTC [channelCmd] readBlock -> DEBU 016 Got status:*orderer.DeliverResponse_Status 
2018-11-14 08:13:05.168 UTC [msp] GetLocalMSP -> DEBU 017 Returning existing local MSP
2018-11-14 08:13:05.168 UTC [msp] GetDefaultSigningIdentity -> DEBU 018 Obtaining default signing identity
2018-11-14 08:13:05.170 UTC [channelCmd] InitCmdFactory -> INFO 019 Endorser and orderer connections initialized
2018-11-14 08:13:05.371 UTC [msp] GetLocalMSP -> DEBU 01a Returning existing local MSP
2018-11-14 08:13:05.371 UTC [msp] GetDefaultSigningIdentity -> DEBU 01b Obtaining default signing identity
2018-11-14 08:13:05.372 UTC [msp] GetLocalMSP -> DEBU 01c Returning existing local MSP
2018-11-14 08:13:05.372 UTC [msp] GetDefaultSigningIdentity -> DEBU 01d Obtaining default signing identity
2018-11-14 08:13:05.372 UTC [msp/identity] Sign -> DEBU 01e Sign: plaintext: 0AC3060A1508021A060891B2AFDF0522...763BCEC0A59A12080A021A0012021A00 
2018-11-14 08:13:05.372 UTC [msp/identity] Sign -> DEBU 01f Sign: digest: FEB035372AC14E18601FE0D76D77663902FE971F5E4C69C1DD8EC9C7823F3A6D 
2018-11-14 08:13:05.390 UTC [channelCmd] readBlock -> DEBU 020 Received block:0 
2018-11-14 08:13:05.397 UTC [main] main -> INFO 021 Exiting.....

 

將peer將入通道

 

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel join -b mychannel.block
----------Welcome to my-basic-network----------
2018-11-14 08:14:04.808 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-11-14 08:14:04.808 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-11-14 08:14:04.810 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
2018-11-14 08:14:04.811 UTC [msp/identity] Sign -> DEBU 004 Sign: plaintext: 0A8A070A5C08011A0C08CCB2AFDF0510...D06A96FA3B5F1A080A000A000A000A00
2018-11-14 08:14:04.811 UTC [msp/identity] Sign -> DEBU 005 Sign: digest: 1D06F595E5B39045672FA2476B6ECA972FE674864ECC13A14F8201C416594444
2018-11-14 08:14:04.862 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel!
2018-11-14 08:14:04.862 UTC [main] main -> INFO 007 Exiting.....

 

7.4 總結

我們可以看到上面創建通道和將peer加入通道的輸出中, 我們在源碼裏面的改動生效了, 通過這樣的方式可以改動fabric源碼後不用頻繁編譯超級賬本鏡像文件了。

如果我們要改動源碼,流程是首先在主機上改動代碼,然後到compile容器中編譯代碼, 然後在其他容器中執行編譯後的程序,由於這幾個容器都能看到主機上~/go/bin目錄中的內容,只要compile容器中代碼編譯完成後,其他容器中立馬就能使用它

 

八、 操作過程中的問題解決

8.1 沒有權限執行dock命令解決辦法

原因:

ocker進程使用Unix Socket而不是TCP端口。而默認情況下,Unix socket屬於root用戶,需要root權限才能訪問

解決辦法:

#添加docker用戶組
sudo groupadd docker  
#將登陸用戶加入到docker用戶組中
sudo gpasswd -a $USER docker   
 #更新用戶組
newgrp docker 
#測試docker命令是否可以使用sudo正常使  
 docker ps

-END-

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章