Checks and range hashes¶
Recordings get copied, cut apart and archived, and whoever holds a copy must know that it is whole and unchanged. A tape answers this at three levels. A chain's links show a missing or swapped piece, and a reader verifies a piece's bytes. A range hash proves that two files hold the same data in a range of items.
For example, the drive's last piece, which holds the fault, is copied alone to another computer, and an archive keeps the whole chain. The copy must hold the frames the bench recorded, and the archive must hold every piece unchanged.
The links of a chain¶
Each piece names its neighbours, so a reader tells a whole chain from one with a piece missing or swapped. The links run both ways:
- Backward. Each piece head names the piece before it by its id and by its content hash. The content hash is a BLAKE3 hash, a cryptographic hash, over the earlier piece's bytes, and the earlier piece's close states it.
- Forward. A close cannot name the next piece's hash, which is not written yet. It names the next piece's id, and the next piece head repeats that id.
TapeReader::chain checks every link between the pieces at hand. It reads nothing beyond what opening read, since
the heads and closes are already in memory. Its ChainReport lists each link, how the chain ends, and the pieces
that belong to another chain. ChainReport::is_whole says that every piece is there and linked.
let report = reader.chain(); // each head against the piece before it, each close against the next
let verified = (0..reader.pieces().len())
.map(|i| reader.verify(i).map(|v| v.holds)) // every byte hashed and replayed
.collect::<Result<Vec<bool>, _>>()?;
What the example uses here: Checked
/// What [check] found.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Checked {
/// The chain's links that are [Linked](ww_tape::piece::ChainLink::Linked), and whether it is whole.
pub linked: usize,
pub whole: bool,
/// Each piece holds the content hash its close states.
pub verified: Vec<bool>,
}
The drive's chain has three links between its four pieces, and all three are linked. The report also tells a chain whose first pieces were deleted to free space from one with a piece lost in the middle. Tape pieces in the format reference lists every case.
Verifying a piece¶
A link proves that a piece head names the hash its neighbour states. It does not prove that the piece's bytes still
have that hash. TapeReader::verify does: it hashes the piece's bytes before its close and compares the result with
the content hash that the close states.
Verify also replays the piece block by block, as its writer placed them. Each index entry must be the entry that the writer makes of the block at its offset. Each track's row in the close must match the track's blocks. A writer that wrote a wrong index therefore fails too, not only a changed byte.
Verify reads every byte. For the drive's chain it reads about 119 MB in 41 472 reads. That is about 3.9 MB more than the recording's 116 MB. Verify reads each block's 32-byte header before the block, 13 816 headers in all.
Verify reads each final index whole, about 1.8 MB in all. It then reads the first 120 bytes of each index entry again, one by one as it replays the blocks: about 1.7 MB more. It does not read the closes and tails, about 13 KB in all.
All four pieces hold. An archive runs verify once after a transfer and on a schedule, never on each open.
Track hashes¶
A content hash covers a piece's bytes, so it changes whenever any byte does. Two files can hold the same data in other bytes, such as a recording joined into one file and the same recording in four. To compare data, a reader needs a hash of a track's content that does not depend on where the content lies.
The track hash is that hash. It starts as 32 zero bytes and takes in each chunk and each record of the track as the writer writes them. The writer leaves out puts and state copies, which repeat what the tape already holds. Each chunk's share is its unit hash, which the writer stores in the chunk's index entry. The piece head states each track's hash before the piece, and the close states it after the piece.
The track hash depends only on the track's chunks and records, in order. A track written alone or among other tracks, in one piece or in many, ends with the same hash. A recording relayed or stored again block for block keeps that hash.
The hash takes in each chunk whole: its items, its head and how its columns are coded. Two tracks with the same hash therefore hold the same chunks, and so the same data. A different hash proves less. The same items cut into other chunks, or coded with another codec, hash differently. Only a read of the items then tells whether the data differ.
A track's definition also names its track owner, 32 bytes that ww_tape does not interpret. Two devices of the same type recorded into one tape have tracks with the same keys, and the owner tells them apart. Each such track has its own item numbers and its own hash.
The hash of a range¶
A copy of the last piece alone cannot compare whole track hashes with the archive's chain. It compares a range
instead. TapeReader::range_hash folds the unit hashes of the chunks that hold any item of a range, from 32
zero bytes, and returns a RangeHash. The result depends only on those chunks, so the archive's chain and the
copied piece give the same hash for the same range:
let can0 = key("can0").id();
let whole = reader.range_hash(can0, OwnerId::NONE, items.clone(), TrackHash::START)?;
let alone = copy_reader.range_hash(can0, OwnerId::NONE, items.clone(), TrackHash::START)?;
let same = whole.hash == alone.hash && whole.items == alone.items;
What the example uses here: frame_items, SameFrames
/// The item numbers of `can0` whose chunks start within minutes `from..to`, from the index of piece `piece`.
fn frame_items<S: ByteSource>(
reader: &TapeReader<S>,
piece: usize,
minutes: core::ops::Range<i64>,
) -> Result<core::ops::Range<ItemNum>, FaultError<S::Error>> {
let (from, to) = (minutes.start * US_PER_MINUTE, minutes.end * US_PER_MINUTE);
let chunks: Vec<IndexEntry> = reader
.track_entries(piece, key("can0").id())?
.into_iter()
.filter(|e| e.kind == BlockKind::Events && from <= e.first_tick && e.first_tick < to)
.collect();
let first = chunks.first().ok_or(FaultError::Missing("frames"))?;
let last = chunks.last().ok_or(FaultError::Missing("frames"))?;
Ok(first.first_item..last.end_item())
}
/// What [same_frames] found.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SameFrames {
/// The frames of minutes 51 to 53: their item numbers, and the chunks the range hash folded, all from the index.
pub items: core::ops::Range<ItemNum>,
pub chunks: u64,
pub stated: u64,
/// The range hash in the whole chain and in a copy of its last piece alone are equal.
pub same: bool,
}
For the frames of minutes 51 to 53, items 3 060 000 to 3 179 999 of can0, the range hash folds 120 chunks. Each
unit hash comes from its index entry, so the fold reads no payload: the chain's reader reads about 577 KB in 15 reads.
The hashes in the chain and in the last piece alone are equal. A program that analysed those frames can store the
range hash beside its result. Anyone can later check that a file still holds that input.
A fold from the index gives the hash that the writer stated there. RangeHash::stated counts the chunks folded that
way, all 120 here. Only verify proves the stated hashes against the chunks' bytes, and every read of a chunk checks its
own. Where the index is damaged or missing, the reader reads the chunks and computes the same hash. It counts the
bytes that no block explains in skipped, so a fold over damage never passes for a whole one.
When bytes go bad shows what still reads when an archived piece has a flipped bit.
TapeReader.pub fn chain(&self) -> ChainReport
How the pieces with a head link up (PieceInfo::summary); its indices are into Self::pieces.
pub struct ChainReport
check_chain's answer.
pub fn is_whole(&self) -> bool {
!self.head_cut
&& self.strays.is_empty()
&& self.refused.is_empty()
&& matches!(self.end, ChainEnd::Closed { .. })
&& self.links.iter().all(|l| {
matches!(
l,
ChainLink::Linked | ChainLink::Unproven | ChainLink::Removed { .. }
)
})
}
Every piece there or removed on purpose, linked, from the first to a close record of the last; a link by id alone (ChainLink::Unproven) counts.
pub fn verify(&self, i: usize) -> R<Verification, S>
Whether closed piece i holds up as a whole: its bytes before its close record hash to the close's content
hash (each block that reads whole as it is, a chunk's payload by its hash, other bytes as they are,
piece docs); its final index and the blocks walked are the same set: each block walked that
its writer lists has exactly the entries its writer made of it, field for field (offset, length, kind, track,
items, ticks, a chunk's unit hash of its bytes and its range relation's range from its column), but a
byte-identical copy of the listed block before it; each entry names a block walked, or a block that doesn't
read whole in bytes no block explains, ending before the close and before the next block walked; and each
close row states the hash its track's blocks fold to from the head row's, or says it incomplete exactly where
a damaged block kept or the head leaves units out.
TapeWriter.pub fn range_hash(
&self,
track_id: TrackId,
owner: OwnerId,
items: Range<ItemNum>,
from: TrackHash,
) -> R<RangeHash, S>
The hash of the whole chunks of track track_id owned by owner that hold an item in items, folded from
from (TrackHash::then) over the pieces in order.
pub struct RangeHash
What TapeReader::range_hash folded.
pub fn stated(&self) -> u64 {
self.chunks.saturating_sub(self.read)
}
Chunks folded by the unit hash their index entry states, not read: the writer's statement, which TapeReader::verify proves.