文章目录

账本(Ledger)

什么是账本(What is a Ledger)?

A ledger contains the current state of a business as a journal of transactions. The earliest European and Chinese ledgers date from almost 1000 years ago, and the Sumerians had stone ledgers 4000 years ago – but let’s start with a more up-to-date example!
帐本包含交易日记帐的业务当前状态。最早的欧洲和中国账本的历史可以追溯到近1000年前,而苏美尔人在4000年前曾有石头账本 - 但让我们从一个最新的例子开始吧!

You’re probably used to looking at your bank account every month. What’s most important to you is the available balance – it’s what you’re able to spend at the current moment in time. If you want to see how your balance was derived, then you can look through the transaction credits and debits that determined it. This is a real life example of a ledger – a state (your bank balance), and a set of ordered transactions (credits and debits) that determine it. Hyperledger Fabric is motivated by these same two concerns – to present the current value of a set of ledger states, and to capture the history of the transactions that determined these states.
你可能习惯每个月查看一下你的银行账户。对你来说最重要的是可用的余额 - 这是你在当前时刻可以花费的。如果你想了解你的余额是如何来的,那么你可以查看交易的贷方和借方来确定它。这是账本的真实例子 - 状态(你的银行余额),以及一组确定它的有序交易(贷方和借方)。 Hyperledger Fabric受到同样两个关注点的驱动 - 呈现一组帐本状态的当前值,并捕获确定这些状态的交易的历史记录。

Let’s take a closer look at the Hyperledger Fabric ledger structure!
让我们仔细看看Hyperledger Fabric帐本结构!

区块链账本(A Blockchain Ledger)

A blockchain ledger consists of two distinct, though related, parts – a world state and a blockchain.
区块链帐本由两个不同但相关的部分组成 - 世界状态和区块链。

Firstly, there’s a world state – a database that holds the current values of a set of ledger states. The world state makes it easy for a program to get the current value of these states, rather than having to calculate them by traversing the entire transaction log. Ledger states are, by default, expressed as key-value pairs, though we’ll see later that Hyperledger Fabric provides flexibility in this regard. The world state can change frequently, as states can be created, updated and deleted.
首先,有一个世界状态 - 一个数据库,它保存一组分类账状态的当前值。世界状态使程序可以轻松获取这些状态的当前值,而不必通过遍历整个交易日志来计算它们。默认情况下,账本状态表示为键值对,但我们稍后会看到Hyperledger Fabric在这方面提供了灵活性。世界状态可以经常变化,因为状态可以创建、更新和删除。

Secondly, there’s a blockchain – a transaction log that records all the changes that determine 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 an immutable sequence of blocks, each of which contains a set of ordered transactions.
其次,有一个区块链 - 一个记录决定世界状态的所有变化的交易日志。交易在附加到区块链中的区块内收集 - 使你能够了解导致当前世界状态的更改历史记录。区块链数据结构与世界状态非常不同,因为一旦编写,就无法修改。它是一个不可变的区块序列,每个区块都包含一组有序的交易。

Hyperledger Fabric 1.3 官方文档翻译(三)关键概念 (Key Concepts) - 3.9 账本 (Ledger)-LMLPHP

The visual vocabulary expressed in facts is as follows: Ledger L comprises blockchain B and World State W. Blockchain B determines World State W. Also expressed as: World state W is derived from blockchain B.
*据实表达的视觉词汇如下:账本L包括区块链B和世界状态W。区块链B确定世界状态W。也表示为:世界状态W源自区块链B。 *

It’s helpful to think of there being one logical ledger in a Hyperledger Fabric network. In reality, the network maintains multiple copies of a ledger – which are kept consistent with every other copy through a process called consensus. The term Distributed Ledger Technology (DLT) is often associated with this kind of ledger – one that is logically singular, but has many consistent copies distributed throughout a network.
考虑在Hyperledger Fabric网络中存在一个逻辑账本是有帮助的。 实际上,网络维护一个帐本的多个副本 - 通过一个名为共识的过程与其他所有副本保持一致。 术语**分布式帐本技术(DLT)**通常与这种帐本相关联 - 这种帐本在逻辑上是单一的,但在整个网络中分布有许多一致的副本。

Let’s now examine the world state and blockchain data structures in more detail.
现在让我们更详细地研究世界状态和区块链数据结构。

世界状态(World State)

