CKB Contract Flow

CKBFS V2 约束(witness 文件存储)CKBFS V2 Constraints (witness file storage)

来源:code-monad/ckbfs @db973a8 contracts/ckbfs/src/entry.rs + contracts/ckb-adler32(mode 1) · data1 0x31e6…8d3a(主网 + 测试网)Source: code-monad/ckbfs @db973a8 contracts/ckbfs/src/entry.rs + contracts/ckb-adler32 (mode 1) · data1 0x31e6…8d3a (mainnet + testnet)
Type 脚本Type script代理 / 工具Proxy / utility主网Mainnet测试网Testnet开源Open source社区Community
贡献者:Contributed by @code-tsao @code-tsao

给 AI agent:For AI agents: ckbfs_v2.md · llms.txt

0 → 1发布(创建)Publish (create)

Inputs 输入Inputs
inputs[0]:任意inputs[0]: anyCellInput{ since, previous_output }
Outputs 输出Outputs
CKBFS cellCell{ type: { codeHash: CKBFS_V2, hashType: data1, args: type_id 32B ‖ [hasher_code_hash 32B] }, data: CKBFSData { indexes, checksum, content_type, filename, backlinks: [] } }
witnesses[i], i ∈ indexes"CKBFS"(5B) ‖ 0x00(1B) ‖ content part
args 字节布局args byte layout
type_id32B
hasher_code_hash32B?
  • type_id32B唯一 IDunique id= blake2b(inputs[0] ‖ 输出序号 u64)= blake2b(inputs[0] ‖ output index u64)102
  • hasher_code_hash32B?可选:校验器 data1 code hashoptional: hasher data1 code hash缺省用内置 ckb-adler32 hashdefaults to the built-in ckb-adler32 hash106
data(molecule table CKBFSData)data (molecule table CKBFSData)
indexesUint32[]
checksum4B LE
content_typeBytes
filenameBytes
backlinksBackLink[]
  • indexesUint32[]内容所在 witness 序号witness indexes holding the content创建时非空non-empty on create101
  • checksum4B LEAdler-32= adler32(各 witness[6..] 依次拼接)= adler32(witness[6..] of each index, in order)-101
  • content_typeBytesMIME之后不可变immutable afterwards105
  • filenameBytes文件名file name之后不可变immutable afterwards105
  • backlinksBackLink[]历史片段 {indexes, checksum, tx_hash}earlier parts {indexes, checksum, tx_hash}创建时必须为空(见说明)must be empty on create (see notes)101

限制Constraints

  • type同一 tx 中该 type 的输出 ≤ 1≤ 1 output with this type in the tx107
  • args≥ 32 字节;args[0..32] 按 type_id 规则≥ 32 bytes; args[0..32] by the type_id rule-3 / 102
  • data合法 molecule;indexes 非空;backlinks 为空valid molecule; indexes non-empty; backlinks empty-4 / 101
  • cell_deps含 data hash = 校验器 code hash 的 cell(解码器不检查)contains a cell whose data hash = the hasher code hash (not checked by the decoder)106
  • checksumexec ckb-adler32 mode 1:adler32(witnesses[indexes][6..]) = data.checksum;witness 缺失或 < 6 字节时 panicexec ckb-adler32 mode 1: adler32(witnesses[indexes][6..]) = data.checksum; a missing or < 6-byte witness panics-101

1 → 1追加内容Append

Inputs 输入Inputs
CKBFS cellCell{ type: CKBFS_V2, data: { indexes: I, checksum: c0, content_type, filename, backlinks: B } }
Outputs 输出Outputs
CKBFS cellCell{ type: same, data: { indexes: I′ ≠ [], checksum: c1, content_type, filename, backlinks: B ‖ BackLink{ indexes, checksum, tx_hash } } }
witnesses[i], i ∈ I′"CKBFS" ‖ 0x00 ‖ new content part

限制Constraints

  • content_type / filename不变unchanged105
  • backlinks若输入 indexes 非空或输入 backlinks > 1:输出 backlinks 恰多 1 个;最后一个的 tx_hash = 输入 out_point.tx_hash 或 indexes = 输入 indexesif input indexes is non-empty or input backlinks > 1: output has exactly one more backlink; the last one has tx_hash = input out_point.tx_hash or indexes = input indexes108
  • backlinks输入 backlinks 原样作为输出前缀input backlinks kept unchanged as the output prefix108
  • checksum从 c0 恢复 Adler-32 状态,继续累加 witnesses[I′][6..],结果 = c1resume Adler-32 from c0 over witnesses[I′][6..]; result = c1-101

1 → 1转移Transfer

Inputs 输入Inputs
CKBFS cellCell{ lock: A, type: CKBFS_V2, data: { checksum: c0, backlinks: B, … } }
Outputs 输出Outputs
CKBFS cellCell{ lock: B′, type: same, data: { indexes: [], checksum: c0, content_type, filename, backlinks: … } }

限制Constraints

  • checksum输出 indexes 为空即视为转移,checksum 不变empty output indexes means transfer; checksum unchanged105
  • backlinks输入无 backlink:输出恰 1 个,且 tx_hash = 输入 out_point.tx_hash;否则 backlinks 完全相同input has no backlink: output has exactly 1 with tx_hash = input out_point.tx_hash; otherwise backlinks identical109 / 105

1 → 0销毁:禁止Destroy: forbidden

Inputs 输入Inputs
CKBFS cellCell{ type: CKBFS_V2 }
Outputs 输出Outputs
无 CKBFS 输出no CKBFS output—

限制Constraints

  • type有输入无输出 → 失败input without output → fails103
