by Nervos CKB Team
At the end of 2017, we felt that some of our ideas, including a layered network and a blockchain as a common knowledge base, had matured. Therefore, as soon as the new year’s day of 2018 passed, we couldn’t wait to take action, gathered in a small office, shared CKB ideas for the first time, sorted out the development plan for the first time, and wrote the first line of code on January 23.
More than 300 days and nights have passed since then. After many iterations and sprints, the version of CKB has finally come to 0.1.0. The design framework has basically taken shape. Components including consensus, cell model, virtual machine and network protocol have been implemented, and the preparation of open source has been completed. We can finally say hello CKB ~
CKB open source warehouse
Nervos network is a distributed application network with layered architecture. CKB is a public chain protocol with very different design. It is also the basic layer in nervos network. It is the foundation of the whole networkTrust engine of encrypted economic network。 From the perspective of hierarchical architecture, CKB aims to become the asset storage layer and the arbitration layer of upper layer protocols.
CKB’s open source warehouse is as follows:
- https://github.com/nervosnetw…-CKB main warehouse
- https://github.com/nervosnetw…-CKB virtual machine warehouse. CKB virtual machine is specially designed for blockchain and uses risc-v instruction set（https://riscv.org/), the virtual machine used to execute the smart contract on the CKB.
The programming model of CKB consists of cell model and ckb-vm. DAPP running on CKB hasCalculation and verification are two parts, the calculation runs outside the chain, and the verification is performed by ckb-vm on the chain. As this is a new programming model, we provide contract code and SDK examples through the following three warehouses:
Please note that CKB is still in rapid development iteration. Here is just a simple example reference. CKB’s API, programming model and other designs may change in the future.
As an open network, nervos defines specifications and improves protocols through the RFC (request for comments) process. We welcome the community to promote the development of nervos network with us through the RFC process. Nervos RFCs warehouse is located at:
- Use rust as the main development language
- A compact and complete blockchain underlying system is realized through different functional modules
- Extend the utxo model based on the cell model and support the storage of general state
- The smart contract is implemented based on cell model and ckb-vm. The contract calculation and verification are separated. The calculation takes place under the chain and the verification is on the chain
- Support code logic and state separation of smart contract
- Ckb-vm uses risc-v instruction set and supports writing smart contracts in high-level languages such as ruby and python
- Using flatbuffer to realize the serialization of data structure, directly access the serialized data without parsing, and efficient memory utilization based on zero copy
- The module communication mechanism is implemented based on message and channel in the system. In modules with high performance requirements, such as storage and index access, the shared memory locking scheme is used
- The implementation of secure P2P network has better network security and better resistance to dos and solar eclipse attacks
- More efficient inter node discovery, synchronization protocol
Schematic diagram of CKB P2P network
CKB main module
The main function is stored and is the compilation entry module of the whole project.
It is used to save the definition of the core data structure of CKB, including block, cell, transaction and other core data structures.
The consensus configuration of the chain, which will be written to the genesis block. Nodes with different configurations cannot communicate directly.
It is used to save the logic and code common to each module.
Encapsulates the underlying data persistence layer. The underlying storage of CKB uses kV data storage. There are two corresponding implementations. One is based on rocksdb, which uses rocksdb to persist data to disk. Another implementation is memory based simulation persistence, which is mainly used in testing, development and other scenarios.
The blockchain data structure is implemented. Use the DB module for persistence. Chain is mainly accused of recording and updating the chain with the highest local cumulative workload, and maintaining the index of data on the chain. When updating the chain, you need to verify and update the index at the same time.
The main function of the pool module is to realize the transaction pool. The characteristic of the CKB pool is to dynamically adjust the block time according to the network conditions, which will make more rational use of network resources and bandwidth. The biggest challenge in the design and implementation of trading pool is to balance multiple objectives at the same time. This includes considering the sorting of transactions, dependencies, and overall performance, especially reducing the data that needs to be synchronized between nodes and using cache reasonably.
It is used to store the structure definition of messages between nodes and the builder of message body. Messages are serialized using flatbuffers.
The underlying implementation code of peer-to-peer communication encapsulates trust libp2p, and exposes some basic protocols through services. Through the abstraction of libp2p, it is more convenient to realize the customizable libp2p in the future.
The network synchronization protocol of CKB is implemented, including two protocols, Synchronizer protocol and relay protocol. The working mode of Synchronizer protocol is head first, which makes more efficient use of network bandwidth to improve block download efficiency and is used for high-speed downloading of block data between nodes. Relay protocol is used between nodes to process broadcast and forward new transactions. Based on bitcoin’s head first synchronization, compact block and other protocols, the sync module combines the functions of transaction submission area, uncle fast statistics and so on.
Ckb-vm is a stand-alone implementation. From the implementation point of view, ckb-vm is the implementation of risc-v hardware CPU instruction set, and all implementations fully comply with risc-v standards. Therefore, ckb-vm can be regarded as a general sandbox runtime module. The role of ckb-vm in the project is to verify the status and execute smart contracts, so as to maintain the maximum flexibility of the computing layer of the whole system. For example, by updating the system contracts stored in the cell, new encryption algorithms or other functions can be added, Moreover, the isolation design of the sandbox of ckb-vm provides a strong operation security guarantee for the execution of the contract.
The replacement of different consensus algorithms is realized through pluggable. At present, in order to facilitate development, two sets of built-in consensus algorithms of CPU and cuckoo are realized, and it is convenient to add externally implemented consensus algorithms, such as progpow algorithm.
It is a set of pub / sub modules used for message communication between internal modules.
It contains scripts implemented in ruby to facilitate sending test data to the blockchain during development.
Code reading suggestions
Reading the code requires some basic knowledge, such as familiarity with rust language and ecology, as well as some basic data structures and algorithms, and a certain understanding of the basic concepts of blockchain, such as consensus protocol, smart contract and so on.
It is suggested that you can read the code through three paths
- The logic of data synchronization is related to the network. It will involve the network protocol and the implementation of the protocol. It mainly depends on the three modules of protocol, network and sync.
- For data storage, you can understand the design of data structure and the implementation of data persistence, mainly focusing on core, DB, chain, pool and other modules.
- Consensus protocol requires a certain conceptual understanding of consensus algorithm, mainly including spec, pow, sync, chain and other modules.
Nervos CKB is still in a very active development stage. The code has not been verified by the production environment, and the functions and modules may be greatly adjusted in the future. Therefore, please refer to the latest source code on GitHub for project status and description.