博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
血浆现金区块链状态的数据结构
阅读量:2511 次
发布时间:2019-05-11

本文共 15013 字,大约阅读时间需要 50 分钟。

Hello, dear Habr users! This article is about Web 3.0 — the decentralized Internet. Web 3.0 introduces the concept of decentralization as the foundation of the modern Internet. Many computer systems and networks require security and decentralization features to meet their needs. A distributed registry using blockchain technology provides efficient solutions for decentralization.

您好,尊敬的Habr用户! 本文是关于Web 3.0(分散式Internet)的。 Web 3.0引入了分散化的概念,将其作为现代Internet的基础。 许多计算机系统和网络都需要安全性和分散功能来满足其需求。 使用区块链技术的分布式注册表为去中心化提供了有效的解决方案。

Blockchain is a distributed registry. You can think of it as a huge database that lives forever, never changing over the course of time. The blockchain provides the basis for decentralized web applications and services.

区块链是一个分布式注册表。 您可以将其视为一个永久存在的巨大数据库,并且不会随着时间的流逝而改变。 区块链为分散的Web应用程序和服务提供了基础。

However, the blockchain is more than just a database. It serves to increase security and trust between network members, enhancing online business transactions.

但是,区块链不仅仅是一个数据库。 它用于增强网络成员之间的安全性和信任度,从而增强在线业务交易。

Byzantine consensus increases network reliability and solves the problem of consistency.

拜占庭共识提高了网络可靠性并解决了一致性问题。

The scalability provided by DLT changes existing business networks.

DLT提供的可伸缩性改变了现有的业务网络。

Blockchain offers new, very important benefits:

区块链提供了新的非常重要的好处:

  1. Prevents costly mistakes.

    防止代价高昂的错误。
  2. Ensures transparent transactions.

    确保透明的交易。
  3. Digitalizes real goods.

    数字化真实商品。
  4. Enforces smart contracts.

    强制执行智能合约。
  5. Increases the speed ​​and security of payments.

    提高付款速度和安全性。

We have developed a special PoE to research cryptographic protocols and improve existing DLT and blockchain solutions.

我们已经开发了一种特殊的PoE,以研究密码协议并改善现有的DLT和区块链解决方案。

Most public registry systems lack the property of scalability, making their throughput rather low. For example, Ethereum processes only ~ 20 tx/s.

大多数公共注册系统缺乏可伸缩性,因此其吞吐量相当低。 例如,以太坊仅处理〜20 tx / s。

Many solutions were developed to increase scalability while maintaining decentralization. However, only 2 out of 3 advantages — scalability, security, and decentralization — can be achieved simultaneously.

开发了许多解决方案以增加可伸缩性,同时又保持分散性。 但是,只能同时实现三分之二的优势(可伸缩性,安全性和分散性)。

The use of sidechains provides one of the most effective solutions.

侧链的使用提供了最有效的解决方案之一。

等离子概念 (The Plasma Concept)

The Plasma concept boils down to the idea that a root chain processes a small number of commits from child chains, thereby acting as the most secure and final layer for storing all intermediate states. Each child chain works as its own blockchain with its own consensus algorithm, but there are a few important caveats.

“等离子”概念可以归结为以下概念:根链处理子链中的少量提交,从而充当存储所有中间状态的最安全,也是最后一层。 每个子链都具有自己的共识算法,作为自己的区块链,但是有一些重要的警告。

  • Smart contracts are created in a root chain and act as checkpoints for child chains within the root chain.

    智能合约在根链中创建,并充当根链中子链的检查点。
  • A child chain is created and functions as its own blockchain with its own consensus. All states in the child chain are protected by fraud proofs that ensure all transitions between states are valid and apply a withdrawal protocol.

    创建子链,并以自己的共识将其用作自己的区块链。 子链中的所有州都受到欺诈证据的保护,这些欺诈证据可确保州之间的所有过渡有效且适用撤回协议。
  • Smart contracts specific to DApp or child chain application logic can be deployed in the child chain.

    可以在子链中部署特定于DApp的智能合约或子链应用逻辑。
  • Funds can be transferred from the root chain to the child chain.

    资金可以从根链转移到子链。

Validators are given economic incentives to act honestly and send commitments to the root chain — the final transaction settlement layer.

向验证者提供经济诱因,使其诚实行事并向根链(即最终交易结算层)发送承诺。

As a result, DApp users working in the child chain do not have to interact with the root chain at all. In addition, they can place their money in the root chain whenever they want, even if the child chain is hacked. These exits from the child chain allow users to securely store their funds with Merkle proofs, confirming ownership of a certain amount of funds.

结果,在子链中工作的DApp用户根本不必与根链进行交互。 此外,即使子链遭到黑客入侵,他们也可以随时将其资金放在根链中。 子链的这些出口使用户可以使用Merkle证明安全地存储他们的资金,从而确认一定数量的资金的所有权。

