Hyperledger Fabric v1.4(LTS) 系列(5.3):Developing Applications--Process and Data Design

Hyperledger Fabric v1.4(LTS) 系列(5.3):Developing Applications-Process and Data Design

Process and Data Design

Audience: Architects, Application and smart contract developers, Business professionals

This topic shows you how to design the commercial paper processes and their related data structures in PaperNet. Our analysis highlighted that modelling PaperNet using states and transactions provided a precise way to understand what’s happening. We’re now going to elaborate on these two strongly related concepts to help us subsequently design the smart contracts and applications of PaperNet.

译注
实操章节的内容翻译了两三节了,和之前讲概念的第三章比起来,没那么晦涩,有点技术基础的人都比较容易懂了。所以这一部分后边的内容,我就只选自己读起来不是那么一眼就看到底的部分,或者重点的部分翻译一下,权当自己的读书笔记了。

这一节的主题是设计商业票据流程和相关数据结构,需要使用到状态和交易来设计智能合约和商业票据网络应用。

Lifecycle

As we’ve seen, there are two important concepts that concern us when dealing with commercial paper; states and transactions. Indeed, this is true for all blockchain use cases; there are conceptual objects of value, modelled as states, whose lifecycle transitions are described by transactions. An effective analysis of states and transactions is an essential starting point for a successful implementation.

状态和交易是所有区块链应用的重要概念。值的概念对象,被建模为状态,其生命周期通过交易描述。

We can represent the life cycle of a commercial paper using a state transition diagram:

通过状态变化图可以展现商业票据的生命周期。

译注
其实计算机领域太多场景都是需要用到状态图表现的了。

[外链图片转存失败(img-A8kOrcmV-1563460062471)(https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/develop.diagram.4.png)]
The state transition diagram for commercial paper. Commercial papers transition between issued, trading and redeemed states by means of the issue, buy and redeem transactions.

See how the state diagram describes how commercial papers change over time, and how specific transactions govern the life cycle transitions. In Hypledger Fabric, smart contracts implement transaction logic that transition commercial papers between their different states. Commercial paper states are actually held in the ledger world state; so let’s take a closer look at them.

商业票据的状态是通过账本全局状态保存的。

Ledger state

Recall the structure of a commercial paper:

[外链图片转存失败(img-uBvuAB1O-1563460062473)(https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/develop.diagram.5.png)]
A commercial paper can be represented as a set of properties, each with a value. Typically, some combination of these properties will provide a unique key for each paper.

See how a commercial paper Paper property has value 00001, and the Face value property has value 5M USD. Most importantly, the Current state property indicates whether the commercial paper is issued,trading or redeemed. In combination, the full set of properties make up the state of a commercial paper. Moreover, the entire collection of these individual commercial paper states constitutes the ledger world state.

这里有提到了面值和现值。
所有独立的商业票据的状态集合组成了票据的全局状态。

All ledger state share this form; each has a set of properties, each with a different value. This multi-property aspect of states is a powerful feature – it allows us to think of a Fabric state as a vector rather than a simple scalar. We then represent facts about whole objects as individual states, which subsequently undergo transitions controlled by transaction logic. A Fabric state is implemented as a key/value pair, in which the value encodes the object properties in a format that captures the object’s multiple properties, typically JSON. The ledger database can support advanced query operations against these properties, which is very helpful for sophisticated object retrieval.

Fabric状态包含多个属性,因此可以看做向量而非标量。属性值往往用JSON保存,可以对保存属性的账本数据库进行复杂查询(类似MongoDB)。

See how MagnetoCorp’s paper 00001 is represented as a state vector that transitions according to different transaction stimuli:

[外链图片转存失败(img-0DOE4Ski-1563460062474)(https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/develop.diagram.6.png)]
A commercial paper state is brought into existence and transitions as a result of different transactions. Hyperledger Fabric states have multiple properties, making them vectors rather than scalars.

Notice how each individual paper starts with the empty state, which is technically a nil state for the paper, as it doesn’t exist! See how paper 00001 is brought into existence by the issue transaction, and how it is subsequently updated as a result of the buy and redeem transactions.

issue交易之前,状态是存在的,只是值都是nil。

Notice how each state is self-describing; each property has a name and a value. Although all our commercial papers currently have the same properties, this need not be the case for all time, as Hyperledger Fabric supports different states having different properties. This allows the same ledger world state to contain different forms of the same asset as well as different types of asset. It also makes it possible to update a state’s structure; imagine a new regulation that requires an additional data field. Flexible state properties support the fundamental requirement of data evolution over time.

状态都是自描述的,一个Fabric账本全局状态支持以不同形式保存相同的资产,或者压根不同形式的资产,更新状态的结构也是可行的(有点像MongoDB以xml保存数据,并且同时还保存xsd一样)。

State keys

