HyperledgerFabric1.4学习笔记(包括官方文档翻译)

HyperledgerFabric1.4学习笔记(包括官⽅⽂档翻译)
⽬录
名词概念
In Hyperledger Fabric, a ledger consists of two distinct, though related, parts – a world state and a blockchain. Each of these represents a set of facts about a set of business objects.
账本由世界状态区块链两部分组成。
世界状态
Firstly, there’s a world state – a database that holds a cache of the current values of a set of ledger states. The world state makes it easy for a program to directly access the current value of a state rather than having to calculate it by traversing the entire transaction log. Ledger states are, by default, expressed as key-value pairs, and we’ll see later how Hyperledger Fabric provides flexibility in this regard. The world state can change frequently, as states can be created, updated and deleted.
世界状态是⼀个数据库,存储的是账本当前值。作⽤是能够快速获取账本最新值⽽不必根据交易⽇志从头开始计算。默认情况下,世界状态以键值对的形式表⽰。
上图是世界状态的⼀个⽰例,记录的是两辆车CAR1和CAR2的信息。应⽤程序可以调⽤(invoke)智能合约来put和delete状态。
The key design point is that only transactions that are signed by the required set of endorsing organizations will result in an update to the world state. If a transaction is not signed by sufficient endorsers, it will not result in a change of world
state. You can read more about how applications use , and how to .
设计的重点是只有当交易被所需的⼀组背书组织签名后才能更新世界状态。
You’ll also notice that a state has an version number, and in the diagram above, states CAR1 and CAR2 are at their starting versions, 0. The version number for internal use by Hyperledger Fabric, and is incremented every time the state changes. The version is checked whenever the state is updated to make sure the current states matches the version at the time of endorsement. This ensures that the world state is changing as expected; that there has not been a concurrent update.
世界状态中还有⼀个属性——版本号,版本号从0开始,每当状态更新时版本号就递增。状态更新时会⾸先检查版本号,以确保当前状态的版本与背书时的版本⼀致(避免并发更新)。
世界状态可以根据区块链推导出来(区块链记录了所以的操作),因此当创建⼀个peer节点时,此节点会⾃动⽣成世界状态。
Secondly, there’s a blockchain – a transaction log that records all the changes that have resulted in the current the world state. Transactions are collected inside blocks that are appended to the blockchain – enabling you to understand the
history of changes that have resulted in the current world state. The blockchain data structure is very different to the world state because once written, it cannot be modified; it is immutable.
区块链是⼀个交易⽇志,记录着导致当前世界状态的所有操作(不管交易有没有效,都会记录到区块链中)。区块链的数据结构和世界状态的数据结构⾮常不同。因为它要保证不可更改。
The blockchain is structured as sequential log of interlinked blocks, where each block contains a sequence of transactions, each transaction representing a query or update to the world state. The exact mechanism by which transactions are ordered is discussed elsewhere; what’s important is that block sequencing, as well as transaction sequencing within blocks, is established when blocks are first created by a Hyperledger Fabric component called the ordering service.
区块链被构造成互连区块的顺序⽇志,每个区块中包含⼀系列的交易,每个交易代表着query或update世界状态。交易的排序机制在其他地⽅讨论。这⾥的重点是,当区块第⼀次被ordering service组件创建的时候,区块顺序以及区块内的交易顺序就被确⽴了。
The blockchain is always implemented as a file, in contrast to the world state, which uses a database. This is a sensible design choice as the blockchain data structure is heavily biased towards a very small set of simple operations. Appending to the end of the blockchain is the primary operation, and query is currently a relatively infrequent operation.
区块链是以⽂件的⽅式实现的,这与使⽤数据库实现的世界状态不同。这种设计⾮常巧妙,因为区块
链数据结构严重偏向于⼀⼩组简单操作。 附加到区块链的末尾是主要操作,查询当前是⼀个相对不频繁的操作。
Finally, as you can see in the diagram, the first block in the blockchain is called the genesis block. It’s the starting point for the ledger, though it does not contain any user transactions. Instead, it contains a configuration transaction containing the initial state of the network channel (not shown). We discuss the genesis block in more detail when we discuss the blockchain network and  in the documentation.
创始块不包含任何⽤户交易,只包含配置交易。配置交易⽤来初始化世界状态。
区块由三个部分组成,分别是区块头、区块数据和区块元数据。
区块头
区块头包含三个属性(区块号、当前区块哈希、前⼀个区块的哈希),当⼀个区块被创建时写⼊。
区块链
Block number: An integer starting at 0 (the genesis block), and increased by 1 for every new block appended to the blockchain.
羟基磷酸钙Current Block Hash: The hash of all the transactions contained in the current block.
Previous Block Hash: A copy of the hash from the previous block in the blockchain.
These fields are internally derived by cryptographically hashing the block data. They ensure that each and every block is inextricably linked to its neighbour, leading to an immutable ledger.磁悬浮支架图片
回油阀
区块数据
This section contains a list of transactions arranged in order. It is written when the block is created b
y the ordering service. These transactions have a rich but straightforward structure, which we describe later in this topic.
区块元数据包含的是排序后的交易列表。当区块被ordering service创建时写⼊。
区块元数据
This section contains the time when the block was written, as well as the certificate, public key and signature of the block writer. Subsequently, the block committer also adds a valid/invalid indicator for every transaction, though this information is not included in the hash, as that is created when the block is created.
区块元数据包括区块的写⼊时间,以及区块写⼊者的证书、公钥和签名。
交易在fabric中指的就是对链代码(即智能合约)的操作,交易分为两种,部署交易(deploy transaction)和调⽤交易(invoke transaction)。
1. 部署交易
部署交易指的是创建新的链代码,并且⽤⼀个程序作为参数,当⼀个部署交易成功执⾏时,链代码就被安装到区块链上了。
2. 调⽤交易
调⽤交易指的是运⾏链代码,链代码执⾏时可能会修改相应的状态,并返回输出。
下图是⼀个交易的详细结构:
⼀个交易主要包含交易头、交易签名、交易提案、交易响应以及⼀个背书列表5个属性(还有其他不重要的属性)。
交易头:包含交易的元数据,如链码名称、版本等
交易签名:包含由客户端应⽤程序创建的加密签名,作⽤是判断交易是否被篡改
交易提案:作⽤是对由应⽤程序提供给智能合约的输⼊参数进⾏编码。当智能合约运⾏时,提案负责将参数传递过去
交易响应:是智能合约的输出,包含的是世界状态在交易前后的值,以读写集的形式展⽰。
背书列表:As shown in E4, this is a list of signed transaction responses from each required organization sufficient to satisfy the endorsement policy. You’ll notice that, whereas only one transaction response is included in the transaction, there are multiple endorsements. That’s because each endorsement effectively encodes its organization’s particular transaction response – meaning that there’s no need to include any transaction response that doesn’t match sufficient endorsements as it will be rejected as invalid, and not update the world state.
各种节点
节点(peer)是区块链的通信实体,是⼀个逻辑概念,不同类型的多个节点可以运⾏在同⼀个物理服务器上。节点主要有以下四种:
客户端节点:客户端必须连接到某⼀个peer节点或排序服务节点上才能与区块链⽹络进⾏通信。客户
端向背书节点(endorser)提交交易提案(transaction proposal),当收集到⾜够背书后,向排序服务⼴播交易提案,进⾏排序,⽣成区块。
普通节点peer:peer节点根据所承担的⾓⾊⼜可以分为记账节点(committer)、背书节点(endorser)、主节点(leader)和锚节点(anchor)。
1. 记账节点:所有的peer节点都是记账节点(committer),负责验证排序服务节点区块⾥的交易,维护状态和总账(Ledger)的副
本。该节点会定期从orderer节点获取包含交易的区块,在对这些区块进⾏核发验证之后,会把这些区块加⼊到区块链中。committer 节点⽆法通过配置⽂件配置,需要在当前客户端或者命令⾏发起交易请求的时候⼿动指定相关的committer节点。记账节点可以有多个。
2. 背书节点:部分节点还会执⾏交易并对结果进⾏签名背书,充当背书节点(endorser)的⾓⾊。背书节点是动态的⾓⾊,是与具体链
码绑定的。每个链码在实例化的时候都会设置背书策略,指定哪些节点对交易背书后交易才是有效的。并且只有应⽤程序向它发起交易背书请求的时候才是背书节点,其他时候都是普通的记账节点,只负责验证交易并记账。背书节点也⽆法通过配置⽂件指定,⽽是由发起交易请求的客户端指定。背书节点可以有多个。
3. 主节点:peer节点还可以是主节点(leader peer),能与排序服务节点通信,负责从排序服务节点获取最新的区块并在组织内部同
丰胸乳液步。主节点在整个组织中只能有⼀个。
4. 锚节点:peer节点还可以是锚节点(anchor peer),锚节点主要负责代表组织和其他组织进⾏信息交换。每个组织都有⼀个锚节
点,锚节点对于组织来说⾮常重要,如果锚节点出现问题,当前组织就会与其他组织失去联系。锚节点的配置信息是在configtxgen 模块的配置⽂件configtx.yaml中配置的。锚节点只能有⼀个。
排序服务节点orderer:接收包含背书签名的交易,对未打包的交易进⾏排序⽣成区块,⼴播给peer节点。排序服务提供的是原⼦⼴播,保证同⼀个链上的节点接收到相同的信息,并且有相同的逻辑顺序。
CA节点:fabric1.0的证书颁发机构,由服务器和客户端组成。CA节点接收客户端的注册申请,返回注册密码⽤于⽤户登录,以便获取⾝份证书。区块链上的所有操作都需要验证⽤户⾝份。
参考:
幼儿园门禁fabric1.0典型交易流程
该流程的前提假设是各节点已经提前颁发好证书,且已正常启动,并加⼊已经创建好的通道。此流程介绍的是在已经实例化了的链码通道上从发起⼀个调⽤交易到最终结账的全过程。
1. 提交交易提案
应⽤程序(客户端节点)构造好交易提案(交易提案中包含本次交易要调⽤的合约标识、合约⽅法和参数信息以及客户端签名等)请求后,根据背书策略选择背书节点执⾏交易提案并进⾏背书签名。背书节点是链代码中背书策略指定的节点。正常情况下背书节点执⾏后的结果是⼀致的,只有背书节点对结果的签名不⼀样。
2. 模拟执⾏提案并进⾏背书
背书节点在收到交易提案后会进⾏⼀些验证,验证通过后,背书节点会根据当前账本数据模拟执⾏链代码中的业务逻辑并⽣成读写集(RwSet)。模拟执⾏时不会更新账本数据。然后背书节点对这些读写集进⾏签名⽣成提案响应(proposal response),然后返回给应⽤程序。
3. 收集交易的背书(返回模拟执⾏结果)
应⽤程序收到proposal response后会对背书节点的签名进⾏验证(所有节点接收到任何消息时都需要先验证消息的合法性)。如果链码只进⾏账本查询操作,应⽤程序只需要检查查询响应,并不会将交易提交给排序服务节点。如果链码对账本进⾏了invoke操作,则需要提交交易给排序服务进⾏账本更新(提交前会判断背书策略是否满⾜)。
4. 构造交易请求并发送给排序服务节点
应⽤程序接收到所有背书节点的签名后,根据背书签名调⽤SDK⽣成交易,并⼴播给排序服务节点。其中⽣成交易的过程很简单,只需要确认所有背书节点的执⾏结果完全⼀致,再将交易提案、提案响应和背书签名打包⽣成交易即可。
5. 排序服务节点对交易进⾏排序并⽣成区块
排序服务节点接收到⽹络中所有通道发出的交易信息,读取交易信封获取通道名称,按各个通道上交
易的接收时间顺序对交易信息进⾏排序(多通道隔离),⽣成区块。(在这个过程中,排序服务节点不会关⼼交易是否正确,只是负责排序和打包。交易的有效性在第7步进⾏验证)
6. 排序服务节点⼴播区块给主节点
排序服务节点⽣成区块后会⼴播给通道上不同组织的主节点。
7. 记账节点验证区块内容并写⼊到账本
所有的peer节点都是记账节点,记录的是节点已加⼊通道的账本数据。记账节点接收到的排序服务节点⽣成的区块后,会验证区块交易的有效性,然后提交到本地账本并产⽣⼀个⽣成区块的事件,监听区块事件的应⽤程序会进⾏后续的处理。(如果接收的是配置区块,则会更新缓存的配置信息)
8. 主节点在组织内部同步最新的区块
如果交易是⽆效的,也会更新区块,但不会更新世界状态。(区块存储的是操作语句,⽽世界状态存储的是被处理的数据)
组织
绝对值角度编码器
fabric系统是通过组织来划分的,每个组织内都有承担不同功能的peer节点,同时每个组织都有⾃⼰对应的fabric-ca服务器
fabric系统中所有的组织共⽤⼀个orderer集。
fabric中的组织在现实世界中可以是⼀个公司、⼀个企业,或者⼀个协会。
在fabric中,组织是承担着数据信⽤责任的区块链系统参与⽅。
在设计⼀个fabric系统时,第⼀步就是要确定系统的参与⽅,然后从这些参与者中选出组织(⽣成对应的组织编号、域名、证书等),然后再确认组织的管理⽅式。组织的管理⽅式是指组织在遇到问题时的协作⽅式(如新组织的加⼊)。
fabric系统逻辑结构图

本文发布于:2024-09-23 18:26:36,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/158427.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:节点   交易   区块   背书   状态   排序
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议