Plasma's main advantages are related to its ability to significantly facilitate calculations that overload the main chain. In addition, the Ethereum blockchain can handle more extensive and parallel data sets. Time removed from the root chain is also transferred to Ethereum nodes, which have lower processing and storage requirements.

Plasma的主要优势与它显着促进使主链超载的计算的能力有关。 此外,以太坊区块链可以处理更广泛和并行的数据集。 从根链中删除的时间也转移到以太坊节点,这些节点的处理和存储要求较低。

等离子现金 (Plasma Cash)

assigns unique serial numbers to online tokens. The advantages of this scheme include no need for confirmations, simpler support for all types of tokens (including Non-fungible tokens), and mitigation against mass exits from a child chain.

为在线令牌分配唯一的序列号。 该方案的优点包括无需确认,对所有类型的令牌(包括不可替代令牌)的更简单支持以及对子链大量退出的缓解。

The concept of “mass exits” from a child chain is a problem faced by Plasma. In this scenario, coordinated simultaneous withdrawals from a child chain could potentially lead to insufficient computing power to withdraw all funds. As a result, users may lose funds.

子链的“大量出口”的概念是Plasma面临的一个问题。 在这种情况下,协调一致地从子链中同时取款可能会导致计算能力不足以提取所有资金。 结果,用户可能会损失资金。

实施等离子的选项 (Options for implementing Plasma)

Basic Plasma has a lot of implementation options.

基本等离子有很多实现选项。

The main differences refer to:

主要区别在于:

  • archiving information about state storage and presentation methods;

    归档有关状态存储和表示方法的信息;
  • token types (divisible, indivisible);

    令牌类型(可分割,不可分割);
  • transaction security;

    交易安全;
  • consensus algorithm type.

    共识算法类型。

The main variations of Plasma include:

等离子的主要变化包括:

  • UTXO-based Plasma — each transaction consists of inputs and outputs. A transaction can be conducted and spent. The list of unspent transactions is the state of a child chain itself.

    基于UTXO的等离子-每个事务都由输入和输出组成。 可以进行交易并花费。 未花费的交易清单是子链本身的状态。
  • Account-based Plasma — this structure contains each account's reflection and balance. It is used in Ethereum, since each account can be of two types: a user account and a smart contract account. Simplicity is an important advantage of account-based Plasma. At the same time, the lack of scalability is a disadvantage. A special property, «nonce,» is used to prevent the execution of a transaction twice.

    基于帐户的等离子-此结构包含每个帐户的反映和平衡。 它在以太坊中使用,因为每个帐户可以有两种类型:用户帐户和智能合约帐户。 简单性是基于帐户的血浆的重要优势。 同时,缺乏可伸缩性是一个缺点。 特殊属性“ nonce”用于防止两次执行事务。

In order to understand the data structures used in the Plasma Cash blockchain and how commitments work, it is necessary to clarify the concept of Merkle Tree.

为了了解Plasma Cash区块链中使用的数据结构以及承诺如何工作,有必要阐明Merkle Tree的概念。

默克尔树及其在等离子中的用途 (Merkle Trees and their use in Plasma)

Merkle Tree is an extremely important data structure in the blockchain world. It allows us to capture a certain data set and hide the data, yet prove that some information was in the set. For example, if we have ten numbers, we could create a proof for these numbers and then prove that some particular number was in this set. This proof would have a small constant size, which makes it inexpensive to publish in Ethereum.

Merkle Tree是区块链世界中极其重要的数据结构。 它使我们可以捕获特定的数据集并隐藏数据,但可以证明其中包含一些信息。 例如,如果我们有十个数字,则可以为这些数字创建证明,然后证明此集合中有某个特定数字。 该证明将具有较小的恒定大小,这使其在以太坊中发布的成本不高。

You can use this principle for a set of transactions, and prove that a particular transaction is in this set. This is precisely what an operator does. Each block consists of a transaction set that turns into a Merkle Tree. The root of this tree is a proof that is published in Ethereum along with each Plasma block.

您可以将此原理用于一组事务,并证明该组中有特定事务。 这正是操作员要做的。 每个块由一个事务集组成,该事务集变成一个Merkle树。 这棵树的根是与每个等离子块一起发布在以太坊中的证明。

Users should be able to withdraw their funds from the Plasma chain. For this, they send an “exit” transaction to Ethereum.

用户应该能够从等离子链中提取资金。 为此,他们向以太坊发送“退出”交易。

Plasma Cash uses a special Merkle Tree that eliminates the need to validate a whole block. It is enough to validate only those branches that correspond to the user's token.

Plasma Cash使用特殊的Merkle树,消除了验证整个区块的需要。 仅验证与用户令牌相对应的那些分支就足够了。

