export.go raw

   1  package backup
   2  
   3  import (
   4  	"fmt"
   5  
   6  	"git.smesh.lol/nostrino/pkg/btcwire"
   7  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   8  )
   9  
  10  // Store is the part of a light client's store an export reads. Declaring it here rather than
  11  // importing the client keeps the format independent of the thing that produces it.
  12  type Store interface {
  13  	Header(height uint32) (btcwire.BlockHeader, bool)
  14  	FilterHeader(height uint32) (chainhash.Hash, bool)
  15  	FilterHash(height uint32) (chainhash.Hash, bool)
  16  	Filter(height uint32) ([]byte, bool)
  17  }
  18  
  19  // ExportHeaders encodes up to max headers starting at from.
  20  //
  21  // A missing header ends the run rather than failing: a store that stopped early is a store whose
  22  // export ends where its verified data does, and the manifest records that tip.
  23  func ExportHeaders(store Store, from, max uint32) (Chunk, error) {
  24  	chunk := Chunk{Kind: Headers, From: from}
  25  
  26  	for i := uint32(0); i < max; i++ {
  27  		header, ok := store.Header(from + i)
  28  
  29  		if !ok {
  30  			break
  31  		}
  32  
  33  		raw := header.Serialize()
  34  
  35  		chunk.Payload = append(chunk.Payload, raw[:]...)
  36  		chunk.Count++
  37  	}
  38  
  39  	if chunk.Count == 0 {
  40  		return Chunk{}, fmt.Errorf("backup: no headers at or above %d", from)
  41  	}
  42  
  43  	return chunk, nil
  44  }
  45  
  46  // ExportFilterHeaders encodes up to max filter hash and filter header pairs starting at from.
  47  func ExportFilterHeaders(store Store, from, max uint32) (Chunk, error) {
  48  	chunk := Chunk{Kind: FilterHeaders, From: from}
  49  
  50  	for i := uint32(0); i < max; i++ {
  51  		height := from + i
  52  
  53  		filterHash, ok := store.FilterHash(height)
  54  
  55  		if !ok {
  56  			break
  57  		}
  58  
  59  		filterHeader, ok := store.FilterHeader(height)
  60  
  61  		if !ok {
  62  			return Chunk{}, fmt.Errorf("backup: filter header %d is missing its hash", height)
  63  		}
  64  
  65  		chunk.Payload = append(chunk.Payload, filterHash[:]...)
  66  		chunk.Payload = append(chunk.Payload, filterHeader[:]...)
  67  		chunk.Count++
  68  	}
  69  
  70  	if chunk.Count == 0 {
  71  		return Chunk{}, fmt.Errorf("backup: no filter headers at or above %d", from)
  72  	}
  73  
  74  	return chunk, nil
  75  }
  76  
  77  // ExportFilters encodes filters starting at from, stopping before the payload would exceed
  78  // maxBytes. One filter is always included, however large, so a chunk always makes progress.
  79  //
  80  // max bounds the count; zero means the byte budget alone bounds the chunk, which is what a
  81  // caller that asked for "a chunk of filters" means.
  82  func ExportFilters(store Store, from, max uint32, maxBytes int) (Chunk, error) {
  83  	chunk := Chunk{Kind: Filters, From: from}
  84  
  85  	limit := max
  86  
  87  	if limit == 0 {
  88  		limit = ^uint32(0)
  89  	}
  90  
  91  	for i := uint32(0); i < limit; i++ {
  92  		height := from + i
  93  
  94  		filter, ok := store.Filter(height)
  95  
  96  		if !ok {
  97  			break
  98  		}
  99  
 100  		header, ok := store.Header(height)
 101  
 102  		if !ok {
 103  			return Chunk{}, fmt.Errorf("backup: filter %d has no header", height)
 104  		}
 105  
 106  		filterHash, ok := store.FilterHash(height)
 107  
 108  		if !ok {
 109  			return Chunk{}, fmt.Errorf("backup: filter %d has no committed hash", height)
 110  		}
 111  
 112  		record := FilterRecord{
 113  			Height:     height,
 114  			BlockHash:  header.BlockHash(),
 115  			FilterHash: filterHash,
 116  			Filter:     filter,
 117  		}
 118  
 119  		recordBytes := filterRecordFixed + len(filter)
 120  
 121  		if chunk.Count > 0 && maxBytes > 0 && len(chunk.Payload)+recordBytes > maxBytes {
 122  			break
 123  		}
 124  
 125  		payload, err := AppendFilters(chunk.Payload, []FilterRecord{record})
 126  
 127  		if err != nil {
 128  			return Chunk{}, err
 129  		}
 130  
 131  		chunk.Payload = payload
 132  		chunk.Count++
 133  	}
 134  
 135  	if chunk.Count == 0 {
 136  		return Chunk{}, fmt.Errorf("backup: no filters at or above %d", from)
 137  	}
 138  
 139  	return chunk, nil
 140  }
 141