温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Hyperledger Fabric网络构架的示例分析

发布时间:2021-12-06 14:46:43 来源:亿速云 阅读:135 作者:小新 栏目:互联网科技

这篇文章主要为大家展示了“Hyperledger Fabric网络构架的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Hyperledger Fabric网络构架的示例分析”这篇文章吧。

1

Fabric网络概要

Hyperledger Fabric网络构架的示例分析

tx: transaction 交易,操作。

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

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

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

2

智能合约

Hyperledger Fabric网络构架的示例分析

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 这个词。这个就是账本的意思么?跟账本是什么关系呢?

Hyperledger Fabric网络构架的示例分析

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生成命令行接口

Hyperledger Fabric网络构架的示例分析

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 ===========

Hyperledger Fabric网络构架的示例分析

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

具体交易流程和内容,可以更加输出的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都消失了。

以上是“Hyperledger Fabric网络构架的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI