package backup import ( "fmt" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // Store is the part of a light client's store an export reads. Declaring it here rather than // importing the client keeps the format independent of the thing that produces it. type Store interface { Header(height uint32) (btcwire.BlockHeader, bool) FilterHeader(height uint32) (chainhash.Hash, bool) FilterHash(height uint32) (chainhash.Hash, bool) Filter(height uint32) ([]byte, bool) } // ExportHeaders encodes up to max headers starting at from. // // A missing header ends the run rather than failing: a store that stopped early is a store whose // export ends where its verified data does, and the manifest records that tip. func ExportHeaders(store Store, from, max uint32) (Chunk, error) { chunk := Chunk{Kind: Headers, From: from} for i := uint32(0); i < max; i++ { header, ok := store.Header(from + i) if !ok { break } raw := header.Serialize() chunk.Payload = append(chunk.Payload, raw[:]...) chunk.Count++ } if chunk.Count == 0 { return Chunk{}, fmt.Errorf("backup: no headers at or above %d", from) } return chunk, nil } // ExportFilterHeaders encodes up to max filter hash and filter header pairs starting at from. func ExportFilterHeaders(store Store, from, max uint32) (Chunk, error) { chunk := Chunk{Kind: FilterHeaders, From: from} for i := uint32(0); i < max; i++ { height := from + i filterHash, ok := store.FilterHash(height) if !ok { break } filterHeader, ok := store.FilterHeader(height) if !ok { return Chunk{}, fmt.Errorf("backup: filter header %d is missing its hash", height) } chunk.Payload = append(chunk.Payload, filterHash[:]...) chunk.Payload = append(chunk.Payload, filterHeader[:]...) chunk.Count++ } if chunk.Count == 0 { return Chunk{}, fmt.Errorf("backup: no filter headers at or above %d", from) } return chunk, nil } // ExportFilters encodes filters starting at from, stopping before the payload would exceed // maxBytes. One filter is always included, however large, so a chunk always makes progress. // // max bounds the count; zero means the byte budget alone bounds the chunk, which is what a // caller that asked for "a chunk of filters" means. func ExportFilters(store Store, from, max uint32, maxBytes int) (Chunk, error) { chunk := Chunk{Kind: Filters, From: from} limit := max if limit == 0 { limit = ^uint32(0) } for i := uint32(0); i < limit; i++ { height := from + i filter, ok := store.Filter(height) if !ok { break } header, ok := store.Header(height) if !ok { return Chunk{}, fmt.Errorf("backup: filter %d has no header", height) } filterHash, ok := store.FilterHash(height) if !ok { return Chunk{}, fmt.Errorf("backup: filter %d has no committed hash", height) } record := FilterRecord{ Height: height, BlockHash: header.BlockHash(), FilterHash: filterHash, Filter: filter, } recordBytes := filterRecordFixed + len(filter) if chunk.Count > 0 && maxBytes > 0 && len(chunk.Payload)+recordBytes > maxBytes { break } payload, err := AppendFilters(chunk.Payload, []FilterRecord{record}) if err != nil { return Chunk{}, err } chunk.Payload = payload chunk.Count++ } if chunk.Count == 0 { return Chunk{}, fmt.Errorf("backup: no filters at or above %d", from) } return chunk, nil }