在本文中我们将创建一个自定义网络,并使用Hyperledger Fabric v2.1部署示例链代码。之后,我们将查询chaincode并由我们将创建的2个对等点与网络进行交互。
在开始之前,你先要配置好Hyperledger Fabric的插件和配置好环境变量。具体可以参考上一遍文章。
创建一个自定义网络并启动它
我们在fabric-samples文件夹下创建一个名为burakcan-network的文件夹。在我们的网络中,有3个节点,包括2个组织,分别是BurakCan1和Burakcan2,以及1个orderer,因此我们需要生成所有证书和密钥来证明节点是否真的是自己。我们将使用cryptogen工具提供用于为我们的网络实体生成密码材料,并且在安装特定于平台的二进制文件时已经安装了它。它放在fabric-samples / bin文件夹下。它使用了包含网络拓扑的crypto-config.yaml文件,并允许我们为组织以及属于这些组织的组件生成一组证书和密钥。
这就是我们在burakcan网络目录下创建以下文件的原因。我们定义了一个Orderer组织(OrdererOrg)和2个对等组织(Burakcan1和Burakcan2)。组织中的每个计数值都指定该组织中的peers数。
OrdererOrgs:
– Name: OrderingService
Domain: burakcan-network.com
Specs:
– Hostname: orderer
PeerOrgs:
– Name: Burakcan1
Domain: be1.burakcan-network.com
EnableNodeOUs: true
Template:
Count: 2
Users:
Count: 1
– Name: Burakcan2
Domain: be2.burakcan-network.com
EnableNodeOUs: true
Template:
Count: 1
Users:
Count: 1
定义crypto-config.yaml文件后,我们在burakcan-network目录下执行以下命令通道。
../bin/cryptogen generate –config crypto-config.yaml –output=crypto-config
我们应该看到以下消息。
然后,我们将继续安装平台特定的二进制文件时已经安装的configtxgen工具,该工具也将放置在fabric-samples/bin文件夹下。它提供了生成这些重要配置工件的方法。
· 1 orderer genesis block
· 1 channel configuration transaction
· 2 anchor peer transactions(必须针对每个组织,并且我们有Burakcan1和Burakcan2组织,因此我们将生成2个)
它使用了configtx.yaml文件,其中包含网络的定义,因此我们将在burakcan-network目录下创建以下文件。
Organizations:
– &OrdererOrg
Name: OrderingService
ID: OrdererMSP
MSPDir: crypto-config/ordererOrganizations/burakcan-network.com/msp
Policies:
Readers:
Type: Signature
Rule: “OR(‘OrdererMSP.member’)”
Writers:
Type: Signature
Rule: “OR(‘OrdererMSP.member’)”
Admins:
Type: Signature
Rule: “OR(‘OrdererMSP.admin’)”
OrdererEndpoints:
– orderer.burakcan-network.com:7050
– &Burakcan1
Name: Burakcan1MSP
ID: Burakcan1MSP
MSPDir: crypto-config/peerOrganizations/be1.burakcan-network.com/msp
Policies:
Readers:
Type: Signature
Rule: “OR(‘Burakcan1MSP.admin’, ‘Burakcan1MSP.peer’, ‘Burakcan1MSP.client’)”
Writers:
Type: Signature
Rule: “OR(‘Burakcan1MSP.admin’, ‘Burakcan1MSP.client’)”
Admins:
Type: Signature
Rule: “OR(‘Burakcan1MSP.admin’)”
Endorsement:
Type: Signature
Rule: “OR(‘Burakcan1MSP.peer’)”
AnchorPeers:
– Host: peer0.be1.burakcan-network.com
Port: 7051
– &Burakcan2
Name: Burakcan2MSP
ID: Burakcan2MSP
MSPDir: crypto-config/peerOrganizations/be2.burakcan-network.com/msp
Policies:
Readers:
Type: Signature
Rule: “OR(‘Burakcan2MSP.admin’, ‘Burakcan2MSP.peer’, ‘Burakcan2MSP.client’)”
Writers:
Type: Signature
Rule: “OR(‘Burakcan2MSP.admin’, ‘Burakcan2MSP.client’)”
Admins:
Type: Signature
Rule: “OR(‘Burakcan2MSP.admin’)”
Endorsement:
Type: Signature
Rule: “OR(‘Burakcan2MSP.peer’)”
AnchorPeers:
– Host: peer0.be2.burakcan-network.com
Port: 7051
Capabilities:
Channel: &ChannelCapabilities
V2_0: true
Orderer: &OrdererCapabilities
V2_0: true
Application: &ApplicationCapabilities
V2_0: true
# Orderer
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
– orderer.burakcan-network.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
– 127.0.0.1:9092
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: “ANY Readers”
Writers:
Type: ImplicitMeta
Rule: “ANY Writers”
Admins:
Type: ImplicitMeta
Rule: “MAJORITY Admins”
BlockValidation:
Type: ImplicitMeta
Rule: “ANY Writers”
Channel: &ChannelDefaults
Policies:
Readers:
Type: ImplicitMeta
Rule: “ANY Readers”
Writers:
Type: ImplicitMeta
Rule: “ANY Writers”
Admins:
Type: ImplicitMeta
Rule: “MAJORITY Admins”
Capabilities:
<<: *ChannelCapabilities
# Application
Application: &ApplicationDefaults
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: “ANY Readers”
Writers:
Type: ImplicitMeta
Rule: “ANY Writers”
Admins:
Type: ImplicitMeta
Rule: “MAJORITY Admins”
LifecycleEndorsement:
Type: ImplicitMeta
Rule: “MAJORITY Endorsement”
Endorsement:
Type: ImplicitMeta
Rule: “MAJORITY Endorsement”
Capabilities:
<<: *ApplicationCapabilities
Profiles:
OrdererGenesis:
<<: *ChannelDefaults
Capabilities:
<<: *ChannelCapabilities
Orderer:
<<: *OrdererDefaults
Organizations:
– *OrdererOrg
Capabilities:
<<: *OrdererCapabilities
Consortiums:
MyFirstConsortium:
Organizations:
– *Burakcan1
– *Burakcan2
ChannelDemo:
Consortium: MyFirstConsortium
<<: *ChannelDefaults
Application:
<<: *ApplicationDefaults
Organizations:
– *Burakcan1
– *Burakcan2
Capabilities:
<<: *ApplicationCapabilities
定义configtx.yaml文件后,我们有序执行以下命令以生成我们在burakcan-network目录下上面提到的配置构件;
生成orderer的创世块;
../bin/configtxgen -profile OrdererGenesis -outputBlock
./channel-artifacts/genesis.block -channelID channelorderergenesis
生成通道配置事务;
../bin/configtxgen -profile ChannelDemo -outputCreateChannelTx
./channel-artifacts/channel.tx -channelID channeldemo
为Burakcan1生成锚peer事务;
../bin/configtxgen -profile ChannelDemo -outputAnchorPeersUpdate ./channel-artifacts/Burakcan1Anchor.tx -channelID channeldemo -asOrg Burakcan1MSP
为Burakcan2生成锚peer事务;
../bin/configtxgen -profile ChannelDemo -outputAnchorPeersUpdate
./channel-artifacts/Burakcan2Anchor.tx -channelID channeldemo -asOrg Burakcan2MSP
然后需要指定docker文件并定义它们的位置,因此需要在burakcan-network目录下创建一些文件。我们在burakcan-network目录和以下docker-compose-base.yaml和peer-base.yaml文件下创建基础文件夹,它们在其中为我们的网络设置了基础。
version: ‘2’
services:
orderer.burakcan-network.com:
container_name: orderer.burakcan-network.com
image: hyperledger/fabric-orderer
environment:
– ORDERER_GENERAL_LOGLEVEL=INFO
– ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
– ORDERER_GENERAL_GENESISMETHOD=file
– ORDERER_GENERAL_GENESISFILE=/opt/gopath/fabric-samples/burakcan-network/channel-artifacts/genesis.block
– ORDERER_GENERAL_LOCALMSPID=OrdererMSP
– ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
# enabled TLS
– ORDERER_GENERAL_TLS_ENABLED=true
– ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
– ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
– ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
– ../channel-artifacts/genesis.block:/opt/gopath/fabric-samples/burakcan-network/channel-artifacts/genesis.block
– ../crypto-config/ordererOrganizations/burakcan-network.com/orderers/orderer.burakcan-network.com/msp:/var/hyperledger/orderer/msp
– ../crypto-config/ordererOrganizations/burakcan-network.com/orderers/orderer.burakcan-network.com/tls/:/var/hyperledger/orderer/tls
– orderer.burakcan-network.com:/var/hyperledger/production/orderer
ports:
– 7050:7050
peer0.be1.burakcan-network.com:
container_name: peer0.be1.burakcan-network.com
extends:
file: peer-base.yaml
service: peer-base
environment:
– GODEBUG=netdns=go
– CORE_PEER_ID=peer0.be1.burakcan-network.com
– CORE_PEER_ADDRESS=peer0.be1.burakcan-network.com:7051
– CORE_PEER_GOSSIP_BOOTSTRAP=peer0.be1.burakcan-network.com:7051
– CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.be1.burakcan-network.com:7051
– CORE_PEER_LOCALMSPID=Burakcan1MSP
volumes:
– /var/run/:/host/var/run/
– ../crypto-config/peerOrganizations/be1.burakcan-network.com/peers/peer0.be1.burakcan-network.com/msp:/etc/hyperledger/fabric/msp
– ../crypto-config/peerOrganizations/be1.burakcan-network.com/peers/peer0.be1.burakcan-network.com/tls:/etc/hyperledger/fabric/tls
– peer0.be1.burakcan-network.com:/var/hyperledger/production
ports:
– 7051:7051
peer0.be2.burakcan-network.com:
container_name: peer0.be2.burakcan-network.com
extends:
file: peer-base.yaml
service: peer-base
environment:
– GODEBUG=netdns=go
– CORE_PEER_ID=peer0.be2.burakcan-network.com
– CORE_PEER_ADDRESS=peer0.be2.burakcan-network.com:7051
– CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.be2.burakcan-network.com:7051
– CORE_PEER_GOSSIP_BOOTSTRAP=peer0.be2.burakcan-network.com:7051
– CORE_PEER_LOCALMSPID=Burakcan2MSP
volumes:
– /var/run/:/host/var/run/
– ../crypto-config/peerOrganizations/be2.burakcan-network.com/peers/peer0.be2.burakcan-network.com/msp:/etc/hyperledger/fabric/msp
– ../crypto-config/peerOrganizations/be2.burakcan-network.com/peers/peer0.be2.burakcan-network.com/tls:/etc/hyperledger/fabric/tls
– peer0.be2.burakcan-network.com:/var/hyperledger/production
ports:
– 9051:7051
version: ‘2’
services:
peer-base:
image: hyperledger/fabric-peer
environment:
– CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
– CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=${COMPOSE_PROJECT_NAME}_byfn
– CORE_LOGGING_LEVEL=INFO
– CORE_PEER_TLS_ENABLED=true
– CORE_PEER_GOSSIP_USELEADERELECTION=true
– CORE_PEER_GOSSIP_ORGLEADER=false
– CORE_PEER_PROFILE_ENABLED=true
– CORE_VM_DOCKER_ATTACHSTDOUT=true
– CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
– CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
– CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
我们只需要创建一个文件。这是从docker-compose-base.yaml文件扩展的docker-compose-cli.yaml文件,必须将其放置在burakcan-network目录的根目录下。通过建立网络或部署,查询和交互chaincode,我们将为每个组织使用一个cli容器,因此我们创建以下文件。
version: ‘2’
volumes:
orderer.burakcan-network.com:
peer0.be1.burakcan-network.com:
peer0.be2.burakcan-network.com:
networks:
byfn:
services:
orderer.burakcan-network.com:
extends:
file: base/docker-compose-base.yaml
service: orderer.burakcan-network.com
container_name: orderer.burakcan-network.com
networks:
– byfn
peer0.be1.burakcan-network.com:
container_name: peer0.be1.burakcan-network.com
extends:
file: base/docker-compose-base.yaml
service: peer0.be1.burakcan-network.com
networks:
– byfn
peer0.be2.burakcan-network.com:
container_name: peer0.be2.burakcan-network.com
extends:
file: base/docker-compose-base.yaml
service: peer0.be2.burakcan-network.com
networks:
– byfn
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
stdin_open: true
dns_search: .
environment:
– GOPATH=/opt/gopath
– CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
#- CORE_LOGGING_LEVEL=DEBUG
– CORE_LOGGING_LEVEL=INFO
– CORE_PEER_ID=cli
– CORE_PEER_ADDRESS=peer0.be1.burakcan-network.com:7051
– CORE_PEER_LOCALMSPID=Burakcan1MSP
– CORE_PEER_TLS_ENABLED=true
– CORE_PEER_TLS_CERT_FILE=/opt/gopath/fabric-samples/burakcan-network/crypto-config/peerOrganizations/be1.burakcan-network.com/peers/peer0.be1.burakcan-network.com/tls/server.crt
– CORE_PEER_TLS_KEY_FILE=/opt/gopath/fabric-samples/burakcan-network/crypto-config/peerOrganizations/be1.burakcan-network.com/peers/peer0.be.burakcan-network.com/tls/server.key
– CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/fabric-samples/burakcan-network/crypto-config/peerOrganizations/be2.burakcan-network.com/peers/peer0.be2.burakcan-network.com/tls/ca.crt
– CORE_PEER_MSPCONFIGPATH=/opt/gopath/fabric-samples/burakcan-network/crypto-config/peerOrganizations/be2.burakcan-network.com/users/[email protected]/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
– /var/run/:/host/var/run/
– ./../chaincode/:/opt/gopath/src/chain
– ./crypto-config:/opt/gopath/fabric-samples/burakcan-network/crypto-config/
– ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
– ./channel-artifacts:/opt/gopath/fabric-samples/burakcan-network/channel-artifacts
depends_on:
– orderer.burakcan-network.com
– peer0.be1.burakcan-network.com
– peer0.be2.burakcan-network.com
networks:
– byfn
然后设置COMPOSE_PROJECT_NAME变量,因为它在我们启动网络时可能会导致问题。
echo COMPOSE_PROJECT_NAME=net > .env
现在我们可以建立我们的网络,并且应该在下图中看到类似的图像;
docker-compose -f docker-compose-cli.yaml up -d