To transfer a token, it is necessary to analyze its history and scan only those tokens that a certain user needs. When transferring a token, the user simply sends the entire history to another user, who can then authenticate the entire history and, most importantly, do it very quickly.

要传输令牌,有必要分析其历史记录并仅扫描特定用户需要的那些令牌。 转移令牌时,用户只需将整个历史记录发送给另一个用户,另一个用户就可以对整个历史记录进行身份验证,最重要的是,可以非常快速地进行身份验证。

用于状态和历史记录存储的等离子现金数据结构 (Plasma Cash data structures for state and history storage)

It is advisable to use only selected Merkle Trees, because it is necessary to obtain inclusion and non-inclusion proofs for a transaction in a block. For example:

建议仅使用选定的Merkle树,因为有必要为块中的事务获取包含和不包含证明。 例如:

  • Sparse Merkle Tree

    稀疏默克尔树
  • Patricia Tree

    帕特里夏树

We have developed our own Sparse Merkle Tree and Patricia Tree implementations for our client.

我们为客户开发了自己的稀疏Merkle树和Patricia树实现。

A Sparse Merkle Tree is similar to a standard Merkle Tree, except that its data is indexed, and each data point is placed on a leaf that corresponds to this data point's index.

稀疏Merkle树类似于标准Merkle树,不同之处在于其数据已建立索引,并且每个数据点都位于与该数据点的索引相对应的叶上。

Suppose we have a four-leaf Merkle Tree. Let's fill this tree with letters A and D, for demonstration. The letter A is the first alphabet letter, so we will place it on the first leaf. Similarly, we will place D on the fourth leaf.

假设我们有一棵四叶默克尔树。 让我们用字母A和D填充这棵树,以进行演示。 字母A是第一个字母,因此我们将其放置在第一片叶子上。 同样,我们将D放置在第四片叶子上。

So what happens on the second and third leaves? They should be left empty. More precisely, a special value (for example, zero) is used instead of a letter.

那么第二和第三片叶子会发生什么呢? 他们应该留空。 更确切地说,使用特殊值(例如零)代替字母。

The tree eventually looks like this:

树最终看起来像这样:

The inclusion proof works in the same way as in a regular Merkle Tree. What happens if we want to prove that C is not a part of this Merkle Tree? Elementary! We know that if C is a part of a tree, it would be on the third leaf. If C is not a part of the tree, then the third leaf should be zero.

包含证明的工作方式与常规Merkle树中的工作方式相同。 如果我们想证明C不是此默克尔树的一部分,会发生什么? 初级! 我们知道,如果C是树的一部分,它将在第三片叶子上。 如果C不是树的一部分,则第三片叶子应为零。

All that is needed is a standard Merkle inclusion proof showing that the third leaf is zero.

所有需要的是标准的Merkle包含证明,该证明表明第三片叶子为零。

The best feature of a Sparse Merkle Tree is that it provides repositories for key-values inside the Merkle Tree!

稀疏Merkle树的最大特点是它为Merkle树内的键值提供了存储库!

A part of the PoE protocol code constructs a Sparse Merkle Tree:

PoE协议代码的一部分构造了一个稀疏Merkle树:

