This article is the author of Jan, this paper presents a very interesting design pattern of DApp support in Cell model: First-class Asset, which makes the encryption block in the chain of assets into a “citizen”.
Love functional programming engineer should be familiar with a noun: First-class Function, translated into Chinese should be called the “first function” or “a function”. First-class Function is a kind of programming language in these languages function is a completely independent concept: the function can be regarded as the value assigned to a variable that can be used as parameters to other functions, but also can be used as the return value coming from other functions. In this language we can manipulate data manipulation functions like the same, so the function in these languages and data as “a citizen” (First-class Citizen). First-class Function is one of the key features of functional languages, many functional programming power comes from this.
To construct the common knowledge base Cell model Nervos CKB using state. Cell model is a very simple but very different from the existing block chain design model, we have realized in the design of the Cell model, Cell model based on DApp will have some very different properties, the design model and program characteristics like functional programming and object-oriented programming will produce different styles. In this article, a very interesting design pattern of DApp I think this Cell model can support, we call it First-class Asset, because it can be a custom encryption block in the chain of assets into a “citizen”.
Quick start state model
In the Cell model, various block chain using state model basically is two kinds: UTXO model and Account model.
On behalf of the use of the UTXO model is bitcoin. UTXO is not being spent trading output (Unspent Transaction Output) for a UTXO can be simply understood as a bitcoin, however and the coin is different for each UTXO value, are not the same. Each UTXO through a lock (Lock Script) script record who is the coin of the owners, while ensuring that only the owner can take this coin. Every bitcoin will maintain the current set of all nodes in all UTXO, this set is called the current US state bitcoin books (the current books). Every time a bitcoin transfers are removed from the UTXO collection (a few coins belonging to payment) and then add a few new coins (belonging to the payee and / or payment) process. Because the books state is constructed based on UTXO the smallest unit, we call it the UTXO model.
On behalf of the use of the Account model is the etheric fang. Account’s account, bank account and similar, on behalf of the asset owner account inside the most important data is the balance (Balance), the number of records of the account holder etheric coins. The account is on behalf of the owner of the assets, the owner can be people (corresponding to the external account) or intelligent contract (corresponding contract account), external accounts through the private key signature to verify the ownership of assets, ownership contract account determined by contract contract code, code and state are stored inside contract account. When the external account transfers to the user specified in the transaction, the transfer number of books in payment account and payee account balances will accordingly decrease and increase. Because the entire state is based on the account books (Account) to construct the smallest unit, we call it the Account model.
UTXO model and Account model to represent two kinds of ideas of the construction of state books. The relationship between the owner and the books is a collection of assets. UTXO model of asset based modeling, to construct the “coin” concept, to give the owner of the coin to the owner of property; Account model based modeling, to construct the “accounts” concept, to give the property account balance. In what way as the basic object model based decision system in the operation of the assets or accounts (owner).
So we say that the coin is UTXO (Coin) model in First-class Citizen, each UTXO is an independent object identifier (Transaction ID + Output Index), Coin is the direct object of the operation of the user (user to include UTXO in trading structure), the account is established based on Coin (upper concept only in the wallet). So UTXO is First-class Coin.
In the Account model, the First-class account is Citizen, the polymerization in the account balance coin has no independent identifier. Account is the direct object of the operation of the user, the transfer of assets by the account as a user agent to achieve, this point in the receiving party is the embodiment of contract account is the most obvious. In this model, user defined encryption assets (e.g. ERC 20) is more like the third party billing way, rather than the transfer point to point mode, the difference will be the third party (the third party refers to the intelligent contract escrow encryption assets) into the asset transfer process, increase the design of intelligent the complexity of the contract (we can take the contract as intelligent would automatically execute the logic in asset transfer). In order to reduce the complexity of the Account model in the transaction need to add special logic (Value fields), but such special logic helps only the original assets, at the same time due to the underlying assets and user defined assets of different code paths.
For these problems, Kelvin Fitcher wrote a Looking at ownership in the EVM has a very good analysis, and will not go.
With this background, we should design idea that is easier to understand CKB:
With the Cell model, we can simplify the design and implementation of user defined, as “a citizen” assets in Nervos CKB (User Defined Assets), referred to as First-class Assets.
First-class Assets and UTXO, with independent identifiers can be directly referenced and user script and operation.
How to realize the First-class Assets?
No matter in what way, we all need to record the relationship between the owner and the assets. These records, essentially through consensus state. Have First-class Assets, you must have First-class State, and this is the starting point of the Cell model.
Nervos CKB Common Knowledge Base (the name comes from the common knowledge base) abbreviation. We chose the Nervos network block chain called “common knowledge”, because it is the responsibility of continuing the network common state formed a global consensus, in other words, CKB is a global consensus maintained by the state library. A basic model of the State Library, the very nature of the whole state is divided into smaller units up state. These smaller state units, is Cell.
Because Cell is a state independent unit identifier (Transaction ID + Cell Output Index), can be directly quoted, as the argument to the script, it is CKB in a “citizen”, that is to say the state is CKB in a “citizen”. Cell is not only a kind of First-class State and First-class State, is a kind of the most simple: a Cell only Capacity, Data, Lock and Contract (optional, Contract is a piece of code or refer to a Code Cell Reference) four fields.
As shown below, the owner of Cell Cell can be stored in the state update, without any intermediary, but in the Account model, the user only through contract code (account Code) to operate the account of the state, the state is actually in the hands of the hosting contract.
It is worth noting that, with Cell, CKB actually get a stateful programming model. A common view is that the ability to express the etheric programming model from the virtual machine workshop Turing complete, in fact through the account of the intelligent contract can save the computation of the state is a big advantage of EVM (Turing incomplete language also has very strong expression ability: https:// en.wikipedia.org/wiki/Total_functional_programming).
CKB by Cell and CKB-VM (Simple Yet Powerful! This must also write a article) combination to achieve a new state intelligent contract programming model. This programming model is more suitable for Layer 2, because the common mode through the analysis of Layer 2 we can see that the interaction between protocol layers should be the state of the object (State Transaction) instead of the event object (Event Transaction), Layer 1 should be a state layer instead of calculating layer.
Another feature of the CKB programming model that does not distinguish between data and code (state). The meaning of this sentence is different with the Account model, and the status of contract code can be stored in the Data field in the Cell, the Cell can save the code by other Cell references (because they are First-class State!), and the status of contract code does not need to be bound together, stored in a place. Developers can use a simple instruction to code Cell or Cell data loading content of memory to run, then according to the need to be interpreted as code or data to read and write.
With these underlying support, we can put a contract code and the state stored separately in different places: Code Cell Code (Data) and State field storage code, Cell State (Data) field is stored; through the business logic constraint Contract ref reference Cell to build Code for self preservation State in State Cell, quoted another Code Cell to express State Cell through Lock ref ownership. Each State Cell can belong to different users, so under the Cell model independent user state is very easy to implement mode (under the Account model, the contract state often constitute, by multiple users in a mixed state such as ERC 20 and Bob Alice contract, Token credits are recorded in the same the internal state of the contract inside).
If you want to CKB-VM the contract written more, please see this article two:
An Introduction to Nervos CKB-VM
With such a programming model, we can construct the First-class Asset.
User defined assets in CKB (User Defined Asset) can be configured such that:
The design of assets (Asset Definition), the definition of contract provisions of the main constraints of assets (such as the total number of transactions, issuers, before and after the same number, etc.);
Save the code to the Asset Definition Cell contract;
In order to meet the issue of authority, issuing assets, and assets of the state stored in State Cell. Contract field State Cell reference asset preservation definition of Code Cell, State Cell by warrant changes the definition of asset constraint;
Asset Cell the owner can update the Lock to change the owner of Cell Asset.
As you can see, in this design, the user defined assets is as an independent object in the system, each asset is a Cell, each one has its own identifier assets. We can think that Asset Cell is a general version of UTXO. This First-class Asset has the following advantages:
Asset Cell can be used as a parameter, can be directly into other contracts. As long as the reference Asset Cell Input has the correct user authorization, the contract can be the normal use of the user’s Asset Cell;
The definition of assets and asset separation state. Asset Definition Cell is the owner of the assets of the issuer, while Asset Cell is belong to each user. The separation of authorization logic and business logic Asset Cell, ownership is completely determined by its own Lock, has nothing to do with the Asset Definition logic, which means that First-class Asset is not managed assets the definition of contract in the hands of the issuer, developers or assets, but really belongs to the user;
The user’s assets are separated from each other, the user independent state assets. The CKB model of economic incentive problem: the user attention state is stored in block chain not only need to pay to save state costs, storage costs and should bear proportional to the storage time. If the asset state mixed users in a storage place (e.g. ERC 20), the storage cost of these state who has to pay is a problem. (CKB Economics Paper is working on writing…) ;
As long as the Lock Asset Definition Cell logic allows the definition of assets can be updated independently.
The above schematic only in CKB implementation of a First-class Asset. In addition to the above discussion, there are some interesting details, for example, Asset Definition Cell is not to have their own state? Asset Definition Cell and Asset Cell Capacity should be provided by who? For these problems, we have some very nice idea. The details of the design and implementation is discussed, we are now working.
Cell model is a highly abstract model, in fact, you can not only achieve the First-class Asset in Cell, also can simulate the Account in Cell. Through this article we can see that the Cell model is a new design is different from the UTXO model and Account model. In addition to the state of different model, CKB will also calculate (that is, state generated) transferred to the chain in the chain, only need to verify the logic state. The state model and calculation show unique separation of the two decisions in DApp paradigm and a new design pattern is bound to the CKB programming model on.
From the CKB white paper to now nearly a year’s time, we see more and more people begin to pay attention to and discuss the First-class State and First-class Asset these two kinds of new ideas (although the term we use each are not the same), these advances make us very excited. If you are interested in more about First-class State and First-class Asset, or what interesting ideas in the programming model of CKB, welcome to contact us to discuss.
The CKB code has been completely open source, this article introduced the contents have been implemented in the code. Welcome to our code put forward various views:
Https://github.com/nervosnetwork/ckb-demo-ruby-sdk (CKB with Ruby script programming examples, to understand the CKB programming model on optimal entrance)
Thanks to Ian Yang, Xuejie Xiao, Kevin Wang and Cell CKB in model design to help.