The world state represents the current values of all ledger states. It’s extremely useful because programs usually need the current value of a ledger state and that’s always easily available. You do not need to traverse the entire blockchain to calculate the current value of any ledger state – you just get it directly from the world state.
世界状态代表所有帐本状态的当前值。 它非常有用,因为程序通常需要而且总是很容易获得帐本状态的当前值。 你不需要遍历整个区块链来计算任何帐本状态的当前值 - 你只需直接从世界状态获取它。

Hyperledger Fabric 1.3 官方文档翻译(三)关键概念 (Key Concepts) - 3.9 账本 (Ledger)-LMLPHP

The visual vocabulary expressed in facts is as follows: There is a ledger state with key=CAR1 and value=Audi. There is a ledger state with key=CAR2 and a more complex value {model:BMW, color=red, owner=Jane}. Both states are at version 0.
据实表达的视觉词汇如下:有一个帐本状态,key=CAR1,value=Audi。有一个帐本状态,其中key=CAR2且值更复杂{model:BMW,color=red,owner=Jane}。这两个状态的版本都是0。

Ledger state is used to record application information to be shared via the blockchain. The example above shows ledger states for two cars, CAR1 and CAR2. You can see that states have a key and a value. Your application programs invoke chaincode which access states via simple APIs – they get, put and delete states using a state key. Notice how a state value can be simple (Audi…) or complex (type:BMW…).
帐本状态用于记录要通过区块链共享的应用程序信息。上面的例子显示了两辆汽车的帐本状态,CAR1和CAR2。你可以看到状态具有键和值。你的应用程序调用通过简单API访问状态的链代码 - 它们使用状态的key get、** put delete **状态。注意状态值是如何简单(Audi…)或复杂(type:BMW…)。

Physically, the world state is implemented as a database. This makes a lot of sense because a database provides a rich set of operators for the efficient storage and retrieval of states. We’ll see later that Hyperledger Fabric can be configured to use different world state databases to address the needs of different types of state values and the access patterns required by applications, for example in complex queries.
在物理上,世界状态是作为数据库实现的。这很有意义,因为数据库提供了丰富的运算符集,可以有效地存储和检索状态。稍后我们将看到Hyperledger Fabric可以配置为使用不同的世界状态数据库来满足不同类型的状态值和应用程序所需的访问模式的需求,例如在复杂查询中。

Transactions capture changes to the world state, and as you’d expect, transactions have a lifecycle. They are created by applications, and finally end up being committed to the ledger blockchain. The whole lifecycle is described in detail here; but the key design point for Hyperledger Fabric is that only transactions that are signed by a set of endorsing organizations will result in an update to the world state. If a transaction is not signed by sufficient endorsers, then it will fail this validity check, and will not result in an update to the world state.
交易捕获对世界状态的更改,正如你所期望的,交易具有生命周期。它们由应用程序创建,最终被提交到帐本区块链。这里详细描述了整个生命周期;但Hyperledger Fabric的关键设计点是,只有一组认可组织签名的交易才会导致对世界状态的更新。如果交易未由足够的背书人签名,那么它将无法通过此有效性检查,并且不会导致对世界状态的更新。

You’ll also notice that a state has a version number, and in the diagram above, states CAR1 and CAR2 are at their starting versions, 0. The version number of a state is incremented every time the state changes. It is also checked whenever the state is updated – to make sure it matches the version when the transaction was created. This check ensures that the world state changing from the same expected value to the same expected value as when the transaction was created.
你还会注意到状态具有版本号,并且在上图中,状态CAR1和CAR2处于其起始版本0。每次状态更改时,状态的版本号都会递增。每当状态更新时也会检查它 - 确保它与创建交易时的版本匹配。此检查确保世界状态在创建交易时从相同的预期值更改为相同的预期值

Finally, when a ledger is first created, the world state is empty. Because any transaction which represents a valid change to world state is recorded on the blockchain, it means that the world state can be re-generated from the blockchain at any time. This can be very convenient – for example, the world state is automatically generated when a peer is created. Moreover, if a peer fails abnormally, the world state can be regenerated on peer restart, before transactions are accepted.
最后,当首次创建帐本时,世界状态为空。因为任何代表世界状态有效变化的交易都记录在区块链上,这意味着可以随时从区块链重新生成世界状态。这可能非常方便 - 例如,在创建对等节点时自动生成世界状态。此外,如果对等节点异常失败,则可以在接受交易之前在对等节点重启时重新生成世界状态。

区块链(Blockchain)

Let’s now turn our attention from the ledger world state to the ledger blockchain.
现在让我们将注意力从账本世界状态转移到账本区块链。

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. 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.
区块链是一个交易日志,结构为互连区块,每个区块包含一系列交易,每个交易代表对世界状态的查询或更新。 其他地方讨论了排序交易的确切机制 - 重要的是区块排序,以及区块内的交易排序是在首次创建区块时建立的。