class SparseTree { //...        buildTree() {    if (Object.keys(this.leaves).length > 0) {      this.levels = []      this.levels.unshift(this.leaves)      for (let level = 0; level < this.depth; level++) {        let currentLevel = this.levels[0]        let nextLevel = {}         Object.keys(currentLevel).forEach((leafKey) => {          let leafHash = currentLevel[leafKey]          let isEvenLeaf = this.isEvenLeaf(leafKey)          let parentLeafKey = leafKey.slice(0, -1)          let neighborLeafKey = parentLeafKey + (isEvenLeaf ? '1' : '0')           let neighborLeafHash = currentLevel[neighborLeafKey]          if (!neighborLeafHash) {            neighborLeafHash = this.defaultHashes[level]          }           if (!nextLevel[parentLeafKey]) {            let parentLeafHash = isEvenLeaf ?              ethUtil.sha3(Buffer.concat([leafHash, neighborLeafHash])) :              ethUtil.sha3(Buffer.concat([neighborLeafHash, leafHash]))            if (level == this.depth - 1) {              nextLevel['merkleRoot'] = parentLeafHash            } else {              nextLevel[parentLeafKey] = parentLeafHash            }          }        })         this.levels.unshift(nextLevel)      }    }  }}

This code is quite trivial. We have a key-value repository with an inclusion / non-inclusion proof.

这段代码很简单。 我们有一个包含/不包含证明的键值存储库。

In each iteration, a specific level of a final tree is filled, starting with the last one. Depending on whether the key of the current leaf is even or odd, we take two adjacent leaves and count the hash of the current level. If we reach the end, we would write down a single merkleRoot — a common hash.

在每次迭代中,将从最后一棵树开始,填充特定级别的最终树。 根据当前叶子的键是偶数还是奇数,我们取两个相邻的叶子并计算当前级别的哈希值。 如果到达末尾,我们将写下一个merkleRoot-一个公共哈希。

You have to understand that this tree is filled with initially empty values. If we stored a huge amount of token IDs, we would have a huge tree size, and it would be long!

您必须了解,此树最初填充了空值。 如果我们存储了大量的令牌ID,则树的大小将很大,而且很长!

There are many remedies for this non-optimization, but we have decided to change this tree to a Patricia Tree.

对于这种非优化,有许多补救措施,但是我们决定将其更改为Patricia树。

A Patricia Tree is a combination of Radix Tree and Merkle Tree.

帕特里夏树是基数树和默克尔树的组合。

A Radix Tree data key stores the path to the data itself, which allows us to create an optimized data structure for memory.

基数树数据键存储数据本身的路径,这使我们能够为内存创建优化的数据结构。

Here is an implementation developed for our client:

这是为我们的客户开发的实现:

buildNode(childNodes, key = '', level = 0) {    let node = {key}    this.iterations++     if (childNodes.length == 1) {      let nodeKey = level == 0 ?        childNodes[0].key :        childNodes[0].key.slice(level - 1)      node.key = nodeKey       let nodeHashes = Buffer.concat([Buffer.from(ethUtil.sha3(nodeKey)),        childNodes[0].hash])      node.hash = ethUtil.sha3(nodeHashes)      return node    }     let leftChilds = []    let rightChilds = []     childNodes.forEach((node) => {      if (node.key[level] == '1') {        rightChilds.push(node)      } else {        leftChilds.push(node)      }    })     if (leftChilds.length && rightChilds.length) {      node.leftChild = this.buildNode(leftChilds, '0', level + 1)      node.rightChild = this.buildNode(rightChilds, '1', level + 1)      let nodeHashes = Buffer.concat([Buffer.from(ethUtil.sha3(node.key)),        node.leftChild.hash,        node.rightChild.hash])      node.hash = ethUtil.sha3(nodeHashes)    } else if (leftChilds.length && !rightChilds.length) {      node = this.buildNode(leftChilds, key + '0', level + 1)    } else if (!leftChilds.length && rightChilds.length) {      node = this.buildNode(rightChilds, key + '1', level + 1)    } else if (!leftChilds.length && !rightChilds.length) {      throw new Error('invalid tree')    }     return node  }

We moved recursively and built the separate left and right subtrees. A key was built as a path in this tree.

我们以递归方式移动并构建了单独的左右子树。 在此树中构建了一个密钥作为路径。

This solution is even more trivial. It is well-optimized and works faster. In fact, a Patricia Tree may be optimized even more by introducing new node types — extension node, branch node, and so on, as done in the Ethereum protocol. But the current implementation satisfies all our requirements — we have a fast and memory-optimized data structure.

此解决方案更为简单。 它经过优化,运行速度更快。 实际上,可以通过引入新的节点类型(扩展节点,分支节点等)来进一步优化Patricia树,就像在以太坊协议中所做的那样。 但是当前的实现可以满足我们的所有要求-我们拥有快速且内存优化的数据结构。

By implementing these data structures in our client’s project, we have made Plasma Cash scaling possible. This allows us to check a token’s history and inclusion / non-inclusion of the token in a tree, greatly accelerating the validation of blocks and the Plasma Child Chain itself.

通过在客户项目中实现这些数据结构,我们使等离子现金扩展成为可能。 这使我们能够检查令牌的历史记录以及是否将令牌包含在树中,从而大大加快了对区块和等离子儿童链本身的验证。

链接: (Links:)

翻译自:

转载地址:http://izdwd.baihongyu.com/

你可能感兴趣的文章
2015年创业中遇到的技术问题:21-30
查看>>
《社交红利》读书总结--如何从微信微博QQ空间等社交网络带走海量用户、流量与收入...
查看>>
JDK工具(一)–Java编译器javac
查看>>
深入.NET框架与面向对象的回顾
查看>>
merge http://www.cplusplus.com/reference/algorithm/merge/
查看>>
Python-DB接口规范
查看>>
改变label中的某字体颜色
查看>>
列表、元组、字典、集合的定义与操作
查看>>
代理模式
查看>>
地址栏参数获取
查看>>
[VS2013]常见异常修正
查看>>
[转]SQL SERVER 的排序规则
查看>>
SQLServer锁原理和锁的类型
查看>>
Eclipse中SVN的安装步骤(两种)和使用方法[转载]
查看>>
C语言函数的可变参数列表
查看>>
七牛云存储之应用视频上传系统开心得
查看>>
struts2日期类型转换
查看>>
Spark2-数据探索
查看>>
Spark连续特征转化成离散特征
查看>>
C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用...
查看>>