cad3-encoding

Định dạng mã hóa CAD3 — các ô, tham chiếu nhúng so với tham chiếu nhánh, ID giá trị, quy tắc hợp lệ. Sử dụng khi làm việc với bộ mã hóa, bộ giải mã, tuần tự hóa, băm hoặc…

npx skills add https://github.com/convex-dev/convex --skill cad3-encoding

CAD3 Encoding

CAD3 is the byte-level encoding for all Convex data. Normative spec: https://docs.convex.world/docs/cad/encoding. Read it before changing encoder or decoder behaviour — this skill is orientation, not a substitute.

The Model

A cell is the unit of encoding. Cells reference other cells, forming a Merkle DAG: every reference carries the hash of the referenced encoding.

  • Encoding is a byte sequence. Every cell maps to exactly one encoding, and distinct cells map to distinct encodings. Both directions matter — the round-trip and the uniqueness.
  • Value ID = SHA3-256 of the encoding. This is the content address.
  • Maximum encoding length is 16383 bytes, so any cell fits a fixed buffer and most operations stay O(1).
  • The first byte is the tag, which determines how the rest is read. A tag not defined in CAD3 MUST be rejected.

Embedded vs Branch

The distinction drives both correctness and performance.

  • Embedded: the child's encoding sits inside the parent's encoding. An embedded cell MUST be 140 bytes or less.
  • Branch: the child is referenced externally by value ID, and must be fetched separately.

A cell that is embedded MUST NOT also be referenced externally. Allowing both would give a parent two valid encodings, breaking uniqueness. When you touch embedding rules, that invariant is what you are protecting.

Embedding is why [1 2 3 4 5] is one encoding rather than six, and why embedded values cost zero memory — see the memory skill.

Validity

An encoding is valid if some cell produces exactly those bytes. Implementations MUST reject:

  • trailing bytes after a complete valid encoding
  • a sequence that ends before the encoding is complete
  • undefined or reserved tags

Random bytes are almost always invalid, which is what lets a peer discard corrupt or hostile input cheaply. Preserve that property — it is load-bearing for the peer's robustness against malicious messages.

Traps

Do not "fix" the decoder to reject non-canonical NaN. Every 64-bit pattern in a Double is a valid encoding, including every distinct NaN payload, both signed zeroes, infinities and subnormals. Each is a distinct value with its own value ID. The CVM defines one canonical NaN (##NaN, 0x1d7ff8000000000000) and normalises results to it, but that is a CVM value-layer concern enforced by coercion — never by rejecting an encoding. This looks like a decoder bug and is not one.

Contrast with Integers, where excess leading bytes are genuinely redundant and therefore invalid. The test is whether two byte sequences would denote the same value: if so, only one may be legal.

Preserve values you do not understand. CAD3 is deliberately extensible — applications assign their own meaning to values, particularly in the 0xAn, 0xCn, 0xDn and 0xEn categories. An implementation MUST relay encoded values it cannot interpret rather than dropping or normalising them.

Value IDs of non-branch cells may not be in storage. Only roots and branches are generally persisted. If you hold a value ID for an intermediate cell, navigate down from a known root instead of assuming a store lookup will resolve it.

Where the Code Lives

Encoding logic is in convex-core: see convex.core.cvm.CVMEncoder, CVMTag, and convex-core/docs/ENCODER_DESIGN.md.

Changes here affect consensus compatibility. SnapshotStateTest replays a fixed state and checks its hash — if your change moves that hash, it is a consensus-visible change, not a refactor, and needs to be gated on a protocol version rather than shipped unconditionally.

Thêm skills từ convex-dev

account
convex-dev
Tạo hoặc kiểm tra các account Convex. Sử dụng khi người dùng muốn thiết lập account mới, kiểm tra chi tiết account, hoặc quản lý keys.
account
convex-dev
Tạo hoặc kiểm tra tài khoản Convex. Sử dụng khi người dùng muốn thiết lập tài khoản mới, kiểm tra chi tiết tài khoản hoặc quản lý khóa.
token
convex-dev
Tạo và quản lý token có thể thay thế trên Convex. Sử dụng khi người dùng muốn tạo token mới, kiểm tra số dư token, hoặc quản lý nguồn cung token.
build-convex
convex-dev
Xây dựng dự án Convex từ mã nguồn. Sử dụng khi một người đóng góp muốn biên dịch, kiểm thử hoặc đóng gói Convex.
convex-db
convex-dev
Sử dụng Convex DB — cơ sở dữ liệu SQL hỗ trợ lattice. Sử dụng khi giúp người dùng viết truy vấn, kết nối qua JDBC hoặc máy khách PostgreSQL, tạo bảng, chèn/truy vấn dữ liệu,…
convex-lisp
convex-dev
Tài liệu tham khảo ngôn ngữ Convex Lisp — quy ước CVM, gọi mã thư viện, định nghĩa actor, mã juice và lỗi. Sử dụng khi viết hoặc gỡ lỗi mã nguồn CVM cho…
deploy
convex-dev
Triển khai một actor (hợp đồng thông minh) lên mạng Convex. Sử dụng khi người dùng muốn tạo một actor trên chuỗi mới với các hàm được xuất.
ecosystem
convex-dev
Định hướng trong hệ sinh thái Convex — thứ gì nằm trong repository nào, specs và docs ở đâu, và những thư viện client nào tồn tại. Sử dụng khi bạn cần ngữ cảnh…