Each block’s header includes a hash of the block’s transactions, as well a copy of the hash of the prior block’s header. In this way, all transactions on the ledger are sequenced and cryptographically linked together. This hashing and linking makes the ledger data very secure. Even if one node hosting the ledger was tampered with, it would not be able to convince all the other nodes that it has the ‘correct’ blockchain because the ledger is distributed throughout a network of independent nodes.
每个区块的头部包括区块的交易的hash,以及先前区块的头部hash的副本。通过这种方式,账本上的所有交易都按顺序排列并以加密方式链接在一起。这种散列和链接使得帐本数据非常安全。即使托管帐本的一个节点被篡改,它也无法说服所有其他节点它具有“正确”的区块链,因为帐本分布在整个独立节点的网络中。

Physically, 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.
物理上,区块链总是作为文件实现,与使用数据库的世界状态形成对比。这是一种明智的设计选择,因为区块链数据结构严重偏向于一小组简单操作。附加到区块链的末尾是主要操作,查询是当前一个相对不频繁的操作。

Let’s have a look at the structure of a blockchain in a little more detail.
让我们更详细地看一下区块链的结构。

Hyperledger Fabric 1.3 官方文档翻译(三)关键概念 (Key Concepts) - 3.9 账本 (Ledger)-LMLPHP

The visual vocabulary expressed in facts is as follows: Blockchain B contains blocks B0, B1, B2, B3. B0 is the first block in the blockchain, the genesis block
据实表达的视觉词汇如下:区块链B包含区块B0、B1、B2、B3。 B0是区块链中的第一个区块,即起源区块。

In the above diagram, we can see that block B2 has a block data D2 which contains all its transactions: T5, T6, T7.
在上图中,我们可以看到区块 B2具有区块数据 D2,其包含它所有交易:T5、T6、T7。

Most importantly, B2 has a block header H2, which contains a cryptographic hash of all the transactions in D2 as well as with the equivalent hash from the previous block B1. In this way, blocks are inextricably and immutably linked to each other, which the term blockchain so neatly captures!
最重要的是,B2具有区块头 H2,其包含D2中所有交易的加密hash以及来自前一块B1的等效hash。通过这种方式,区块彼此之间有着密不可分和不可变化的联系,术语区块链如此巧妙地录制!

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 channels in the documentation.
最后,正如你在图中看到的,区块链中的第一个块称为** 起源区块**。它是帐本的起点,但它不包含任何用户交易。相反,它包含网络通道初始状态(未显示出)的配置交易。当我们在文档中讨论区块链网络和通道时,我们会更详细地讨论创世块。

区块(Blocks)

Let’s have a closer look at the structure of a block. It consists of three sections
让我们仔细看看一个区块的结构。 它由三部分组成

  • Block Header
    区块头

    This section comprises three fields, written when a block is created.
    此部分包含三个字段,在创建块时写入。

    • Block number: An integer starting at 0 (the genesis block), and increased by 1 for every new block appended to the blockchain.
      区块号:从0(起源块)开始的整数,并且对于附加到区块链的每个新区块增加1。
    • Current Block Hash: The hash of all the transactions contained in the current block.
      当前区块Hash:当前区块中包含的所有交易的哈希值。
    • Previous Block Hash: A copy of the hash from the previous block in the blockchain.
      上一个区块Hash:区块链中前一个区块的哈希副本。

Hyperledger Fabric 1.3 官方文档翻译(三)关键概念 (Key Concepts) - 3.9 账本 (Ledger)-LMLPHP

The visual vocabulary expressed in facts is as follows: Block header H2 of block B2 consists of block number 2, the hash CH2 of the current block data D2, and a copy of a hash PH1 from the previous block, block number 1.
*据实表达的视觉词汇如下:区块B2的区块头H2由区块号2、当前区块数据D2的散列CH2和来自前一块(区块号1)的散列PH1的副本组成。

  • Block Data
    区块数据

    This section contains a list of transactions arranged in order. It is written when the block is created. These transactions have a rich but straightforward structure, which we describe later in this topic.
    本节包含按顺序排列的交易清单。 它是在创建区块时写入的。 这些交易具有丰富但直接的结构,我们将在本主题后面介绍。

  • Block Metadata
    区块元数据

    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.
    此部分包含写入区块的时间,以及区块编写者的证书、公钥和签名。 随后,区块提交者还为每个交易添加了一个有效/无效的指示符,尽管此信息未包含在hash中,因为创建块时才会创建该信息。