In most practical applications, a state will have a combination of properties that uniquely identify it in a given context – it’s key. The key for a PaperNet commercial paper is formed by a concatenation of the Issuer and paper properties; so for MagnetoCorp’s first paper, it’s MagnetoCorp00001.
A state key allows us to uniquely identify a paper; it is created as a result of the issue transaction and subsequently updated by buy and redeem. Hyperledger Fabric requires each state in a ledger to have a unique key.

When a unique key is not available from the available set of properties, an application-determined unique key is specified as an input to the transaction that creates the state. This unique key is usually with some form of UUID, which although less readable, is a standard practice. What’s important is that every individual state object in a ledger must have a unique key.

state通过混合的属性在一个给定上下文中被唯一确定,此即为state的key。商业票据的key被issue交易创始,然后被buy和redeem更新。
Fabric要求账本中的每个state有唯一的key.

Multiple states

As we’ve seen, commercial papers in PaperNet are stored as state vectors in a ledger. It’s a reasonable requirement to be able to query different commercial papers from the ledger; for example: find all the papers issued by MagnetoCorp, or: find all the papers issued by MagnetoCorp in the redeemed state.

To make these kinds of search tasks possible, it’s helpful to group all related papers together in a logical list. The PaperNet design incorporates the idea of a commercial paper list – a logical container which is updated whenever commercial papers are issued or otherwise changed.

state可以有一个logical list用来进行复杂查询。

Logical representation

It’s helpful to think of all PaperNet commercial papers being in a single list of commercial papers:

外链图片转存失败(img-ZU9RKiFQ-1563803155595)(./develop.diagram.7.png)](https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/develop.diagram.7.png)
MagnetoCorp’s newly created commercial paper 00004 is added to the list of existing commercial papers.

New papers can be added to the list as a result of an issue transaction, and papers already in the list can be updated with buy or redeem transactions. See how the list has a descriptive name: org.papernet.papers; it’s a really good idea to use this kind of DNS name because well-chosen names will make your blockchain designs intuitive to other people. This idea applies equally well to smart contract names.

可以把所有的PaperNet的商票视为一个简单的列表,新的商票通过issue添加到列表。列表名字是org.papernet.papers
给列表起名字的时候可以参考DNS names的规则。

Physical representation

While it’s correct to think of a single list of papers in PaperNet – org.papernet.papers – lists are best implemented as a set of individual Fabric states, whose composite key associates the state with its list. In this way, each state’s composite key is both unique and supports effective list query.
[外链图片转存失败(img-qewMzITA-1563803155596)(https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/develop.diagram.8.png)]
Representing a list of PaperNet commercial papers as a set of distinct Hyperledger Fabric states

但正确的看法应该是把PaperNet的票据单一列表视为Fabric里的一组状态,每个状态的组合key是唯一且支持高效的查询的。

Notice how each paper in the list is represented by a vector state, with a unique composite key formed by the concatenation of org.papernet.paper, Issuer and Paper properties. This structure is helpful for two reasons:

每个票据通过状态向量表示,用三个属性组成唯一key,即 org.papernet.paper, Issuer and Paper

  • It allows us to examine any state vector in the ledger to determine which list it’s in, without reference to a separate list. It’s analogous to looking at set of sports fans, and identifying which team they support by the colour of the shirt they are wearing. The sports fans self-declare their allegiance; we don’t need a list of fans.

  • Hyperlegder Fabric internally uses a concurrency control mechanism to update a ledger, such that keeping papers in separate state vectors vastly reduces the opportunity for shared-state collisions. Such collisions require transaction re-submission, complicate application design, and decrease performance.

This second point is actually a key take-away for Hyperledger Fabric; the physical design of state vectors is very important to optimum performance and behaviour. Keep your states separate!

可以通过状态向量确定state在哪个列表,就像我们要查找一组体育爱好者,并通过其体恤的颜色识别其支持的球队。这些爱好者通过属性自声明其团体,我们不需要一个实际的爱好者列表。
Fabric使用并行算法更新账本,把票据保存在各自的状态向量里大大减少了共享状态带来的冲突。这些冲突会带来事务重新提交,应用设计复杂化,降低性能。

Trust relationships

We have discussed how the different roles in a network, such as issuer, trader or rating agencies as well as different business interests determine who needs to sign off on a transaction. In Fabric, these rules are captured by so-called endorsement policies. The rules can be set on a chaincode granularity, as well as for individual state keys.

This means that in PaperNet, we can set one rule for the whole namespace that determines which organizations can issue new papers. Later, rules can be set and updated for individual papers to capture the trust relationships of buy and redeem transactions.

In the next topic, we will show you how to combine these design concepts to implement the PaperNet commercial paper smart contract, and then an application in exploits it!

我们探讨了网络中不同角色的职责,职责通过 背书策略 实现,可以在链码或者单独的state key的粒度上设置规则。

这意味着在PaperNet,可以对全局或单独的票据设置各种规则。
下一个主题讨论组合这些设计概念来实现商票智能合约。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值