MMOframework
Why AI Playground Libraries Developers Agency
FR EN
Build a project live
Index

LogChain

MMO\core\helpers · 1 symbol · generated 30 Nov 2026

class

LogChain

LogChain.php:22

A journal whose lines cannot be changed, removed or put back without it showing (ISO/IEC 27001 A.8.15, A.5.28, A.5.33): each line carries, in its FIELD, its number (seq), the hash of the line before (previous) and its own (hash), the SHA-256 of those and of its content. Changing a line changes its hash; removing one leaves a gap in the numbers; rewriting one's hash breaks the next line's previous.

final class MMO\core\helpers\LogChain
Namespace
MMO\core\helpers
See
Verify reads them back (php admin.php log-verify). A purge removes the oldest lines only, and keeps the last one it removes as a seal ({seq, hash}): the first line left chains to it.
The repository writing the journal chains its lines (mmo.mongodb's `MongoRepository::$logChain`) and What it does not prevent: whoever can write to the database can compute a hash as this class does, and rewrite every line from the one it changes to the last. Keep the head (`log-verify` prints it) out of the database -- a backup, a ticket -- and check it later (`--head=<seq>:<hash>`): a rewritten or truncated chain no longer has it.
$doc['chain'] = LogChain::Link($doc, $previous); // $previous: the last line's chain, or a seal, null for the first LogChain::Verify($cursor)['valid']; // false if a line was changed, removed or put back

Index

FIELDthe key of a line holding its link: {seq: int, previous: ?string, hash: string}
MAX_BREAKSthe most breaks Verify lists: past one, the chain is broken already
Canonical($document) staticThe text a line is hashed as: its content, without its link, as json, the keys of each map sorted -- what it reads back from its database (a BSON type as its extended json, mmo.mongodb's ObjectId a…
Hash($document, $seq, $previous) static
IdOf($document) static
Link($document, $previous) staticThe link of $document, after the line (or the seal) $previous
Normalize($value) static
ParseHead($head) static
Verify($documents, $anchor, $expect) staticRead a chain back and tell where it breaks.

Constants2

FIELD

the key of a line holding its link: {seq: int, previous: ?string, hash: string}

const FIELD = 'chain'

MAX_BREAKS

the most breaks Verify lists: past one, the chain is broken already

const MAX_BREAKS = 20

Methods7

Canonical($document)

staticLogChain.php:35

The text a line is hashed as: its content, without its link, as json, the keys of each map sorted -- what it reads back from its database (a BSON type as its extended json, mmo.mongodb's ObjectId as {"$oid": ...}) is what it was written with.

static function Canonical(array $document): string
$documentarray
returnsstring
LogChain::Canonical(['b' => 1, 'a' => [2, 1.0]]); // '{"a":[2,1.0],"b":1}'

Hash($document, $seq, $previous)

staticLogChain.php:60
static function Hash(array $document, int $seq, ?string $previous): string
$documentarraythe line (its FIELD left out)
$seqintits number in the chain: 1 for the first line
$previousnull | stringthe hash of the line before, null for the first
returnsstringthe hash of the line: SHA-256, in hexadecimal
LogChain::Hash(['type' => 'INSERT'], 1, null); // 64 hexadecimal characters

IdOf($document)

staticLogChain.php:138
protected static function IdOf(array $document): string
$documentarray@return string its id (_id or id), as text
returnsstring

Normalize($value)

staticLogChain.php:41
protected static function Normalize(mixed $value): mixed
$valuemixed
returnsmixed$value with every map's keys sorted, an object as what it serializes to

ParseHead($head)

staticLogChain.php:148
static function ParseHead(string $head): ?array
$headstring<seq>:<hash>, as log-verify prints it
returnsnull | arrayint, hash: string} null when $head is not one
LogChain::ParseHead('42:ab12...'); // ['seq' => 42, 'hash' => 'ab12...']

Verify($documents, $anchor, $expect)

staticLogChain.php:90
Read a chain back and tell where it breaks. Each break is `{seq, id, reason}`: - `modified`: the line's content (or its hash) is no longer what was hashed; - `missing`: lines `seq` to `to` are gone (removed, and no purge sealed them); - `unlinked`: the line does not chain to the one before (that one was replaced, or the chain rewritten from here); - `head`: the line $expect names has another hash (the chain was rewritten), `truncated`: it is gone (the last lines were removed).static function Verify(iterable $documents, ?array $anchor = null, ?array $expect = null): array
$documentsiterablethe chained lines (arrays: a cursor's documents), by seq ascending
$anchor = nullnull | array
$expect = nullnull | array
returnsarraybool, count: int, first: ?int, head: ?array{seq: int, hash: string}, breaks: list<array<string, int|string>>}
LogChain::Verify($lines)['breaks'][0]; // ['seq' => 7, 'id' => '6af4...', 'reason' => 'modified']