交易(Transactions)

As we’ve seen, a transaction captures changes to the world state. Let’s have a look at the detailed blockdata structure which contains the transactions in a block.
正如我们所见,交易采集了世界状态的变化。 让我们看一下详细的区块数据结构,它包含一个区块中的交易。

Hyperledger Fabric 1.3 官方文档翻译(三)关键概念 (Key Concepts) - 3.9 账本 (Ledger)-LMLPHP

The visual vocabulary expressed in facts is as follows: Transaction T4 in blockdata D1 of block B1 consists of transaction header, H4, a transaction signature, S4, a transaction proposal P4, a transaction response, R4, and a list of endorsements, E4.
据实表达的视觉词汇如下:区块B1的区块数据D1中的交易T4包括交易头H4、交易签名S4、交易提议P4、交易响应R4和背书列表E4。

In the above example, we can see the following fields:
在上面的示例中,我们可以看到以下字段:

  • Header

    This section, illustrated by H4, captures some essential metadata about the transaction – for example, the name of the relevant chaincode, and its version.
    该部分(由H4说明)采集有关交易的一些基本元数据 - 例如,相关链代码的名称及其版本。

  • Signature
    签名

    This section, illustrated by S4, contains a cryptographic signature, created by the client application. This field is used to check that the transaction details have not been tampered with, as it requires the application’s private key to generate it.
    该部分(由S4说明)包含由客户端应用程序创建的加密签名。此字段用于检查交易详细信息是否未被篡改,因为它需要应用程序的私钥来生成它。

  • Proposal
    提案

    This field, illustrated by P4, encodes the input parameters supplied by an application to the chaincode which creates the proposed ledger update. When the chaincode runs, this proposal provides a set of input parameters, which, in combination with the current world state, determines the new world state.
    该字段(由P4说明)将应用程序提供的输入参数编码到链代码,该链代码创建提议的帐本更新。当链代码运行时,该提案提供了一组输入参数,这些参数与当前世界状态一起确定新的世界状态。

  • Response
    响应

    This section, illustrated by R4, captures the before and after values of the world state, as a Read Write set (RW-set). It’s the output of a chaincode, and if the transaction is successfully validated, it will be applied to the ledger to update the world state.
    该部分(由R4说明)采集世界状态的前后值,作为读写集(RW集)。它是链代码的输出,如果交易成功验证,它将应用于帐本以更新世界状态。

  • Endorsements
    背书

    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.
    如E4所示,这是来自每个所需组织的签名交易响应列表,足以满足背书策略。 你会注意到,虽然交易中只包含一个交易响应,但有多个背书。 这是因为每个背书都有效地编码了其组织的特定交易响应 - 这意味着不需要包含任何不符合足够背书的交易响应,因为它将被拒绝为无效,并且不会更新世界状态。

That concludes the major fields of the transaction – there are others, but these are the essential ones that you need to understand to have a solid understanding of the ledger data structure.
总结了交易的主要领域 - 还有其他领域,但这些是你需要了解的重要领域,以便对帐本数据结构有充分的了解。

世界状态数据库选项(World State database options)

The world state is physically implemented as a database, to provide simple and efficient storage and retrieval of ledger states. As we’ve seen, ledger states can have simple or complex values, and to accommodate this, the world state database implementation can vary, allowing these values to be efficiently implemented. Options for the world state database currently include LevelDB and CouchDB.
世界状态在物理上实现为数据库,以提供简单有效的存储和检索帐本状态。正如我们所看到的,帐本状态可以具有简单或复杂的值,为了适应这种情况,世界状态数据库的实现可以变化,从而允许有效地实现这些值。世界状态数据库的选项目前包括LevelDB和CouchDB。

LevelDB is the default and is particularly appropriate when ledger states are simple key-value pairs. A LevelDB database is closely co-located with a network node – it is embedded within the same operating system process.
LevelDB是默认的,当帐本状态是简单的键值对时尤其适用。LevelDB数据库与网络节点紧密共存 - 它嵌入在同一操作系统进程中。

CouchDB is a particularly appropriate choice when ledger states are structured as JSON documents because CouchDB supports the rich queries and update of richer data types often found in business transactions. Implementation-wise, CouchDB runs in a separate operating system process, but there is still a 1:1 relation between a network node and a CouchDB instance. All of this is invisible to chaincode. See CouchDB as the StateDatabase for more information on CouchDB.
当分类帐状态结构化为JSON文档时,CouchDB是一个特别合适的选择,因为CouchDB支持丰富的查询和更新业务交易中常见的更丰富的数据类型。在实现方面,CouchDB在单独的操作系统进程中运行,但网络节点和CouchDB实例之间仍然存在1:1的关系。所有这些对于链代码都是不可见的。有关CouchDB的更多信息,请参见CouchDB作为状态数据库

