前言
当前,区块链之上的各种应用的雏形已经具备,但如何让区块链技术在更广泛的用户和场景中应用,是所有的公链需要解答的问题。当前,区块链之上的各种应用的雏形已经具备,但如何让区块链技术大规模采用,是所有的公链需要解答的问题。
这个问题进一步拆解,其实有两个问题:这个问题可以拆解为两个问题:
区块链如何接受大规模用户?也就是区块链的扩容难题。
应用应该以什么形态和链结合?也就是链和 Web3 应用的关系。
...
通过改进共识机制,或者减少验证节点等方式来实现扩容。
通过分片或者平行链来实现扩容。
通过分层的方式进行扩容。
这三条路线,各有优劣,Starcoin Starcoin 选择了第三种路线,主要是基于以下理由:
分层是一种人类社会习惯的解决扩容问题的方式,比如司法系统,政治机构,都是通过分层的方式来解决扩容难题,只要实现不同层之间的制约机制即可保证安全。分层是一种人类社会习惯的解决扩容问题的方式,比如司法系统,政治机构,都是通过分层来解决扩容难题,不同层之间的制约机制可以保证安全。
区块链的不可能三角中,Layer1 应该更侧重安全。
不同的应用,应用的不同阶段,对去中心化的要求,对安全的要求,对吞吐的要求都不一样,需要有一种面向应用的演进方案,通过分层的方式更容易实现。
Stargate 是 Starcoin 区块链网络上的分层协议框架。它通过统一的抽象,支持不同的分层方案。Layer1 保证安全(Security)和无准入(Permissionless)。Layer2 实现终局性(Finality),将状态和计算从 Layer1 迁移到 Layer2,实现全局扩容以及交易的即时确认。Layer3 通过面向应用的局部共识,最终实现对 Web3 应用的支撑。
Preface
Currently, the prototypes of various applications on top of blockchain are available, but how to make blockchain technology mass adoption is the question that all public chains need to answer.
This question can be deconstructed into two questions:
How does the blockchain accept massive users? That is the blockchain's scaling problem.
In what way should applications be combined with the chain? That is the relationship between the chain and the Web3 application.
On the issue of scaling, there are three main approaches in the current blockchain world.
Scaling is achieved by improving the consensus mechanism, or by reducing the number of validation nodes, etc.
Scaling through sharding or parallel chains.
Scaling through layering.
Starcoin has chosen the third approach for the following reasons:
Layers are a customary approach to solve the scaling problem in human societies, such as judicial systems and political institutions, all solve the scaling problem by layering, and the constraint mechanism between different layers can ensure security.
In the impossible triangle of blockchain, Layer1 should focus more on security.
The requirements for decentralization, security, and throughput are different for different applications and different stages of applications. It is easier to achieve a gradual application-oriented evolutionary solution through a layered approach.
Stargate is a layered protocol framework on the Starcoin blockchain network. It supports different layering solutions through a universal abstraction.
Layer1 ensures Security and Permissionless.
Layer2 enables Finality, migrating state and computation from Layer1 to Layer2, enabling global scaling and instant confirmation of transactions.
Layer3 supports Web3 applications through application-oriented consensus solutions.
关键方案
分层的所有方案中,主要面临三个技术难题:
如何在 Layer1 校验 Layer2 的执行结果并进行仲裁?Layer3 到 Layer2 同理。
Layer2 以及 Layer3 能否依赖 Layer1 的合约?
合约的状态如何在不同的层之间迁移?
Key Solutions
There are three main technical challenges in all the layering solutions:
How to verify and arbitrate the transaction execution results of Layer2 at Layer1? and Layer3 to Layer2 as well.
Can Layer2 and Layer3 depend on Layer1's smart contracts? This affects the composability of smart contracts across layers.
How does the state of a smart contract move between layers?
交易校验
区块链上执行一个交易,虽然要基于历史状态进行,但实际上不会读取全部的历史状态。如果只提供该交易所依赖的状态,也可以计算出新的状态。
...
Y 要执行 T 的时候,依赖当前状态 σ𝑡,但 Y 并不会读取所有的历史状态,如果把 Y 需要的状态抽出一个子集 σ`𝑡,并同时提供证明,σ`𝑡 属于 σ𝑡,就可以实现状态转换。我们把交易所依赖的前置状态子集叫做读取集(ReadSet),状态的证明叫读取集证明(ReadSetProof),把它们和交易以及执行后的状态树的根哈希一起打包,叫做富状态交易(StateFullTransaction)。
Transaction verification
The execution of a transaction on the blockchain, while to be executed based on the historical state, will not actually read the entire historical state. A new state can also be calculated if only the state that the transaction depends on is provided.
The blockchain state transition can be represented by the following formula (from Ethereum Yellow Paper).
σt+1 ≡ Υ(σt, T)
σ𝑡+1 represents the next world state
Υ represents the state transition function
σ𝑡 represents the current state of the world
T represents a transaction
Y to execute T relies on the current state σ𝑡, but Y does not read all σ𝑡 states. If a subset σ`𝑡 of the states required by Y is extracted and the proof is also provided that σ`𝑡 belongs to σ𝑡, the state transition can also be achieved.
Drawio | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
我们把交易执行所依赖的前置状态子集叫做读取集(ReadSet),状态的证明叫读取集证明(ReadSetProof),把它们和交易以及执行后的状态树的根哈希一起打包,叫做富状态交易(StateFullTransaction)。
We call the subset of states on which the execution of the transaction depends as ReadSet and the proof of the states as ReadSetProof and package them together with the transaction and the root hash of the state tree after execution as StateFullTransaction.
数据结构表达如下:
The data structure is represented as follows:
代码块 | ||
---|---|---|
| ||
pub struct ReadSet{ state:Vec<(AccessPath,Vec<u8>)>, } pub struct ReadSetProof{ //TODO define proof } pub struct StateFullTransaction{ read_set: ReadSet, proof: ReadSetProof, transaction: SignedUserTransaction, //Transaction info is transaction execute result, include new state_root, event_root, etc. transaction_info: TransactionInfo, } |
...
富状态交易(StateFullTransaction)本身包含了交易执行的前置状态,是可以自校验的,校验方法的伪代码表达如下:包含了交易执行依赖的状态,是可以自校验的,校验方法的伪代码表达如下:
The StateFullTransaction contains the state of the transaction execution dependent and is self-verifiable. The pseudo-code expression of the verification method is as follows.
代码块 | ||
---|---|---|
| ||
StateLessVM{ fn eval(txn: StateFullTransaction, prev_state_root: HashValue): bool{ //通过 ReadSet 构造状态树 //Constructing a state tree via ReadSet let state_tree = build_state_tree(txn.read_set); //验证 state_tree 的 root 和 prev_state_root 是否一致一致 assert(state_tree .root == prev_state_root); //在状态树的基础上执行交易 let output = execute_txn(&state_tree, txn.transaction); //将执行结果中的 WriteSet 写入 state_tree state_tree.apply(output.write_set); //验证执行后的结果和 StateFullTransaction 的 transaction info 匹配。匹配 assert(state_tree.root == txn.transaction_info.state_root); //验证 transaction_info 中的其他字段 } } |
校验富状态交易不依赖于外部状态,所以校验所需的 校验富状态交易不依赖于外部状态,所以校验交易的 VM 是 StateLess 的,交易执行所包含的合约代码以及其依赖也一并包含在 ReadSet 中。的,交易执行依赖的 Layer2 的智能合约代码也一并包含在 ReadSet 中。
Verifying StateFullTransaction does not depend on external state, so the VM for verifying transactions is StateLess, and the Layer2 smart contract code on which the transaction execution depends is also included in the ReadSet.
智能合约的依赖
区块链分层后,对智能合约之间的依赖关系带来了难题,Layer2 的合约能否依赖 Layer 1 的合约?这里的依赖包含两层意义,一代码的依赖,二状态的依赖。在 Ethereum 上,合约之间不能在编译期依赖,合约之间的依赖都是通过合约内的动态调用的方式实现的,一和二是同一个问题。这个问题在Ethereum 当前的所有 Layer2 方案中,都未得到很好的解决。
...