不管是想专一于Composer开发的,仍是本身搭建Fabric网络的,都绕不开Fabric构架这一关。(发现绕过方法的人,请于文末留言,顶礼膜拜~)。本文将经过一个网络组建的实践重点介绍Fabric网络。html
tx: transaction 交易,操做。node
好比去超市买狗粮的时候,从本身的电子钱包转账到咖啡店这个请求,能够看做是一个tx。docker
好比你收到快递,你经过快递公司的应用,点击了“查收”,这个操做能够看做一个tx。编程
每次一你准备更新帐本的操做,均可以视为tx。微信
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.app
因此,在Fabric里,智能合约 = chaincodecomposer
chaincode很重要,也很关键了。不懂chaincode就么有办法玩Fabric网络,但chaincode是用Go写的,你须要先回去学习Go语言,怕怕了吧?o(∩∩)o...哈哈,这个部分通过Composer的封装后,能够用JS来写了。比起彻底本身写chaincode,用composer的话,能够简化很多流程呢。因此,将来的铲S官写完这篇博文后,就去学JS。tcp
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.
细心的童靴应该注意到了 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.
智能合约 又叫作 chaincode,全部的对区块链的操做,都须要经过 chaincode。chaincode 目前支持Go语言,对于这个部分的编程,若是使用 Composer 的话,能够简化流程和节约时间。
经过 chaincode ,咱们能够查询,读写 Blockchain Ledger,Blockchain Ledger 又叫作帐本。由 the world state 和 Blockchain 组成。the world state 主要服务于 chaincode 的查询。而 Blockchain是用于流水线式记录交易。
目标
经过动手启动一个最简单的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,加入技术布道群
如下是咱们的社区介绍,欢迎各类合做、交流、学习:)