In LevelDB and CouchDB, we see an important aspect of Hyperledger Fabric – it is pluggable. The world state database could be a relational data store, or a graph store, or a temporal database. This provides great flexibility in the types of ledger states that can be efficiently accessed, allowing Hyperledger Fabric to address many different types of problems.
在LevelDB和CouchDB中,我们看到了Hyperledger Fabric的一个重要方面 - 它是可插拔的。世界状态数据库可以是关系数据存储,或图形存储,或暂存数据库。这为可以有效访问的帐本状态类型提供了极大的灵活性,允许Hyperledger Fabric解决许多不同类型的问题。

账本示例:fabcar(Example Ledger: fabcar)

As we end this topic on the ledger, let’s have a look at a sample ledger. If you’ve run the fabcar sample application, then you’ve created this ledger.
当我们结束帐本这个主题时,让我们看一下样本帐本。 如果你运行fabcar示例应用程序,那么你已经创建了此帐本。

The fabcar sample app creates a set of 10 cars, of different color, make, model and owner. Here’s what the ledger looks like after the first four cars have been created.
fabcar样本应用程序创建了一组10辆不同颜色、品牌、型号和所有者的汽车。 这是在创建前四辆车后,帐本的样子。

Hyperledger Fabric 1.3 官方文档翻译(三)关键概念 (Key Concepts) - 3.9 账本 (Ledger)-LMLPHP

The visual vocabulary expressed in facts is as follows: The ledger L, comprises a world state, W and a blockchain, B. W contains four states with keys: CAR1, CAR2, CAR3 and CAR4. B contains two blocks, 0 and 1. Block 1 contains four transactions: T1, T2, T3, T4.
据实表达的视觉词汇如下:帐本L包括世界状态W和区块链B。W包含四个键为:CAR0、CAR1、CAR2和CAR3的状态。 B包含两个区块,0和1。区块1包含四个交易:T1,T2,T3,T4。

We can see that the ledger world state contains states that correspond to CAR0, CAR1, CAR2 and CAR3. CAR0 has a value which indicates that it is a blue Toyota Prius, owned by Tomoko, and we can see similar states and values for the other cars. Moreover, we can see that all car states are at version number 0, indicating that this is their starting version number – they have not been updated since they were created.
我们可以看到账本世界状态包含对应于CAR0、CAR1、CAR2和CAR3的状态。 CAR0有一个值,表明它是由Tomoko拥有的蓝色丰田普锐斯,我们可以看到其他车型的类似状态和值。此外,我们可以看到所有汽车状态都是版本号0,表明这是他们的起始版本号 - 它们自创建之后就没有更新过。

We can also see that the ledger blockchain contains two blocks. Block 0 is the genesis block, though it does not contain any transactions that relate to cars. Block 1 however, contains transactions T1, T2, T3, T4 and these correspond to transactions that created the initial states for CAR0 to CAR3 in the world state. We can see that block 1 is linked to block 0.
我们还可以看到帐本区块链包含两个区块。区块0是起源区块,但它不包含任何与汽车相关的交易。然而,区块1包含交易T1、T2、T3、T4,并且这些交易对应于在世界状态中为CAR0到CAR3创建初始状态的交易。我们可以看到区块1链接到区块0。

We have not shown the other fields in the blocks or transactions, specifically headers and hashes. If you’re interested in the precise details of these, you will find a dedicated reference topic elsewhere in the documentation. It gives you a fully worked example of an entire block with its transactions in glorious detail – but for now, you have achieved a solid conceptual understanding of a Hyperledger Fabric ledger. Well done!
我们没有在区块或交易中显示其他字段,特别是头和哈希。如果你对这些内容的具体细节感兴趣,可以在文档的其他地方找到专门的参考主题。它为你提供了一个完整区块的完整可工作示例,其交易具有极好的细节 - 但是现在,你已经对Hyperledger Fabric帐本得到了可靠的概念性理解。做得好!

更多信息(More information)

See the Transaction Flow, Read-Write set semantics and CouchDB as the StateDatabase topics for a deeper dive on transaction flow, concurrency control, and the world state database.
参见交易流程读写集语义CouchDB作为状态数据库主题,深入探讨交易流程、并发控制和世界状态数据库。

10-05 10:45