# chain-data backup A synced light client holds the chain it verified: block headers, filter headers and compact filters. That is the expensive part of setting up a second instance or restoring one, and on mainnet the filters are 10 to 15 GB for both chains. A backup is how a user moves that work to another machine instead of repeating it. ## what a backup is A directory: ``` manifest.json headers-00000001.nsbk headers-00200001.nsbk filter_headers-00000001.nsbk filters-00000001.nsbk ... ``` - A chunk file is self-describing: magic `NSBK`, version, kind, first height, record count, payload length, payload. `pkg/backup` writes and reads it, and refuses a payload whose length disagrees with its records. - The three kinds are headers (80 bytes each), filter headers (a filter hash and its rolling filter header, 64 bytes) and filters (height, block hash, filter hash, length, filter bytes). - `manifest.json` names every chunk with its kind, first height, count, size and SHA-256, and records the anchor, the tip and the totals. `Manifest.Validate` refuses a backup with a hole, a chunk from another range, a wrong file name, or totals that do not add up. That matters more than it looks: a manifest that promises more than the directory holds is a restore that silently verifies less than the user thinks. - Chunks are bounded by bytes (a deployment constant, 64 MiB is a reasonable default) and never split a record. Nothing here is one 20 GB file, and nothing here needs one. ## what a backup trusts The anchor is the trust: a backup begins above the checkpoint the profile already carries, so restoring it is exactly as trusting as a first sync from that checkpoint. Inside that range nothing is trusted: - headers are checked for linkage, proof of work, difficulty and timestamps before they enter the store, by the same code the network sync uses; - a filter must hash to the value the stored filter header commits to, and its block hash must be the stored header's; - the manifest's digests catch the copy that was truncated, renamed or corrupted on the way. So a hostile or damaged backup cannot make the wallet accept a chain it did not verify: the worst it can do is fail. ## exporting Export reads ranges straight out of the store: the same bytes the sync wrote, no re-encoding. The export is resumable by construction — it is a function of the store's verified tip, so a paused sync exports exactly what it verified. ## importing Import walks the chunks in ascending height per kind and refuses anything that does not continue the store it is going into. A fresh store with the profile's checkpoint accepts a backup whose anchor matches; a store that already has a tip accepts only the part above it, so restoring a backup twice is a no-op rather than a corruption. ## the page The page owns the files, because a worker cannot write to disk: - **Save a backup** asks for a directory (File System Access API, or one download per chunk where that is unavailable), then pulls chunks from each chain's worker, computes each chunk's SHA-256 while writing it, and finally writes `manifest.json`. - **Restore** takes the directory (or a multi-file selection), reads `manifest.json`, verifies every file against it, and feeds the chunks to the chain worker, which verifies them against the store as it goes. - The page asks to save a backup when a sync finishes or is paused, and always offers the buttons: a user who has to stop a multi-hour sync should be able to keep what it cost. ## what is implemented - `pkg/backup` writes and reads chunks, and its manifest refuses a hole, a wrong file name, a chunk from another range and totals that do not add up. - `backup.ExportHeaders`, `ExportFilterHeaders` and `ExportFilters` read straight from a store; the filter export is bounded by bytes and always makes progress. - `neutrino.ApplyHeaders`, `ApplyFilterHeaders` and `ApplyFilters` verify a run against the stored chain before storing it, with the same checks the network sync makes, and skip heights the store already holds so a restore is resumable and idempotent. - `internal/e2e` proves the round trip: sync, export (in several chunks per kind), parse the manifest, restore into a fresh store, compare every filter byte for byte, run a script match, and refuse an edited filter (both at the digest and at the commitment) and an edited header. - `chaindata` carries the chunks: `MethodBackup` answers one encoded chunk with its kind, first height and count, and `MethodRestore` answers how many records it applied. The byte budget is the caller's, so a page chooses a file size rather than inheriting one. ## what is not implemented yet The wallet worker's proxy for the two methods, and the page's file handling. Those are the next slices; `docs/STATUS.md` records them as gaps until they land.