Talk about nervos’s off chain determinism

Time:2022-1-12

Talk about nervos's off chain determinism
Nervos is a hierarchical multi asset value storage public chain, which is a set of original design completely different from other public chains. The bottom layer of layer 1 focuses on security and system robustness, leaving the use experience, performance, etc. to layer 2. The clear division of labor has also created many different experiences and trade-offs in development. In this article, the core developer JJY of nervos CKB takes you to see how the important feature of nervos bottom layer – off chain certainty can make the whole system more secure and reduce resource consumption, and what trade-offs are there.

How to get the current block height

I have repeatedly heard developers complain about why they can’t read the current block height in their scripts (smart contracts).

As every developer knows, reading the current block height from the smart contract is a basic function on Ethereum and other blockchains using EVM. If CKB is the next generation blockchain as we said, why can’t it do what the previous generation public chain can do?

Well, I guess no one can answer this question. The core team of CKB doesn’t often explain the unique design of CKB. I think the core team should do better.

So I decided to write this article to explain the trade-off of this design: why can’t we read the current block height in the smart contract (script).

The right way

Let’s start by verifying the correct way for the CKB script to read the block height!

My observation is that most developers calculate the block time between two actions by requesting the block height. In abstract terms, a user deposits at block height X and withdraws at block height y. After taking out this action, the smart contract needs to calculate the number of blocks from block x to block y, and send rewards according to the user’s deposit time (Note: for example, liquidity mining is the case) or do other similar things.

Ethereum can directly read the block height and calculate rewards. However, in CKB, we must use other methods instead of directly reading the block height:

  1. We can use the function of transaction valid since to restrict the withdrawal transaction, so as to ensure that the withdrawal transaction will not occur until at least Z blocks after the deposit is completed. However, in this way, the user can extend the withdrawal time beyond block Z, which may not be appropriate in some cases.

Talk about nervos's off chain determinism

  1. We can take two steps to withdraw money to locate the height of the block. The first step is to read the block height X of the user’s deposit by CKB syscalls, and record X in the new “ready to withdraw cell”; The second step is that we can use syscall to read the cell y ready for withdrawal and read x from the data field of the cell.

Therefore, we can also calculate that the block time is y-x.
Consider the design of the nervos Dao

Under chain certainty

So what is the purpose of this? Developers obviously prefer simpler and more intuitive methods, so why does CKB use such a difficult method to complete simple work?!

It’s all becauseUnder chain certainty。 Let’s first explain two questions: what is the input of Ethereum contract? What is the input to the CKB script?

If we treat the contract (or script) as a function “F”, we can express the calculation of smart contract in the following form:

On Ethereum:

output = f(tx, state tree, current blockchain)

On CKB:

output = f(tx, deterministic blockchain)

In Ethereum, a contract can retrieve information from three inputs: TX, account status tree and current blockchain status.

For example, a smart contract can read the current block height and block hash from the blockchain.

In CKB, we only allow scripts to read deterministic input. If the user wants to read information from the blockchain, the user must first include the block hash in the transaction. Therefore, all the information that the script can read is determined. We call it under chain certainty, which is a basic feature of CKB.

advantage

Certainty under the chain brings some benefits. The most important point is that once we verify a transaction, we know it will be valid or invalid, because the input and output are determined, and the verification result does not depend on the blockchain state.

This certainty introduces a verification strategy in CKB, that is, we only verify once before pushing the transaction into the memory pool. When we package the transaction into a block, we only need to check whether the transaction input is still unused, which is very cheap compared with the complete verification of the transaction.

Under this chain of transactions, certainty is not only bound to one node; We can guarantee that a transaction is valid even if we send it over a P2P network. Because if a transaction is valid, it will always be valid, so CKB only broadcasts valid transactions to other nodes. If a malicious node attempts to broadcast an invalid transaction, once the transaction sent by the malicious node cannot be verified, other nodes in the network will immediately prohibit it. Since invalid transactions cannot be broadcast to the CKB network, CKB only packages valid transactions into blocks, which I think is a major advantage of CKB.

Note that this is not feasible in Ethereum, because Ethereum’s transaction is not deterministic under the chain. A transaction may fail at block height 42, even if it is valid at block height 41. Therefore, we never know whether the failed transaction was deliberately sent by a malicious node or failed due to the change of the current state of the blockchain.

Therefore, Ethereum chose another way, which is to allow nodes to broadcast invalid messages and package them into blocks. The protocol then allows miners to punish failed transactions by charging the highest fee to the sender’s account. The mechanism aims to encourage users to send only effective SMS to enhance security; But even if you are an honest user, you may send a failed transaction due to accidentally running to an invalid contract state. (when I tried to deposit money into curve, I lost about $50 in a failed transaction.)

Whether only valid transactions are included in the block or failed transactions are included in the block and the sender is punished. These two different design ideas come from different answers to a simple question: should we allow scripts (smart contracts) to read the current block?

Layer 1 robustness vs user experience

According to my understanding, between the robustness (robustness) of layer 1 and the user experience, CKB’s design obviously chooses the former.

I believe this is the right choice; The only important thing of layer 1 blockchain is to provide robust and secure services. This trade-off does not mean that CKB does not care about the user experience. Remember the slogan of CKB – build layer 1 for layer 2. In this case, layer 1 is for robustness and layer 2 (above) is for user experience.

In hierarchical architecture, most people will use high-level technology, and only a few people need to access it directly from the bottom. For example, in the Internet, most users use HTTP or websocket; Few people use TCP or UDP, and few people use IP directly.
Talk about nervos's off chain determinism
Translation note: the picture is added for the translator, so that those who do not understand the layered architecture of the Internet can refer to the layered diagram of the Internet to understand what the author said.

Source:khan Academy
https://www.khanacademy.org/c…:the-internet/xcae6f4a7ff015e7d:the-internet-protocol-suite/a/the-internet-protocols

From the perspective of smart contract developers, you may find this design difficult to understand, but if you look at the layered architecture, you will find that nervos’s design is very suitable for layer 1 blockchain.

Once nervos’s layer 2 facility is started, developers will be able to easily access the functions provided by layer 2. They can not only read the block height with layer 2 (it’s very simple), but also use the more powerful functions of other layer 2 (here is a space for developers to imagine).

*Original link:
https://talk.nervos.org/t/off…
Original author: CKB core developer JJY
Translator: Williams*

Talk about nervos's off chain determinism

//If you like nervos and like development
//You can pay attention to me and trust me privately~
if (you like Nervos && you like dev) {
    println("you can follow me and private letter for me~");
}