Building Your First Network(构建你的第一个网络)

这些指令已经经过验证,可以在提供的tar文件中使用最新的稳定Docker映像和预编译的安装实用程序。如果您使用来自当前主分支的图像或工具运行这些命令,您可能会看到配置和紧急错误。

构建您的第一个网络(BYFN)场景提供了一个示例Hyperledger Fabric网络,该网络由两个组织组成,每个组织维护两个对等节点,以及一个“solo”模式的订购服务。

Install prerequisites(安装先决条件)

在我们开始之前,如果您还没有这样做,您可能希望检查您在开发区块链应用程序和/或操作Hyperledger Fabric的平台上安装了所有先决条件。

您还需要下载并安装Hyperledger Fabric Samples。 您会注意到fabric-samples存储库中包含许多样本。 我们将使用第一个网络样本。 我们现在打开那个子目录。

cd fabric-samples/first-network

本文档中提供的命令必须从fabric-samples存储库克隆的第一个网络子目录运行。 如果您选择从其他位置运行命令,则各种提供的脚本将无法找到二进制文件。

Want to run it now?(想现在运行吗?)

我们提供了一个完全注释的脚本 - byfn.sh--利用这些Docker镜像快速引导Hyperledger Fabric网络,该网络由代表两个不同组织的4个peer和一个orderer节点组成。 它还将启动一个容器来运行脚本执行,该执行将对等方连接到一个通道,部署和实例化链代码并根据部署的链码驱动事务执行。

这是byfn.sh脚本的帮助文本:

./byfn.sh --help Usage: byfn.sh up|down|restart|generate [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] byfn.sh -h|--help (print this message) -m <mode> - one of 'up', 'down', 'restart' or 'generate' - 'up' - bring up the network with docker-compose up - 'down' - clear the network with docker-compose down - 'restart' - restart the network - 'generate' - generate required certificates and genesis block -c <channel name> - channel name to use (defaults to "mychannel") -t <timeout> - CLI timeout duration in seconds (defaults to 10) -d <delay> - delay duration in seconds (defaults to 3) -f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml) -s <dbtype> - the database backend to use: goleveldb (default) or couchdb -l <language> - the chaincode language: golang (default) or node -a - don't ask for confirmation before proceeding Typically, one would first generate the required certificates and genesis block, then bring up the network. e.g.: byfn.sh -m generate -c mychannel byfn.sh -m up -c mychannel -s couchdb

如果您选择不提供频道名称,则脚本将使用默认名称mychannel。 CLI超时参数(使用-t标志指定)是可选值; 如果您选择不设置它,那么CLI将放弃在默认设置10秒后进行的查询请求。

Generate Network Artifacts(生成网络构件)

准备好试一试了吗?好吧!执行以下命令:

./byfn.sh -m generate

您将看到有关将要发生什么的简要说明,以及yes/no命令行提示。 用y响应或点击返回键以执行所描述的操作。

Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10'
Continue? [Y/n] y
proceeding ...
/Users/xxx/dev/fabric-samples/bin/cryptogen

##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
org1.example.com
2017-06-12 21:01:37.334 EDT [bccsp] GetDefault -> WARN 001 Before using BCCSP, please call InitFactories(). Falling back to bootBCCSP.
...

/Users/xxx/dev/fabric-samples/bin/configtxgen
##########################################################
#########  Generating Orderer Genesis block ##############
##########################################################
2017-06-12 21:01:37.558 EDT [common/configtx/tool] main -> INFO 001 Loading configuration
2017-06-12 21:01:37.562 EDT [msp] getMspConfig -> INFO 002 intermediate certs folder not found at [/Users/xxx/dev/byfn/crypto-config/ordererOrganizations/example.com/msp/intermediatecerts]. Skipping.: [stat /Users/xxx/dev/byfn/crypto-config/ordererOrganizations/example.com/msp/intermediatecerts: no such file or directory]
...
2017-06-12 21:01:37.588 EDT [common/configtx/tool] doOutputBlock -> INFO 00b Generating genesis block
2017-06-12 21:01:37.590 EDT [common/configtx/tool] doOutputBlock -> INFO 00c Writing genesis block

