Expand description
Foundation for KIND blockchain collections (issue #523).
Persists kind in red_config, exposes the reserved-column set used by
every block row, scans the collection to derive the current chain tip,
and wraps the hash helpers from crate::storage::blockchain.
This iteration does NOT validate user-supplied prev_hash/height on INSERT
(the engine computes them) and does NOT enforce conflict-retry semantics
— those land alongside chain-tip RPC and verify_chain in a later slice.
Structs§
- Chain
Tip - Chain tip derived by scanning the collection.
- Chain
TipFull - Full chain tip including timestamp. Returned by
chain_tip_fullandGET /collections/:name/chain-tip. - Verify
Chain Outcome - Outcome of
POST /collections/:name/verify-chain.
Constants§
- CHAIN_
KIND_ TAG - Value stored under
red.collection.{name}.kindfor blockchain collections. - COL_
BLOCK_ HEIGHT - COL_
HASH - COL_
PREV_ HASH - COL_
TIMESTAMP - RESERVED_
COLUMNS - Reserved column names auto-filled by the engine; user INSERTs that supply them are silently overwritten so the chain remains engine-controlled.
Functions§
- canonical_
payload - Canonicalize user-supplied fields for inclusion in the block hash. Sorted
by key,
<key>=<plain_text>;joined — stable across reorderings so the recomputed hash matches regardless of column order at INSERT-time. - chain_
conflict_ error - Build the
BlockchainConflict:<json>error payload mapped to HTTP 409 (#524). The JSON body carries the current tip so the caller can retry with the rightprev_hash/block_height. - chain_
tip - Scan the collection for the highest
block_heightand return its row’shash. O(n) — replaced by a cached tip in a later iteration. - chain_
tip_ full - Scan-based tip with timestamp.
Nonewhen the collection has no rows (pre-genesis). Used by the chain-tip endpoint and the chain-INSERT validation path (#524). - collect_
blocks - Issue #525 — walk a chain collection in
block_heightorder and rebuild theBlocklist expected byverify_chain. Genesis is identified byblock_height == 0. ReturnsNonewhen the collection is absent or not a chain. Reserved-column extraction and canonical payload encoding mirror what the engine writes at INSERT time — this is the alignment of the two encoders referenced in #524. - genesis_
fields - Convenience: produce the genesis row’s full field list. Genesis carries an empty user payload — extra metadata is recorded in subsequent blocks.
- is_
chain - True if
mark_as_chainwas ever called for this collection. - is_
integrity_ broken_ persisted - Scan
red_configfor the latest persisted integrity flag forcollection.Nonemeans no record (treated as ok). - make_
block_ reserved_ fields - Build the reserved-column key/value pairs for a new block. Caller appends
these to the row’s
fieldsAFTER stripping any user-supplied reserved columns. The returnedhashis also returned so callers can advance the tip without recomputing. - mark_
as_ chain - Persist the
chainkind marker. Append-only — only call once at creation. - now_ms
- persist_
integrity_ flag - Persist a chain’s integrity flag in
red_config. Append-only — readers useis_integrity_broken_persistedwhich picks the latest matching row by id. - verify_
chain_ outcome - Walk the chain end-to-end. Returns
(outcome, _). Mismatches are detected viaverify_chain. Callers persist the integrity flag onok == false.