区块链100讲:用一个案例说明Hyperledger Fabric网络构架

无论是想专注于Composer开发的,还是自己搭建Fabric网络的,都绕不开Fabric构架这一关。(发现绕过方法的人,请于文末留言,顶礼膜拜~)。本文将通过一个网络组建的实践重点介绍Fabric网络。

1

Fabric网络概要

tx: transaction 交易,操作。

比如去超市买狗粮的时候,从自己的电子钱包转帐到咖啡店这个请求,可以看作是一个tx。

比如你收到快递,你通过快递公司的应用,点击了“查收”,这个操作可以看作一个tx。

每次一你准备更新账本的操作,都可以视为tx。

2

智能合约

Fabric是做联盟链的。跟公链不同,任何你想对账本的操作,读取,写入,都需要通过 智能合约 才可以发生的。

Hyperledger Fabric smart contracts are written in chaincode and are invoked by an application external to the blockchain when that application needs to interact with the ledger.

所以,在Fabric里,智能合约 = chaincode

chaincode很重要,也很关键了。不懂chaincode就么有办法玩Fabric网络,但chaincode是用Go写的,你需要先回去学习Go语言,怕怕了吧?o(∩∩)o...哈哈,这个部分经过Composer的封装后,可以用JS来写了。比起完全自己写chaincode,用composer的话,可以简化不少流程呢。所以,未来的铲S官写完这篇博文后,就去学JS。

In most cases, chaincode interacts ONLY with the database component of the ledger, the world state (querying it, for example), and NOT the transaction log.

Chaincode can be implemented in several programming languages. The currently supported chaincode language is Go with support for Java and other languages coming in future releases.

3

Blockchain Ledger 账本

细心的童靴应该注意到了 the world state 这个词。这个就是账本的意思么?跟账本是什么关系呢?

Ledger

原来,一个账本里面包含了两部分: the world state 和  Blockchain

The world state represents the current values of all ledger states.

简单粗暴的说,就是区块链里面记录的信息,最终的状态,是由 the world state 来储存的。主要用于上面chaincode的快速查询使用。不用反推所有区块链里面的交易真假。百无聊赖的时候,就可以从 Blockchain 来计算一下,得到新的 the world state。目前存储 the world state可以用: an embedded LevelDB 或者 an external CouchDB。

而账本里面的区块链是: 任何操作的记录

The blockchain is a transaction log, structured as interlinked blocks, where each block contains a sequence of transactions, each of which represents a query or update to the world state.

4

小结

智能合约 又叫做 chaincode,所有的对区块链的操作,都需要通过 chaincode。chaincode 目前支持Go语言,对于这个部分的编程,如果使用 Composer 的话,可以简化流程和节约时间。

通过 chaincode ,我们可以查询,读写 Blockchain Ledger,Blockchain Ledger 又叫做账本。由 the world state 和 Blockchain 组成。the world state 主要服务于 chaincode 的查询。而 Blockchain是用于流水线式记录交易。

5

组建最简网络

目标

通过动手启动一个最简单的Hyperledger Fabric网络,来进一步了解Hyperledger Fabric的网络构成。

这里介绍的步骤,以及代码可能不适用于最新Docker Image,我们在这里主要关注网络结构。

这个网络将包含:

  • 两个参与方

  • 每个参与方有两个节点

  • 有一个 排序节点

准备