部署与来源Deployments & source 未核验unverified
源码source
code-monad/ckbfs@db973a8e · contracts/ckbfs/src/entry.rs · contracts/ckbfs/src/error.rs · contracts/ckbfs/src/hash.rs · contracts/ckb-adler32/src/process.rs · contracts/ckb-adler32/src/utils.rs (README "Version 20241025.db973a8e8032" names this commit for data hash 0x31e6…8d3a; its hash.rs pins ckb-adler32 0x2138…536f = the adler32 cell deployed alongside. Data schema: crates.io ckbfs-types 0.2.1)
二进制核验binary check
未核验unverified no binary or checksum at the recorded commit matched the deployed data hash (reproducible build not attempted)
网络networkcode_hashcell_deptype_id args数据哈希data hash部署交易deploy tx
mainnet0x31e6376287d223b8c0410d562fb422f04d1d617b2947596a14c3d2efb7218d3a data10xfab07962ed…147d1bfa:0 dep_group0xb8c16cf1dc…ffe77e7d0x31e6376287…b7218d3a0xc9b6698f44…67ed2ebc #14392854
testnet0x31e6376287d223b8c0410d562fb422f04d1d617b2947596a14c3d2efb7218d3a data10x469af0d961…e9d66aaa:0 dep_group0x01d5f1721e…e9576a860x31e6376287…b7218d3a0x2c8c9ad313…064d3616 #15011570
mainnet0xfd2058c9a0c0183354cf637e25d2707ffa9bb6fa2ba9b29f4ebc6be3e54ad7eb type0xfab07962ed…147d1bfa:0 dep_group0xb8c16cf1dc…ffe77e7d0x31e6376287…b7218d3a0xc9b6698f44…67ed2ebc #14392854
testnet0x7c6dcab8268201f064dc8676b5eafa60ca2569e5c6209dcbab0eb64a9cb3aaa3 type0x469af0d961…e9d66aaa:0 dep_group0x01d5f1721e…e9576a860x31e6376287…b7218d3a0x2c8c9ad313…064d3616 #15011570
链上数据核对于 on-chain data checked 2026-11-06
版本:README 把 0x31e6…8d3a 标为 "Version 20241025.db973a8e8032",即提交 db973a8(tag v2/v2.0 = 88297de 只改 README)。该提交的 hash.rs 固定 ckb-adler32 = 0x2138…536f,与链上 V2 部署交易同一 dep_group 中的 adler32 cell 一致。未做可复现构建。Version: the README labels 0x31e6…8d3a "Version 20241025.db973a8e8032", i.e. commit db973a8 (tags v2/v2.0 = 88297de only edit the README). That commit's hash.rs pins ckb-adler32 = 0x2138…536f, which matches the adler32 cell in the same on-chain dep_group. No reproducible build was attempted.
数据结构来自 crates.io ckbfs-types 0.2.1(schemas/ckbfs.mol),不是仓库 RFC.md(那时 RFC 仍是 V1 的 index: Uint32Opt)。The data layout comes from crates.io ckbfs-types 0.2.1 (schemas/ckbfs.mol), not the repo RFC.md (which still showed V1's index: Uint32Opt at that commit).
校验通过 exec_cell 跳转到 ckb-adler32,进程被替换,所以 checksum 不符时退出码是 adler32 的 -101(ValidateFailure),104 只在 exec 本身失败时出现。Checksum validation exec_cell-s into ckb-adler32, which replaces the process, so a wrong checksum exits with adler32's -101 (ValidateFailure); 104 only appears when exec itself fails.
按源码原样:创建时若 backlinks 非空,查找引用 cell 用 32 字节 code_hash 与整个序列化 Script 比较,永远不相等 → 101;且读取 cell_dep 用的是输出序号而不是找到的位置。As written: on create with non-empty backlinks, the reference-cell search compares a 32-byte code_hash with the whole serialized Script, which never matches → 101; it would also read the cell_dep at the output index rather than the found position.
只看第一个带此 type 的输入;witness 头 "CKBFS" ‖ 0x00 不被检查,只是跳过 6 字节。Only the first input with this type is considered; the witness header "CKBFS" ‖ 0x00 is not checked, just skipped (6 bytes).
错误码(error.rs):-1 IndexOutOfBound · -2 ItemMissing · -3 LengthNotEnough · -4 Encoding · 101 InvalidInitialData · 102 InvalidTypeId · 103 DeletionForbidden · 104 ChecksumMismatch · 105 InvalidFieldUpdate · 106 NoChecksumHasherFound · 107 DuplicatedOutputs · 108 InvalidAppend · 109 InvalidTransfer;ckb-adler32:-101 ValidateFailureError codes (error.rs): -1 IndexOutOfBound · -2 ItemMissing · -3 LengthNotEnough · -4 Encoding · 101 InvalidInitialData · 102 InvalidTypeId · 103 DeletionForbidden · 104 ChecksumMismatch · 105 InvalidFieldUpdate · 106 NoChecksumHasherFound · 107 DuplicatedOutputs · 108 InvalidAppend · 109 InvalidTransfer; ckb-adler32: -101 ValidateFailure
2026-11-19 上游审阅(非链上升级声明):README 仍将 V2 部署标为 20241025.db973a8e8032;HEAD 是 V3,新增链式 witness、前序位置校验和 110–112 错误码,并更换 adler32 hash。V3 已由 ckbfs_v3 独立描述,V2 继续保留旧 schema / checksum 规则。2026-11-19 upstream review (not an on-chain upgrade claim): README still identifies V2 deployments as 20241025.db973a8e8032. HEAD is V3: linked witnesses, previous-position validation, errors 110–112 and another adler32 hash. V3 is already described by ckbfs_v3; retain V2 schema and checksum rules.