verify static method
Walks the chain and reports the first line whose hash no longer matches or whose link points nowhere; a link to an earlier line other than the last is a fork from concurrent writers before appends were locked, counted in AuditVerifyResult.forks.
Implementation
static AuditVerifyResult verify(String dataDir) {
final entries = readAll(dataDir);
if (entries.isEmpty) {
return const AuditVerifyResult(totalEntries: 0, intact: true);
}
final seen = <String>{};
var previousThisHash = _zeroHash;
var forks = 0;
AuditVerifyResult broken(int i, String reason) => AuditVerifyResult(
totalEntries: entries.length,
intact: false,
brokenAtIndex: i,
brokenReason: reason,
forks: forks,
firstTs: entries.first?.ts,
);
for (var i = 0; i < entries.length; i++) {
final entry = entries[i];
if (entry == null) return broken(i, 'malformed line');
final linksPrevious = entry.prevHash == previousThisHash;
if (!linksPrevious && !seen.contains(entry.prevHash)) {
return broken(
i,
'prev_hash mismatch (expected ${_short(previousThisHash)}, '
'got ${_short(entry.prevHash)})');
}
if (!linksPrevious) forks++;
final preimage = '${entry.ts.toIso8601String()}|${entry.prevHash}|${entry.payloadB64}';
final recomputed = sha256.convert(utf8.encode(preimage)).toString();
if (recomputed != entry.thisHash) {
return broken(
i,
'this_hash mismatch (recomputed ${_short(recomputed)}, '
'recorded ${_short(entry.thisHash)})');
}
seen.add(entry.thisHash);
previousThisHash = entry.thisHash;
}
return AuditVerifyResult(
totalEntries: entries.length,
intact: true,
forks: forks,
firstTs: entries.first?.ts,
lastTs: entries.last?.ts,
);
}