macOS 上运行并通过 Ruby SDK 操作 CKB


#1

本文假设 mac 上已经安装了 homebrew, ruby, bundler 等基本开发工具。

依赖关系

要用 Ruby SDK 操作 CKB,需要了解 SDK 本身( ckb-demo-ruby-sdk )和 CKB 依赖其他一些组件。为了简单方便起见,我们将尽量避免自己编译所有组件。

secp256k1
ckb-demo-ruby-sdkmruby-contractsriscv-gnu-toolchain

mruby-contracts 二进制包

https://github.com/nervosnetwork/binary/tree/master/contracts/mruby 下载预编译好的 mruby-contracts 二进制包文件 argv_source_entry,下面配置 CKB 时会用到。

编译 CKB

brew install autoconf libtool
git clone https://github.com/nervosnetwork/ckb.git
cd ckb
rustup override set 1.31.0
rustup component add rustfmt
rustup component add clippy
cargo build # 默认使用 debug 模式编译

配置 CKB

以下假设,我们将以 ~/ckbnodes 目录来存放运行 CKB 节点的配置信息。

cp -r ckb/nodes_template ~/ckbnodes/node1
cp path/to/argv_source_entry ~/ckbnodes/node1/spec/cells/
# argv_source_entry 是上面下载的预编译二进制文件

用以下内容覆盖 ~/ckbnodes/node1/default.json 文件:

{
    "data_dir": "default",
    "ckb": {
        "chain": "spec/dev.json"
    },
    "logger": {
        "file": "ckb.log",
        "filter": "info,chain=debug",
        "color": true
    },
    "rpc": {
        "listen_addr": "0.0.0.0:8114"
    },
    "network": {
        "listen_addresses": ["/ip4/0.0.0.0/tcp/8115"],
        "boot_nodes": [],
        "reserved_nodes": [],
        "max_peers": 8
    },
    "sync": {
        "verification_level": "Full",
        "orphan_block_limit": 1024
    },
    "pool": {
        "max_pool_size": 10000,
        "max_orphan_size": 10000,
        "max_proposal_size": 10000,
        "max_cache_size": 1000,
        "max_pending_size": 10000
    },
    "miner": {
        "new_transactions_threshold": 8,
        "type_hash": "0x6c5b2cd24ce7cbc16ff368d116294b3c8e5e4f33197900396e9a35d52d8c0f83",
        "rpc_url": "http://127.0.0.1:8114/",
        "poll_interval": 5,
        "max_transactions": 10000,
        "max_proposals": 10000
    }
}

用以下内容覆盖 ~/ckbnodes/node1/spec/dev.json 文件:

{
    "name": "ckb",
    "genesis": {
        "seal": {
            "nonce": 0,
            "proof": [0]
        },
        "version": 0,
        "parent_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "timestamp": 0,
        "txs_commit": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "txs_proposal": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "difficulty": "0x100",
        "cellbase_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "uncles_hash": "0x0000000000000000000000000000000000000000000000000000000000000000"
    },
    "params": {
        "initial_block_reward": 50000
    },
    "system_cells": [
        {"path": "cells/verify"},
        {"path": "cells/always_success"},
        {"path": "cells/argv_source_entry"}
    ],
    "pow": {
        "Dummy": null
    }
}

启动 CKB 节点和矿工进程

./target/debug/ckb -c ~/ckbnodes/node1/default.json run
./target/debug/ckb -c ~/ckbnodes/node1/default.json miner

这时候已经可以通过 RPC 访问节点(http://localhost:8114)。

设置和运行 Ruby SDK

首先要安装 secp256k1

git clone https://github.com/bitcoin-core/secp256k1.git && cd secp256k1
./autogen.sh
./configure --enable-module-recovery
make && make install

然后下载运行 SDK。

git clone https://github.com/nervosnetwork/ckb-demo-ruby-sdk
cd ckb-demo-ruby-sdk
bundle
bundle exec pry -r ./lib/ckb/wallet.rb

如果一切顺利,这时候已经进入了 Ruby Console。敲入以下代码:

miner = Ckb::Wallet.from_hex(Ckb::Api.new, "e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3")
miner.address # 查看矿工地址
miner.get_balance # 查看矿工账户余额,应该已经挖出一些币来了 🤑
alice = Ckb::Wallet.from_hex(Ckb::Api.new, "76e853efa8245389e33f6fe49dcbd359eb56be2f6c3594e12521d2a806d32156")
alice.get_balance # 0
miner.send_capacity(alice.address, 12345) # 从矿工账户转 12345 个币至 alice 账户
# 等一段时间后,再来查看 alice 账户余额
alice.get_balance # 12345

说明:e79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3 这个私钥对应于我们在 default.json 中 通过 type_hash 设置的矿工地址。

以上就是用 Ruby SDK 操作 CKB 的基本流程。

注:CKB 和 Ruby SDK 都在快速迭代开发中,本文操作随时有可能因为它们的变动而过时。

参考