原文链接:超级账本Fabric教程(四):使用docker编译fabric源码(中);超级账本Fabric教程(四):使用docker编译fabric源码(下)
本文继续介绍如何在docker镜像中编译fabric源码,方便修改代码后快速测试。
在主机的工程目录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
编辑一个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.....
我们可以看到上面创建通道和将peer加入通道的输出中, 我们在源码里面的改动生效了, 通过这样的方式可以改动fabric源码后不用频繁编译超级账本镜像文件了。
如果我们要改动源码,流程是首先在主机上改动代码,然后到compile容器中编译代码, 然后在其他容器中执行编译后的程序,由于这几个容器都能看到主机上~/go/bin目录中的内容,只要compile容器中代码编译完成后,其他容器中立马就能使用它
原因:
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-