Prerequisites(http://hyperledger-fabric.readthedocs.io/en/release-1.1/prereqs.html)

Hyperledger Fabric Samples(http://hyperledger-fabric.readthedocs.io/en/release-1.1/samples.html)

我们将会使用: first-network来演示。

开启我们的第一网络

打开目标目录

cd fabric-samples/first-network
复制代码

在first-network的文件夹下面,有一个包含了我们启动所需要全部步骤(两个参与方,每个参与方维护两个节点,有一个独立的排序节点)的脚本文件 byfn.sh,这个脚本文件附带注解,有兴趣的童靴可以研究一下。

*   查询 byfn.sh的文档

./byfn.sh --help

Usage:   

byfn.sh -m up|down|restart|generate 

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

    byfn.sh -m up -c mychannel -s couchdb -i 1.0.6    byfn.sh -m down -c mychannel

Taking all defaults:

    byfn.sh -m generate

    byfn.sh -m up

    byfn.sh -m down
复制代码

生成网络需要的要素

./byfn.sh -m generate

Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10'

Continue (y/n)? 
复制代码

生成证书,以及一个叫做'mychannel'的通道,并为此通道配置一个区块,最长等待请求的时长为10秒? 输入y。 你可以通过参数来个性化配置网络

然后屏幕上就生成了一大堆的输出。根本就不想看对吧?不想看的童鞋可以直接跳到下一个步骤。

##########################################################

##### Generate certificates using cryptogen tool ###################################################################

org1.example.com

org2.example.com

通过 cryptogen tool 生成数字证书

##########################################################

#########  Generating Orderer Genesis block ###############

#########################################################

2018-06-23 13:32:34.246 JST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-06-23 13:32:34.416 JST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block

2018-06-23 13:32:34.417 JST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block

生成排序节点,以及创世纪区块(第一个区块)

#################################################################

### Generating channel configuration transaction 'channel.tx' ###

#################################################################

2018-06-23 13:32:34.462 JST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-06-23 13:32:34.464 JST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx

2018-06-23 13:32:34.464 JST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

生成渠道配置交易:'channel.tx'

#################################################################

#######    Generating anchor peer update for Org1MSP   ##########

#################################################################

2018-06-23 13:32:34.475 JST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-06-23 13:32:34.479 JST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2018-06-23 13:32:34.480 JST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update

#################################################################

#######    Generating anchor peer update for Org2MSP   #########

##################################################################

2018-06-23 13:32:34.490 JST [common/configtx/tool] main -> INFO 001 Loading configuration

2018-06-23 13:32:34.494 JST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update

2018-06-23 13:32:34.494 JST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
复制代码

生成anchor peer,别且将其分配给各个参与方

启动网络

./byfn.sh -m up

Starting with channel 'mychannel' and CLI timeout of '10'Continue (y/n)? 

启动'mychannel'?
输入y。
复制代码

然后屏幕上又出现了一堆代码,不想看的童靴可以跳过。

Creating network "net_byfn" with the default driver

生成网络

Creating volume "net_orderer.example.com" with default driver

生成排序节点数据

volume是Docker里面,存储数据的一种方法。详细可以参照:https://docs.docker.com/storage/volumes/

Creating volume "net_peer0.org1.example.com" with default driver

Creating volume "net_peer1.org1.example.com" with default driver

Creating volume "net_peer0.org2.example.com" with default driver

Creating volume "net_peer1.org2.example.com" with default driver

生成四个节点数据

Creating peer0.org2.example.com ... done

Creating peer1.org1.example.com ... done

Creating peer1.org2.example.com ... done

Creating peer0.org1.example.com ... done

生成四个节点

Creating orderer.example.com    ... done

生成排序节点

Creating cli                    ... done生成命令行接口
复制代码

Channel name : mychannel

Creating channel...
复制代码

生成渠道

Query Result: 90

2018-06-23 04:57:52.496 UTC [main] main -> INFO 008 Exiting.....

===================== Query on PEER3 on channel 'mychannel' is successful ===================== 

========= All GOOD, BYFN execution completed ===========
复制代码

网络启动后,然后自动执行一连串的点对点的交易,如果交易都顺利完成了,你将看到这个提示。

具体交易流程和内容,可以更加输出的log来确认。我们下次再学习了。

docker ps
复制代码

查看Docker的状态

 CONTAINER ID        IMAGE                                                                                                  COMMAND                  PORTS                                        NAMES

91f7854cce55        dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab   "chaincode -peer.add…"                                                      dev-peer1.org2.example.com-mycc-1.0dc4a6df35ca5        dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9   "chaincode -peer.add…"                                                      dev-peer0.org1.example.com-mycc-1.0d60e45ed340a        dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b   "chaincode -peer.add…"                                                      dev-peer0.org2.example.com-mycc-1.0各个节点上生成的智能合约。(好像少了一个,没有dev-peer1.org1.example.com-mycc-1.0)

ec0de22dfda5        hyperledger/fabric-peer:latest                                                                         "peer node start"        0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.comfbbddcc6e511        hyperledger/fabric-peer:latest                                                                         "peer node start"        0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.comacb521a4dff8        hyperledger/fabric-peer:latest                                                                         "peer node start"        0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com555938e16fcb        hyperledger/fabric-peer:latest                                                                         "peer node start"        0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com节点7f6f7b69635c        hyperledger/fabric-orderer:latest                                                                      "orderer"               

 0.0.0.0:7050->7050/tcp                             orderer.example.com
复制代码

排序节点

关闭网络

在完成交易之后,你希望Docker释放生成的所有: containers, the crypto material, four artifacts, 以及the chaincode images

./byfn.sh -m down
Stopping with channel 'mychannel' and CLI timeout of '10'Continue (y/n)? 

关闭通道'mychannel'?
输入y。

Stopping orderer.example.com    ... done

Stopping peer1.org2.example.com ... done

Stopping peer1.org2.example.com ... done

Stopping peer1.org1.example.com ... done

Stopping peer0.org2.example.com ... done

Removing cli                    ... done

Removing orderer.example.com    ... done

Removing peer0.org1.example.com ... done

Removing peer1.org2.example.com ... done

Removing peer1.org1.example.com ... done

Removing peer0.org2.example.com ... done

Removing network net_byfn
复制代码

再次查看Docker的状态

docker ps

所有的containers都消失了。

本文作者:HiBlock区块链技术布道群-AmyWu

原文发布于简书

加微信baobaotalk_com,加入技术布道群

以下是我们的社区介绍,欢迎各种合作、交流、学习:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值