### Setting Up CKB Ruby SDK (Shell) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md This snippet outlines the shell commands to clone the CKB Ruby SDK repository, navigate into its directory, run the setup script to install plugins, and finally enter the Ruby REPL environment for interactive use. ```shell git clone https://github.com/nervosnetwork/ckb-sdk-ruby cd ckb-sdk-ruby ./bin/setup # 安装插件 ./bin/console # 进入 ruby REPL环境 ``` -------------------------------- ### Installing Project Dependencies with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command installs all necessary project dependencies using Yarn. It should be executed once after cloning the repository to set up the development environment. ```bash $ yarn ``` -------------------------------- ### Installing Ruby Environment with rbenv (Shell) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/start/connect-client.md This snippet provides shell commands to clone rbenv and ruby-build repositories, set up a Ruby China mirror (recommended for domestic users), list available Ruby versions, install a specific version (2.5.5), and verify Ruby and Gem versions. It also includes a command to install bundler. ```shell git clone https://github.com/rbenv/rbenv.git ~/.rbenv git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build # 使用 Ruby China 的镜像安装 Ruby, 国内用户推荐 git clone git://github.com/AndorChen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror rbenv install --list # 列出所有 ruby 版本 rbenv install 2.5.5 # 安装 2.5.5 ruby -v # 查看ruby版本 gem -v # 查看 gem版本 gem i bundler -V # 安装 bundler ``` -------------------------------- ### Example Start Withdrawal Epoch Object (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant represents an example parsed epoch object, similar to `depositEpoch`, but specifically for the epoch when a Nervos DAO withdrawal was initiated. ```JavaScript const startWithdrawingEpoch = { "length": "0xa", "index": "0x0", "number": "0xbe" } ``` -------------------------------- ### Starting Local Development Server with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command initiates a local development server and automatically opens a browser window. Most changes made to the code are reflected live without requiring a server restart, facilitating rapid development. ```bash $ yarn start ``` -------------------------------- ### Example Nervos DAO Function Calls (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This snippet shows commented-out example calls to the various Nervos DAO interaction functions defined in the file, demonstrating how they would be invoked sequentially for a full deposit and withdrawal cycle. ```JavaScript // deposit() // logDepositEpoch() // starWithdrawing() // logStartWithdrawingEpoch() // logCurrentEpoch() // withdraw() // setInterval(logCurrentEpoch, 1000) ``` -------------------------------- ### Setting Up CKB-VM Bench Scripts for Native C Benchmarking Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This snippet provides commands to clone and build the `ckb-vm-bench-scripts` repository, specifically checking out a particular commit. This setup is necessary to compile and run the native C version of the secp256k1 benchmark within a Docker environment, allowing for performance comparison with WASM-based implementations. ```shell cd $TOP git clone --recursive https://github.com/nervosnetwork/ckb-vm-bench-scripts cd ckb-vm-bench-scripts git checkout f7ab37c055b1a59bbc4f931c732331642c728c1d cd $TOP sudo docker run --rm -it -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20191209 bash cd /code/ckb-vm-bench-scripts make (omitted ...) exit exit ``` -------------------------------- ### Example Start Withdrawal OutPoint (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant provides an example `OutPoint` object for a transaction that initiates a Nervos DAO withdrawal. It is used to identify the specific cell output that represents the start of the withdrawal period. ```JavaScript const startWithDrawOutPoint = { "txHash": "0xc8ad01deb8b25c56169992598398ad7d539314ada90c84bff12fa7fc69095076", "index": "0x0" } ``` -------------------------------- ### Initializing CKB API and Wallet - Ruby Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/script/script-issue-udt.md This snippet initializes a connection to the CKB node, generates a new random private key, and constructs a CKB wallet from this key. It demonstrates the initial setup required to interact with the CKB blockchain using the Ruby SDK. ```Ruby pry(main)> api = CKB::API.new => # pry(main)> new_key = CKB::Key.random_private_key => "0xc5e72c391909d695fb02a9ee40e55f0ffcd5188591373f6fe0013ad4b89a5342" pry(main)> wallet = CKB::Wallet.from_hex(api, new_key) => #>, @address="ckt1qyq2t9nf8m54kwgp5kdg87nqkyj82ywj6nwsl67j63", @api=#, @blake160="0xa596693ee95b3901a59a83fa60b1247511d2d4dd", @hash_type="type", @key=#, @pubkey="0x02ce875d7dd28539e5d6d74a1ba2743082be36e5465df1181292436ca0dac1dae6", @skip_data_and_type=true> ``` -------------------------------- ### Querying Transaction Pool Information (Bash) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This `bash` example demonstrates how to retrieve current information about the CKB node's transaction pool using the `tx_pool_info` RPC method. It sends a JSON-RPC request with an empty `params` array to get the pool's status. ```bash echo '{ "id": 2, "jsonrpc": "2.0", "method": "tx_pool_info", "params": [] }' \ | tr -d '\n' \ | curl -H 'content-type: application/json' -d @- \ http://localhost:8114 ``` -------------------------------- ### CKB Transaction Structure Example Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-19-ckbscript-09.md An example of a CKB transaction (tx.json) structure, detailing `cell_deps`, `header_deps`, `inputs`, `outputs`, `witnesses`, and `outputs_data`. This JSON is used as input for the CKB debugger. ```JSON { "cell_deps": [ { "out_point": { "tx_hash": "0xfcd1b3ddcca92b1e49783769e9bf606112b3f8cf36b96cac05bf44edcf5377e6", "index": "0x0" }, "dep_type": "code" }, { "out_point": { "tx_hash": "0xfcd1b3ddcca92b1e49783769e9bf606112b3f8cf36b96cac05bf44edcf5377e6", "index": "0x1" }, "dep_type": "code" } ], "header_deps": [ "0x51d199c4060f703344eab3c9b8794e6c60195ae9093986c35dba7c3486224409" ], "inputs": [ { "previous_output": { "tx_hash": "0xa98c57135830e1b91345948df6c4b8870828199a786b26f09f7dec4bc27a73da", "index": "0x0" }, "since": "0x0" } ], "outputs": [ { "capacity": "0x0", "lock": { "args": "0x", "code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "hash_type": "data" }, "type": null } ], "witnesses": [ "0x210000000c0000001d0000000d0000006920616d20612073656372657400000000" ], "outputs_data": [ "0x" ] } ``` -------------------------------- ### Example Multisig Script for CKB Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0021-ckb-address-format/0021-ckb-address-format.md This example demonstrates a specific multisig script configuration. It sets S=0 (format version), R=1 (Cipher must approve, as their key is first), M=2 (any two signatures required), and N=3 (total participants). The script then lists the Blake160 hashes of Cipher's, Alice's, and Bob's public keys in order. ```plaintext 0 | 1 | 2 | 3 | Pk_Cipher_h | Pk_Alice_h | Pk_Bob_h ``` -------------------------------- ### Example Response for get_block_by_number RPC (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON snippet provides an example of the response returned by the `get_block_by_number` RPC method. It contains comprehensive block information including `header` details, `proposals`, `transactions`, and `uncles`. ```json { "id": 2, "jsonrpc": "2.0", "result": { "header": { "compact_target": "0x1e083126", "dao": "0xb5a3e047474401001bc476b9ee573000c0c387962a38000000febffacf030000", "epoch": "0x7080018000001", "hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40", "nonce": "0x0", "number": "0x400", "parent_hash": "0xae003585fa15309b30b31aed3dcf385e9472c3c3e93746a6c4540629a6a1ed2d", "proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp": "0x5cd2b117", "transactions_root": "0xc47d5b78b3c4c4c853e2a32810818940d0ee403423bea9ec7b8e566d9595206c", "uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", "version": "0x0" }, "proposals": [], "transactions": [ { "cell_deps": [], "hash": "0x365698b50ca0da75dca2c87f9e7b563811d3b5813736b8cc62cc3b106faceb17", "header_deps": [], "inputs": [ { "previous_output": { "index": "0xffffffff", "tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000000" }, "since": "0x400" } ], "outputs": [ { "capacity": "0x18e64b61cf", "lock": { "args": "0x", "code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", "hash_type": "data" }, "type": null } ], "outputs_data": [ "0x" ], "version": "0x0", "witnesses": [ "0x450000000c000000410000003500000010000000300000003100000028e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5000000000000000000" ] } ], "uncles": [] } } ``` -------------------------------- ### Estimating Fee Rate using RPC (Bash) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This snippet demonstrates how to call the `estimate_fee_rate` RPC method using `curl` to get an estimated transaction fee rate. It sends a JSON-RPC request with a specified `fee_rate` parameter (0xa in this example). ```bash echo '{ "id": 2, "jsonrpc": "2.0", "method": "estimate_fee_rate", "params": [ "0xa" ] }' \n| tr -d '\n' \n| curl -H 'content-type: application/json' -d @- \nhttp://localhost:8114 ``` -------------------------------- ### Compiling and Running Eaglesong C Implementation Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0010-eaglesong/0010-eaglesong.md This snippet provides instructions for compiling and running the C reference implementation of the Eaglesong hash function. It uses 'gcc' to compile 'eaglesong.c' and 'hash.c' into an executable named 'hash', then demonstrates how to run it and input a string from stdin to get the hash output. ```Bash $> gcc -o hash eaglesong.c hash.c $> ./hash > Hello, world! > [Ctrl-D] > 64867e2441d162615dc2430b6bcb4d3f4b95e4d0db529fca1eece73c077d72d6 ``` -------------------------------- ### Example Deposit Epoch Object (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant represents an example parsed epoch object, containing the length, index, and number of the epoch. This structure is typically returned by `ckb.utils.parseEpoch`. ```JavaScript const depositEpoch = { "length": "0xa", "index": "0x0", "number": "0x69" } ``` -------------------------------- ### Defining `no_std` Entry Point for CKB Rust Contract (Rust) Source: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md This Rust code snippet provides the essential boilerplate for a `no_std` application, which is required for CKB contracts due to the RISC-V target's limitations. It includes `no_std`, `no_main`, `start` feature, `lang_items`, a `start` function as the entry point, a `panic_handler`, and `eh_personality` for exception handling. ```Rust #![no_std] #![no_main] #![feature(start)] #![feature(lang_items)] #[no_mangle] #[start] pub fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 } #[panic_handler] fn panic_handler(_: &core::panic::PanicInfo) -> ! { loop {} } #[lang = "eh_personality"] extern "C" fn eh_personality() {} ``` -------------------------------- ### Example Deposit OutPoint (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant provides an example `OutPoint` object, which identifies a specific cell output on the CKB blockchain. It is typically returned after a successful transaction and used as an input for subsequent transactions. ```JavaScript const depositOutPoint = { "txHash": "0x40e1d58cf8576d5206d55d242284a28f64cb114d0b9a8292582e7596082e5bda", "index": "0x0" } ``` -------------------------------- ### Generating BIP44 Addresses with ckb-cli Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/client/ckb-cli/ckb-cli-account.md This command generates BIP44 compliant addresses using `ckb-cli`. It allows specifying the length and starting index for both change and receiving addresses, along with a lock argument and output format. ```shell ckb-cli account bip44-addresses --change-length --from-change-index --from-receiving-index --lock-arg --output-format --receiving-length ``` -------------------------------- ### C Implementation of Load Script Hash Syscall Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md The `ckb_load_script_hash` function provides a C interface for the *Load Script Hash* syscall (ID 2062). It calculates the hash of the current running script and copies it to the VM memory space. The `addr`, `len`, and `offset` arguments are used for partial loading, specifying the destination address, data length, and starting offset. ```C int ckb_load_script_hash(void* addr, uint64_t* len, size_t offset) { return syscall(2062, addr, len, offset, 0, 0, 0); } ``` -------------------------------- ### Starting Duktape REPL for CKB Script Debugging in Shell Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-02-21-ckbscript-07.md This script navigates to the 'htlc-runner' directory and executes 'runner.js' with debug logging. Instead of running a transaction, it uses the 'repl0' Duktape binary to launch an interactive Duktape REPL, allowing developers to test and debug CKB scripts interactively. ```Shell $ cd $TOP/htlc-runner $ RUST_LOG=debug `./runner.js ../ckb-duktape/build/repl0 ../htlc-template/build/duktape.js` ``` -------------------------------- ### Example Final Withdrawal OutPoint (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This constant provides an example `OutPoint` object for a transaction that completes a Nervos DAO withdrawal. It identifies the cell output representing the final state of the withdrawn funds. ```JavaScript const withDrawOutPoint = { "txHash": "0xb1ee185a4e811247b1705a52df487c3ce839bfa2f72e4c7a74b6fc6b0ea4cfa7", "index": "0x0" } ``` -------------------------------- ### Logging Nervos DAO Start Withdrawal Epoch (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This asynchronous function retrieves the transaction details for a `startWithDrawOutPoint` and, if confirmed in a block, fetches the block header to parse and log the epoch information when the withdrawal process was initiated. ```JavaScript const logStartWithdrawingEpoch = async () => { const tx = await ckb.rpc.getTransaction(startWithDrawOutPoint.txHash) if (tx.txStatus.blockHash) { const b = await ckb.rpc.getBlock(tx.txStatus.blockHash) const epoch = b.header.epoch console.log(`const startWithdrawingEpoch = ${JSON.stringify(ckb.utils.parseEpoch(epoch), null, 2)}`) } else { console.log('not committed') } } ``` -------------------------------- ### Generating CKB Addresses (YAML) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0021-ckb-address-format/0021-ckb-address-format.md This YAML snippet demonstrates the process of generating different CKB address types. It includes examples for short addresses (both standard and multi-signature) and full addresses, showing the input arguments required for encoding and the final generated address strings. ```YAML == short address (code_hash_index = 0x00) test == args to encode: b39bbc0b3673c7d36450bc14cfcdad2d559c6c64 address generated: ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v == short address (code_hash_index = 0x01) test == multi sign script: 00 | 01 | 02 | 03 | bd07d9f32bce34d27152a6a0391d324f79aab854 | 094ee28566dff02a012a66505822a2fd67d668fb | 4643c241e59e81b7876527ebff23dfb24cf16482 args to encode: 4fb2be2e5d0c1a3b8694f832350a33c1685d477a address generated: ckb1qyq5lv479ewscx3ms620sv34pgeuz6zagaaqklhtgg == full address test == code_hash to encode: 9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8 with args to encode: b39bbc0b3673c7d36450bc14cfcdad2d559c6c64 full address generated: ckb1qjda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xw3vumhs9nvu786dj9p0q5elx66t24n3kxgj53qks ``` -------------------------------- ### Example Response for get_block_economic_state RPC (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON snippet shows the expected structure and content of the response when calling the `get_block_economic_state` RPC method. It includes details such as `finalized_at`, `issuance` (primary/secondary), `miner_reward` (committed/primary/proposal/secondary), and `txs_fee`. ```json { "id": 2, "jsonrpc": "2.0", "result": { "finalized_at": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40", "issuance": { "primary": "0x18ce922bca", "secondary": "0x7f02ec655" }, "miner_reward": { "committed": "0x0", "primary": "0x18ce922bca", "proposal": "0x0", "secondary": "0x17b93605" }, "txs_fee": "0x0" } } ``` -------------------------------- ### Initiating Withdrawal from Nervos DAO (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This asynchronous function initiates the withdrawal process from the Nervos DAO. It loads necessary dependencies and cells, generates a withdrawal start transaction using a previously obtained `depositOutPoint`, signs it, and sends it to the network. ```JavaScript const starWithdrawing = async () => { await loadCells() await ckb.loadDaoDep() const tx = await ckb.generateDaoWithdrawStartTransaction({ outPoint: depositOutPoint, fee: 10000n }) const signed = ckb.signTransaction(sk)(tx) const txHash = await ckb.rpc.sendTransaction(signed) const outPoint = { txHash, index: '0x0' } console.log(`const startWithDrawOutPoint = ${JSON.stringify(outPoint, null, 2)}`) } ``` -------------------------------- ### Example CKB Transaction Structure (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md This JSON snippet illustrates a complete CKB transaction, showcasing its various fields such as version, cell dependencies, header dependencies, inputs, outputs, output data, and witnesses. It serves as a concrete example of how a transaction is structured on the CKB blockchain. It includes details like `out_point` for dependencies and `lock` scripts for outputs. ```json { "version": "0x0", "cell_deps": [ { "out_point": { "tx_hash": "0xbd864a269201d7052d4eb3f753f49f7c68b8edc386afc8bb6ef3e15a05facca2", "index": "0x0" }, "dep_type": "dep_group" } ], "header_deps": [ "0xaa1124da6a230435298d83a12dd6c13f7d58caf7853f39cea8aad992ef88a422" ], "inputs": [ { "previous_output": { "tx_hash": "0x8389eba3ae414fb6a3019aa47583e9be36d096c55ab2e00ec49bdb012c24844d", "index": "0x1" }, "since": "0x0" } ], "outputs": [ { "capacity": "0x746a528800", "lock": { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x56008385085341a6ed68decfabb3ba1f3eea7b68", "hash_type": "type" }, "type": null }, { "capacity": "0x1561d9307e88", "lock": { "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "args": "0x886d23a7858f12ebf924baaacd774a5e2cf81132", "hash_type": "type" }, "type": null } ], "outputs_data": [ "0x", "0x" ], "witnesses": ["0x55000000100000005500000055000000410000004a975e08ff99fa0001\n 42ff3b86a836b43884b5b46f91b149f7cc5300e8607e633b7a29c94dc01c6616a12f62e74a1\n 415f57fcc5a00e41ac2d7034e90edf4fdf800"] } ``` -------------------------------- ### Serializing `dynvec` with Multiple Items Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md This example illustrates the serialization of a `BytesVec` containing five `Bytes` items of varying lengths. It demonstrates how the total size, multiple offsets, and individual item data are structured in the byte sequence. ```Hex Bytes # the full size is 52 (0x34) bytes 34 00 00 00 # five offsets (20 bytes in total) 18 00 00 00, 1e 00 00 00, 22 00 00 00, 28 00 00 00, 2d 00 00 00 # five items (28 bytes in total) 02 00 00 00, 12 34 00 00 00 00, 02 00 00 00, 05 67 01 00 00 00, 89 03 00 00 00, ab cd ef ``` -------------------------------- ### Redefining CKB Contract Entry Point to `_start` (Rust) Source: https://github.com/nervoscommunity/docs/blob/master/blog/2019-12-24-rust-contract-01.md This Rust snippet redefines the contract's entry point from `#[start]` to a `_start` function. This change is necessary because `riscv64imac-unknown-none-elf` handles entry points differently, and returning from `_start` would result in an `InvalidPermission` error. The function is designed to never return. ```Rust #[no_mangle] pub fn _start() -> ! { loop{} } ``` -------------------------------- ### C Implementation of Load Transaction Hash Syscall Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md The `ckb_load_tx_hash` function provides a C interface for the *Load Transaction Hash* syscall (ID 2061). It calculates the hash of the current transaction and copies it to the VM memory space. The `addr`, `len`, and `offset` arguments are used for partial loading, specifying the destination address, data length, and starting offset. ```C int ckb_load_tx_hash(void* addr, uint64_t* len, size_t offset) { return syscall(2061, addr, len, offset, 0, 0, 0); } ``` -------------------------------- ### CKB `since` Field Examples Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0017-tx-valid-since/0017-tx-valid-since.md This section provides examples of `since` field values for both absolute and relative time locks in CKB. It illustrates how different flag and value combinations correspond to block number, timestamp, and epoch-based lock conditions, demonstrating how transactions fail verification if the specified conditions are not met. ```txt # Absolute time lock 0x0000_0000_0000_3039 # The tx failed verification unless the block number is greater than #12345 0x4000_0000_5e83_d980 # The tx failed verification unless current blockchain date is later than 2020-04-01 0x2000_0000_0000_0400 # The tx failed verification unless the epoch number is greater than 1024 # Relative time lock 0x8000_0000_0000_0064 # The tx failed verification unless it is 100 blocks later since the input cell get confirmed on-chain 0xc000_0000_0012_7500 # The tx failed verification unless it is 14 days(blockchain time) later since the input cell get confirmed on-chain 0xa000_0000_0000_0018 # The tx failed verification unless it is 24 epochs later since the input cell get confirmed on-chain ``` -------------------------------- ### Example Nervos CKB Block Structure in JSON Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0019-data-structures/0019-data-structures.md This JSON snippet illustrates the complete structure of a Nervos CKB block, including its header, uncle blocks, proposals, and a detailed transaction. It showcases the nested nature of CKB data types like `CellInput` and `OutPoint` within a transaction, providing a concrete example of how these elements are represented. ```JSON { "uncles": [ ``` -------------------------------- ### Getting Cellbase Output Capacity Details with `get_cellbase_output_capacity_details` (Bash) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This Bash command demonstrates how to call the `get_cellbase_output_capacity_details` RPC method. It takes a block hash as a parameter and returns a breakdown of the CKB capacity issued in that block's cellbase, including primary, secondary, proposal reward, and transaction fees. ```bash echo '{ "id": 2, "jsonrpc": "2.0", "method": "get_cellbase_output_capacity_details", "params": [ "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40" ] }' \ | tr -d '\n' \ | curl -H 'content-type: application/json' -d @- \ http://localhost:8114 ``` -------------------------------- ### Serializing multiple Uint32s in a fixed-size vector (Uint32Vec) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md Example of a `fixvec` containing six Uint32 values. The serialization includes a 4-byte length prefix (representing 6 items) followed by each Uint32 serialized in little-endian format. ```hex 06 00 00 00 23 01 00 00 56 04 00 00 90 78 00 00 0a 00 00 00 bc 00 00 00 ef 0d 00 00 ``` -------------------------------- ### C Implementation of Load Transaction Syscall Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0009-vm-syscalls/0009-vm-syscalls.md The `ckb_load_transaction` function provides a C interface for the *Load Transaction* syscall (ID 2051). It serializes the full transaction into Molecule Encoding format and copies it to the VM memory space. The `addr`, `len`, and `offset` arguments are used for partial loading, specifying the destination address, data length, and starting offset. ```C int ckb_load_transaction(void* addr, uint64_t* len, size_t offset) { return syscall(2051, addr, len, offset, 0, 0, 0); } ``` -------------------------------- ### Response for Indexing Lock Hash (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON object is the response from the `index_lock_hash` RPC call, confirming the indexed `lock_hash` along with the `block_hash` and `block_number` where indexing started. ```json { "id": 2, "jsonrpc": "2.0", "result": { "block_hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40", "block_number": "0x400", "lock_hash": "0x4ceaa32f692948413e213ce6f3a83337145bde6e11fd8cb94377ce2637dcc412" } } ``` -------------------------------- ### Response for Setting IP Ban Status (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON snippet provides an example response for the `set_ban` RPC call. A `null` value in the `result` field indicates that the operation to insert or delete an IP/Subnet from the banned list was successful. ```json { "id": 2, "jsonrpc": "2.0", "result": null } ``` -------------------------------- ### CKB Syscall: Loading First Witness from Input (C) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0022-transaction-structure/0022-transaction-structure.md This CKB syscall is used to load data from a transaction's witnesses. Specifically, this example loads the first witness (index 0) associated with the transaction's inputs (`CKB_SOURCE_INPUT`). `addr`, `len`, and `offset` control where the data is stored and how much is read. This is commonly used to retrieve signatures or other witness data required for script validation. ```C ckb_load_witness(addr, len, offset, 0, CKB_SOURCE_INPUT); ``` -------------------------------- ### Response for Transaction Pool Info (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON snippet provides an example response for the `tx_pool_info` RPC call. The `result` object contains various metrics about the transaction pool, including `last_txs_updated_at`, `min_fee_rate`, `orphan`, `pending`, `proposed` transaction counts, and total `tx_cycles` and `tx_size`. ```json { "id": 2, "jsonrpc": "2.0", "result": { "last_txs_updated_at": "0x0", "min_fee_rate": "0x0", "orphan": "0x0", "pending": "0x1", "proposed": "0x0", "total_tx_cycles": "0x219", "total_tx_size": "0x112" } } ``` -------------------------------- ### Sending a Transaction to the Pool (Bash) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This `bash` example demonstrates how to send a new transaction to the CKB node's transaction pool using the `send_transaction` RPC method. The request includes a detailed transaction object with `cell_deps`, `header_deps`, `inputs`, `outputs`, `outputs_data`, `version`, and `witnesses`, along with an `outputs_validator` parameter. ```bash echo '{ "id": 2, "jsonrpc": "2.0", "method": "send_transaction", "params": [ { "cell_deps": [ { "dep_type": "code", "out_point": { "index": "0x0", "tx_hash": "0xa4037a893eb48e18ed4ef61034ce26eba9c585f15c9cee102ae58505565eccc3" } } ], "header_deps": [ "0x7978ec7ce5b507cfb52e149e36b1a23f6062ed150503c85bbf825da3599095ed" ], "inputs": [ { "previous_output": { "index": "0x0", "tx_hash": "0x365698b50ca0da75dca2c87f9e7b563811d3b5813736b8cc62cc3b106faceb17" }, "since": "0x0" } ], "outputs": [ { "capacity": "0x2540be400", "lock": { "args": "0x", "code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5", "hash_type": "data" }, "type": null } ], "outputs_data": [ "0x" ], "version": "0x0", "witnesses": [] }, "passthrough" ] }' \ | tr -d '\n' \ | curl -H 'content-type: application/json' -d @- \ http://localhost:8114 ``` -------------------------------- ### Completing Withdrawal from Nervos DAO (JavaScript) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/sdk/sdk-javascript.md This asynchronous function completes the Nervos DAO withdrawal process. It requires both the initial `depositOutPoint` and the `withdrawOutPoint` from the withdrawal start transaction. It generates, signs, and sends the final withdrawal transaction. ```JavaScript const withdraw = async () => { await ckb.loadDaoDep() await ckb.loadSecp256k1Dep() await loadCells() const tx = await ckb.generateDaoWithdrawTransaction({ depositOutPoint, withdrawOutPoint: startWithDrawOutPoint, fee: BigInt(100000) }) const signed = ckb.signTransaction(sk)(tx) const txHash = await ckb.rpc.sendTransaction(signed) const outPoint = { txHash, index: '0x0' } console.log(`const withdrawOutPoint = ${JSON.stringify(outPoint, null, 2)}`) } ``` -------------------------------- ### Serializing `option` with a Some Value (Empty Bytes) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0008-serialization/0008-serialization.md This example illustrates the serialization of an `option` type containing `Some` value, where the inner item is an empty `Bytes` vector (`[0x]`). It shows the total size of the inner `BytesVec` and its internal structure (offset and empty item length). ```Hex Bytes # the full size of BytesVec is 12 bytes 0c 00 00 00 # the offset of Bytes 08 00 00 00 # the length of Bytes 00 00 00 00 ``` -------------------------------- ### Setting IP Ban Status (Bash) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This `bash` example illustrates how to use the `set_ban` RPC method to insert or delete an IP/Subnet from the CKB node's banned list. It sends a JSON-RPC request with parameters for the `address`, `command` (insert/delete), optional `ban_time`, `absolute` flag, and `reason`. ```bash echo '{ "id": 2, "jsonrpc": "2.0", "method": "set_ban", "params": [ "192.168.0.2", "insert", "0x1ac89236180", true, "set_ban example" ] }' \ | tr -d '\n' \ | curl -H 'content-type: application/json' -d @- \ http://localhost:8114 ``` -------------------------------- ### Requesting a CKB Block Template (JSON-RPC) Source: https://github.com/nervoscommunity/docs/blob/master/docs/rfcs/0013-get-block-template/0013-get-block-template.md This snippet demonstrates JSON-RPC requests to the `get_block_template` method. It shows examples with and without optional parameters like `cycles_limit`, `bytes_limit`, and `max_version`, which allow clients to specify desired maximums for the block template. If omitted, consensus-level defaults are used. ```JSON { "jsonrpc": "2.0", "id": 1, "method": "get_block_template", "params": { "cycles_limit": 100000000, "bytes_limit": 2000000, "max_version": 0 } } ``` ```JSON { "jsonrpc": "2.0", "id": 1, "method": "get_block_template", "params": {} } ``` -------------------------------- ### Building Static Website Content with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command generates static content for the website and places it into the `build` directory. The resulting static files can then be served using any static content hosting service. ```bash $ yarn build ``` -------------------------------- ### Benchmarking Secp256k1 Performance on CKB-VM Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This snippet executes the compiled secp256k1 binaries (`secp_wabt` and `secp_wavm_patched`) using a `runner.rb` script to measure their performance on the CKB-VM. It displays file sizes and detailed cycle consumption metrics (total, transfer, and running cycles) for different input values, demonstrating the performance differences between the WABT and WAVM approaches. ```shell ls -l secp_wabt secp_wavm_patched -rwxrwxr-x 1 ubuntu 1791744 Mar 3 05:27 secp_wabt* -rw-rw-r-- 1 ubuntu 1800440 Mar 3 05:29 secp_wavm_patched ./runner.rb secp_wabt 0x01010000 Run result: Ok(0) Total cycles consumed: 35702943 Transfer cycles: 438060, running cycles: 35264883 ./runner.rb secp_wabt 0x01050000 Run result: Ok(0) Total cycles consumed: 90164183 Transfer cycles: 438060, running cycles: 89726123 ./runner.rb secp_wavm_patched 0x01010000 Run result: Ok(0) Total cycles consumed: 10206568 Transfer cycles: 428764, running cycles: 9777804 ./runner.rb secp_wavm_patched 0x01050000 Run result: Ok(0) Total cycles consumed: 49307936 Transfer cycles: 428764, running cycles: 48879172 ``` -------------------------------- ### Response for Sending Transaction (JSON) Source: https://github.com/nervoscommunity/docs/blob/master/docs/docs/HTTPRPC/rpc-specification.md This JSON snippet shows an example response for the `send_transaction` RPC call. The `result` field contains the transaction hash (e.g., `0xa0ef4eb5f4ceeb08a4c8524d84c5da95dce2f608e0ca2ec8091191b0f330c6e3`), indicating that the transaction was successfully submitted to the transaction pool. ```json { "id": 2, "jsonrpc": "2.0", "result": "0xa0ef4eb5f4ceeb08a4c8524d84c5da95dce2f608e0ca2ec8091191b0f330c6e3" } ``` -------------------------------- ### Deploying Website to GitHub Pages with Yarn Source: https://github.com/nervoscommunity/docs/blob/master/README.md This command provides a convenient way to build the website and push it to the `gh-pages` branch, specifically for hosting on GitHub Pages. It requires specifying a GitHub username and using SSH for authentication. ```bash $ GIT_USER= USE_SSH=true yarn deploy ``` -------------------------------- ### Compiling and Patching Secp256k1 Binaries for CKB-VM Source: https://github.com/nervoscommunity/docs/blob/master/blog/2020-03-03-ckbscript-08.md This snippet outlines the compilation process for both WABT and WAVM versions of the secp256k1 code using `riscv64-unknown-elf-gcc` within a Docker container. It specifies include paths and linker scripts. Finally, it uses `ckb-binary-patcher` to optimize the WAVM binary for CKB-VM execution. ```shell cd $TOP sudo docker run --rm -it -v `pwd`:/code nervos/ckb-riscv-gnu-toolchain:bionic-20191209 bash cd /code/ riscv64-unknown-elf-gcc -O3 -I ckb-c-stdlib -I wavm-aot-generator -I wabt/wasm2c secp_wabt_main.c secp.c wabt/wasm2c/wasm-rt-impl.c -o secp_wabt riscv64-unknown-elf-gcc -O3 -I ckb-c-stdlib -I wavm-aot-generator -I wabt/wasm2c secp_wavm_main.c wavm-aot-generator/abi/riscv64_runtime.S secp_precompiled.o -o secp_wavm -Wl,-T wavm-aot-generator/abi/riscv64.lds exit exit ckb-binary-patcher/target/release/ckb-binary-patcher -i secp_wavm -o secp_wavm_patched ```