#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
2017-06-12 21:01:37.634 EDT [common/configtx/tool] main -> INFO 001 Loading configuration
2017-06-12 21:01:37.644 EDT [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2017-06-12 21:01:37.645 EDT [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

#################################################################
#######    Generating anchor peer update for Org1MSP   ##########
#################################################################
2017-06-12 21:01:37.674 EDT [common/configtx/tool] main -> INFO 001 Loading configuration
2017-06-12 21:01:37.678 EDT [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2017-06-12 21:01:37.679 EDT [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

#################################################################
#######    Generating anchor peer update for Org2MSP   ##########
#################################################################
2017-06-12 21:01:37.700 EDT [common/configtx/tool] main -> INFO 001 Loading configuration
2017-06-12 21:01:37.704 EDT [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2017-06-12 21:01:37.704 EDT [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

第一步生成我们的各种网络实体的所有证书和密钥,用于引导订购服务的创始块,以及配置通道所需的配置事务集合。

Bring Up the Network(打开网络)

接下来,您可以使用以下命令之一启动网络:

./byfn.sh -m up

上面的命令将编译Golang链码images并旋转/启动相应的容器。 Go是默认的链代码语言,但是也支持Node.js链代码。 如果您想通过节点链代码运行本教程,请改为使用以下命令:

# we use the -l flag to specify the chaincode language -l指定链码语言
# forgoing the -l flag will default to Golang

./byfn.sh -m up -l node

有关node.js chaincode填充API的更多信息,请查看Hyperledger Fabric Shim文档。

再次,系统将提示您是继续还是中止。 用y回答或点击返回键:

Starting with channel 'mychannel' and CLI timeout of '10'
Continue? [Y/n]
proceeding ...
Creating network "net_byfn" with the default driver
Creating peer0.org1.example.com
Creating peer1.org1.example.com
Creating peer0.org2.example.com
Creating orderer.example.com
Creating peer1.org2.example.com
Creating cli


 ____    _____      _      ____    _____
/ ___|  |_   _|    / \    |  _ \  |_   _|
\___ \    | |     / _ \   | |_) |   | |
 ___) |   | |    / ___ \  |  _ <    | |
|____/    |_|   /_/   \_\ |_| \_\   |_|

Channel name : mychannel
Creating channel...

日志将从这里继续。这将启动所有容器,然后驱动一个完整的端到端应用程序场景。成功完成后,应在终端窗口报告如下内容:

Query Result: 90
2017-05-16 17:08:15.158 UTC [main] main -> INFO 008 Exiting..... ===================== Query on peer1.org2 on channel 'mychannel' is successful ===================== ===================== All GOOD, BYFN execution completed ===================== _____ _ _ ____ | ____| | \ | | | _ \ | _| | \| | | | | | | |___ | |\ | | |_| | |_____| |_| \_| |____/

您可以滚动浏览这些日志以查看各种事务。 如果您没有得到这个结果,请跳到故障排除部分,看看我们是否可以帮助您发现问题所在。

Bring Down the Network(降低网络)

最后,让我们把它全部放下,这样我们就可以一步一步地探索网络设置。 以下内容将终止您的容器,删除加密材料和四个工件,并从Docker Registry中删除链代码images:

./byfn.sh -m down

再一次,系统将提示您继续,以y回答或点击返回键:

Stopping with channel 'mychannel' and CLI timeout of '10'
Continue? [Y/n] y
proceeding ...
WARNING: The CHANNEL_NAME variable is not set. Defaulting to a blank string.
WARNING: The TIMEOUT variable is not set. Defaulting to a blank string.
Removing network net_byfn
468aaa6201ed
...
Untagged: dev-peer1.org2.example.com-mycc-1.0:latest
Deleted: sha256:ed3230614e64e1c83e510c0c282e982d2b06d148b1c498bbdcc429e2b2531e91
...

如果您想了解有关底层工具和引导机制的更多信息,请继续阅读。 在接下来的部分中,我们将介绍构建全功能Hyperledger Fabric网络的各种步骤和要求。

下面列出的手动步骤假定cli容器中的CORE_LOGGING_LEVEL设置为DEBUG。 您可以通过修改first-network目录中的docker-compose-cli.yaml文件来设置此项。 例如

cli:
  container_name: cli
  image: hyperledger/fabric-tools:$IMAGE_TAG
  tty: true
  stdin_open: true
  environment:
    - GOPATH=/opt/gopath
    - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
    - CORE_LOGGING_LEVEL=DEBUG
    #- CORE_LOGGING_LEVEL=INFO

Crypto Generator(加密生成器/x509证书/签名密匙)

我们将使用加密工具为我们的各种网络实体生成加密材料(x509证书和签名密钥)。这些证书代表身份/标识,它们允许在我们的实体进行通信和交易时进行签名/验证身份验证。


它是如何工作的?
Cryptogen(fabric证书/密匙生成工具)使用一个配置文件 - crypto-config.yaml - 它包含网络拓扑,并允许我们为组织和属于这些组织的组件(peer/orderer)生成一组证书和密钥。每个组织都配置了一个唯一的根证书(ca-cert),用于将特定组件(peer和orderer)绑定到该组织。通过为每个组织分配唯一的CA证书,我们模仿典型的网络,其中参与的成员将使用其自己的证书颁发机构。 Hyperledger Fabric中的事务和通信由实体的私钥(密钥库)签名,然后通过公钥(签名)进行验证。

您将注意到此文件中的count变量。我们用它来指定每个组织的peers数量;在我们的例子中,每个Org(组织)有两个peers。我们现在不会深入研究x.509证书和公钥基础设施的细枝末节。如果您有兴趣,可以在自己的时间内仔细阅读这些主题。

在运行该工具之前,让我们快速浏览一下crypto-config.yaml中的一个片段。特别注意OrdererOrgs标题下的“Name”,“Domain”和“Specs”参数:

OrdererOrgs:
#---------------------------------------------------------
# Orderer
# --------------------------------------------------------
- Name: Orderer Domain: example.com CA: Country: US Province: California Locality: San Francisco # OrganizationalUnit: Hyperledger Fabric # StreetAddress: address for org # default nil # PostalCode: postalCode for org # default nil # ------------------------------------------------------ # "Specs" - See PeerOrgs below for complete description # ----------------------------------------------------- Specs: - Hostname: orderer # ------------------------------------------------------- # "PeerOrgs" - Definition of organizations managing peer nodes # ------------------------------------------------------ PeerOrgs: # ----------------------------------------------------- # Org1 # ---------------------------------------------------- - Name: Org1 Domain: org1.example.com EnableNodeOUs: true

网络实体的命名约定如下 - “{{.Hostname}}.{{.Domain}}”。 因此,使用我们的orderer节点作为参考点,我们留下了一个名为 - orderer.example.com的orderer节点,它与Orderer的MSP ID相关联。 该文件包含有关定义和语法的大量文档。 您还可以参考会员服务提供商(MSP)文档,深入了解MSP。

运行cryptogen工具后,生成的证书和密钥将保存到名为crypto-config的文件夹中。

Configuration Transaction Generator(配置事务生成器)

configtxgen工具用于创建四个配置工件:

  • orderer genesis block,(创世区块)
  • channel configuration transaction,(渠道配置事务)
  • and two anchor peer transactions - one for each Peer Org.(和两个锚点peer事务—每个peer org一个。)

请参阅configtxgen来了解这个工具的功能的完整描述。

orderer块是订购服务的Genesis Block(创世区块),并且通道配置事务文件在Channel创建时广播到orderer。锚点peer事务,正如名称所暗示的那样,锚点对等事务在此通道上指定每个Org的Anchor Peer(锚节点)。

它是如何工作的?
Configtxgen使用一个文件 - configtx.yaml - 它包含示例网络的定义。有三个成员 - 一个Orderer Org(OrdererOrg)和两个Peer Orgs(Org1和Org2),每个成员管理和维护两个对等节点。该文件还指定了一个联盟 - SampleConsortium - 由我们的两个Peer Orgs组成。请特别注意此文件顶部的“配置文件”部分。您会注意到我们有两个唯一标头。一个用于orderer genesis块 - TwoOrgsOrdererGenesis - 一个用于我们的通道 - TwoOrgsChannel。

这些头文件很重要,因为我们将在创建工件时将它们作为参数传递。

请注意,我们的SampleConsortium在系统级配置文件中定义,然后由我们的通道级配置文件引用。 渠道存在于一个联盟的范围内,所有联盟必须在整个网络的范围内定义。

此文件还包含两个值得注意的附加规范。首先,我们为每个Peer Org (peer0.org1.example.com & peer0.org2.example.com)指定锚节点。其次,我们指出每个成员的MSP目录的位置,从而允许我们在orderer genesis块中存储每个Org的根证书。这是一个关键的概念。现在任何与orderer服务通信的网络实体都可以验证其数字签名。

运行工具

您可以使用configtxgen和cryptogen命令手动生成证书/密钥和各种配置工件。 或者,您可以尝试调整byfn.sh脚本来实现目标。

手动生成工件
您可以在byfn.sh脚本中引用generateCerts函数,以获取生成将用于您的网络配置的证书所需的命令,如crypto-config.yaml文件中所定义。 但是,为方便起见,我们也将在此提供参考。

首先让我们运行加密工具。 我们的二进制文件位于bin目录中,因此我们需要提供工具所在位置的相对路径。

../bin/cryptogen generate --config=./crypto-config.yaml

你可在终端机内看到以下资料:

org1.example.com
org2.example.com

证书和密钥(即MSP材料)将输出到第一个网络目录根目录下的目录 - crypto-config。

接下来,我们需要告诉configtxgen工具在哪里查找它需要获取的configtx.yaml文件。 我们将在目前的工作目录中告诉它:

export FABRIC_CFG_PATH=$PWD

然后,我们将调用configtxgen工具生成orderer节点的创世区块

../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

您应该在终端上看到类似如下的输出:

2017-10-26 19:21:56.301 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration 2017-10-26 19:21:56.309 EDT [common/tools/configtxgen] doOutputBlock -> INFO 002 Generating genesis block 2017-10-26 19:21:56.309 EDT [common/tools/configtxgen] doOutputBlock -> INFO 003 Writing genesis block

orderer genesis块和我们即将创建的后续工件将输出到该项目根目录的channel-artifacts目录中。

Create a Channel Configuration Transaction(创建通道配置事务)

接下来,我们需要创建通道事务工件。 请务必替换$ CHANNEL_NAME或将CHANNEL_NAME设置为可在整个说明中使用的环境变量:

#channel.tx工件包含我们的示例通道的定义

export CHANNEL_NAME=mychannel  && ../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

你应该在你的终端机上看到类似以下的输出:

2017-10-26 19:24:05.324 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration 2017-10-26 19:24:05.329 EDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx 2017-10-26 19:24:05.329 EDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

接下来,我们将在我们构建的通道上为Org1定义锚点peer。 同样,请务必替换$ CHANNEL_NAME或为以下命令设置环境变量。 终端输出将模仿通道事务工件的输出:(实际就是定义通道msp)

../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP

现在,我们将在同一个通道上为Org2定义锚点peer:(实际就是定义通道msp)

../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

启动网络
我们将利用脚本来启动我们的网络。 docker-compose文件引用我们先前下载的images,并使用我们之前生成的genesis.block引导orderer。

我们希望手动完成命令,以便公开每个调用的语法和功能。

首先让我们开始你的网络:

docker-compose -f docker-compose-cli.yaml up -d

dfadfasdfsa

猜你喜欢

转载自www.cnblogs.com/apolov-fabric/p/9300621.html