How CKB Works
Whether you’re new to blockchain or have a technical background, this guide will help you understand how CKB works. We’ll explore the fundamental components—Cell Model, Scripts, Transaction, and CKB-VM—in a way that’s easy to grasp.
Intro to CKB
Blockchain is a decentralized, immutable ledger that records transactions across a network, enabling interactions without intermediaries. CKB, or Common Knowledge Base, is the foundational layer of the Nervos Network. It operates on Proof of Work (POW) consensus, where miners solve cryptographic puzzles to secure the network and validate transactions, ensuring strong security and decentralization.
How Transaction Works
The CKB blockchain works like a storage facility filled with boxes, called Cells. These Cells can hold CKBytes, the native tokens of CKB, which represents both value and storage capacity—the more CKBytes you own, the more data you can store on the blockchain.
Each Cell requires at least 61 CKBytes to cover the essential data it needs to function. In practice, it's recommended to allocate 62 CKBytes or more to cover transaction fees and ensure smooth processing.
The Cell Model defines how these Cells behave, governed by Scripts that set the rules for how Cells can be used or accessed. Once added to the blockchain, Cells are immutable, meaning they can’t be changed. To update a Cell’s data, you must consume it—extract the data, modify it, and create a new Cell with the updated information.
Cells that have not been consumed are known as Live Cells, and these are available for use in future transactions. For example, if you want to send 100 CKByes to Alice from a Live Cell containing 200 CKBytes:
-
Create: You start by selecting a Live Cell with a capacity of 200 CKBytes. Two new Live Cells will be created as output: one Cell with 100 CKBytes, locked so only Alice can unlock it; another Cell with 99.999 CKBytes locked to yourself as your change. The 0.001 CKBytes difference will be used as the transaction fee.
-
Sign: You sign the transaction with your private key, proving that you have the authority to spend the CKBytes in this Cell.
-
Broadcast: After signing, the transaction is broadcasted from your wallet to the nearest CKB network node.
-
Validate: The receiving node performs several checks to validate your transaction. For more details, you can check out the verification process
-
Propagate: The validated transaction is propagated to other nodes in the network and enters the mempool–a temporary storage area for unconfirmed transactions, waiting for miners to pick up.
-
Confirm: Miners will select your transaction from the mempool and include it in a new block. Once the block containing your transaction is mined, it’s added to the blockchain. At this point, your transaction has 1 confirmation.
As a result:
- Your original 200 CKBytes Live Cell is consumed and marked as a Dead Cell.
- The two new Live Cells (100 CKBytes for Alice and 99.999 CKBytes for you) are now active on the blockchain.
- With each subsequent block mined, your transaction gains additional confirmations, solidifying its permanence and security in the blockchain.
Scripts
What is a Script?
A Script in CKB is a binary executable that can be executed on-chain. It is a program that runs on a virtual machine powered by the RISC-V instruction set, called the CKB-VM, and can perform arbitrary logic to guard and protect your Cells. You can think of it as smart contract.
code_hash
: identifies the Script code to be loaded into the CKB-VMhash_type
: indicates the the method CKB-VM uses to locate the Script code or Script.args
: provides specific arguments that differentiate one Script from another, such as a users’s public key hash.
There’re two main types of Scripts:
- Lock Script: A required Script controlling the ownership and access to a Cell, ensuring only authorized users can spend/consume the Cell.
- Type Script: An optional Script dictating how a Cell can be used or modified in a transaction.
CKB System Script
One of the most important system Scripts is the default Lock Script known as secp256k1_blake160_sighash_all
. This Script utilizes the secp256k1 elliptic curve and the Blake2b hashing algorithm to ensure that only the owner of the corresponding private key can unlock and spend the CKBytes associated with a given Cell.
For example, if Alice wants to send CKBytes from a Cell that is protected by thesecp256k1_blake160_sighash_all
Lock Script, the following process occurs:
- The transaction is signed using Alice’s private key. This signature proves that Alice has the authority to spend the CKBytes associated with the Cell.
- The public key from the transaction’s signature is hashed using Blake2b to generate a new Blake160 hash. The Lock Script then compares this newly generated Blake160 hash with the one stored in the
script_args
of the Cell. If the two hashes match, the Script verifies the signature using the secp256k1 curve, ensuring that the signature was indeed generated by the private key corresponding to the public key.
CKB-VM
The CKB-VM is the virtual machine that executes Scripts on the CKB. It uses the RISC-V instruction set, which is a modern, open-source architecture. This design provides a low-level access to the CPU, enabling highly efficient execution and flexibility.
Script Execution
The execution of a Script begins when a transaction is submitted to the CKB network. The CKB-VM first invokes syscalls to load the necessary Script code, referenced by code_hash
and hash_type
into memory. Next, the CKB-VM retrieves relevant data from the Cell and Witness in the transaction through syscalls like ckb_load_cell_data()
and ckb_load_witnesses()
. Once all necessary components are in place, the CKB-VM executes the Script, which may involve hashing public keys, verifying signatures and comparing these results against stored values to validate the transaction. The CKB-VM ultimately checks the return value of the Script after execution: if the Script returns 0
, it indicates successful execution, leading to a valid transaction; if it returns a non-zero value, the CKB-VM considers the execution to have failed, resulting in an invalid transaction.