chains.go raw

   1  package utxobuild
   2  
   3  import (
   4  	"fmt"
   5  )
   6  
   7  // ChainInput is one chain to build over.
   8  type ChainInput struct {
   9  	// Name is the chain, and the name its snapshot directory is derived from.
  10  	Name string
  11  	// Tip is the chain's last height.
  12  	Tip uint32
  13  	// Blocks supplies this chain's bodies.
  14  	Blocks BlockSource
  15  }
  16  
  17  // ChainsConfig is a shared-prefix build over several chains.
  18  type ChainsConfig struct {
  19  	// Dir is the dataset's root: <Dir>/canonical holds the shared snapshots,
  20  	// <Dir>/chain-<name> a chain's own, and <Dir>/diff/{shared,<name>} the
  21  	// diffs.
  22  	Dir string
  23  	// Interval is the snapshot cadence, the chain's retarget interval.
  24  	Interval uint32
  25  	// SharedLast is the last height whose block every chain produces.
  26  	SharedLast uint32
  27  	// Chains are the chains to cover.
  28  	Chains []ChainInput
  29  	// Logf receives progress; nil discards it.
  30  	Logf func(string, ...any)
  31  }
  32  
  33  // BuildChains walks the shared prefix once and each chain past it.
  34  //
  35  // One diff per block and one snapshot per retarget height. Filters are built by
  36  // internal/filterbuild from the same bodies, because they need the scripts of
  37  // spent outputs and this walk holds only entries. The prefix is built against
  38  // the first chain's bodies, because that is what
  39  // shared means: the same block, at the same height, on every chain that carries
  40  // it. Each chain then continues from the set the prefix walk ended with, so no
  41  // second walk reconstructs the prefix and no second copy of it is stored.
  42  //
  43  // The heights each walk covers are blocks: a snapshot at H is the set after
  44  // block H, and a diff is the step from H-1 to H.
  45  func BuildChains(cfg ChainsConfig) (map[string]Result, error) {
  46  	if len(cfg.Chains) == 0 {
  47  		return nil, fmt.Errorf("utxobuild: no chains")
  48  	}
  49  	if cfg.Interval == 0 {
  50  		return nil, fmt.Errorf("utxobuild: the interval is required")
  51  	}
  52  	sharedTo := cfg.Chains[0].Tip + 1
  53  	for _, c := range cfg.Chains[1:] {
  54  		if t := c.Tip + 1; t < sharedTo {
  55  			sharedTo = t
  56  		}
  57  	}
  58  	if limit := cfg.SharedLast + 1; sharedTo > limit {
  59  		sharedTo = limit
  60  	}
  61  	out := make(map[string]Result, len(cfg.Chains))
  62  	shared, err := BuildStream(Config{
  63  		Dir:      cfg.Dir + "/canonical",
  64  		DiffDir:  cfg.Dir + "/diff/shared",
  65  		Chain:    "shared",
  66  		Interval: cfg.Interval,
  67  		From:     0,
  68  		To:       sharedTo,
  69  		Blocks:   cfg.Chains[0].Blocks,
  70  		Logf:     cfg.Logf,
  71  	})
  72  	if err != nil {
  73  		return nil, fmt.Errorf("shared prefix: %w", err)
  74  	}
  75  	for _, c := range cfg.Chains {
  76  		if c.Tip+1 <= sharedTo {
  77  			out[c.Name] = shared
  78  			continue
  79  		}
  80  		// The chain continues from the last snapshot at or below the fork, which
  81  		// is the shared walk's own snapshot, and re-applies the few blocks between
  82  		// that height and the fork without writing their diffs: the shared walk
  83  		// already wrote those, once.
  84  		from := sharedTo - sharedTo%cfg.Interval
  85  		if from == 0 && sharedTo > 0 {
  86  			from = 0
  87  		}
  88  		res, err := BuildStream(Config{
  89  			Dir:      cfg.Dir + "/chain-" + c.Name,
  90  			BaseDir:  cfg.Dir + "/canonical",
  91  			DiffDir:  cfg.Dir + "/diff/" + c.Name,
  92  			Chain:    c.Name,
  93  			Interval: cfg.Interval,
  94  			From:     from,
  95  			To:       c.Tip + 1,
  96  			DiffFrom: sharedTo,
  97  			Blocks:   c.Blocks,
  98  			Logf:     cfg.Logf,
  99  		})
 100  		if err != nil {
 101  			return nil, fmt.Errorf("%s: %w", c.Name, err)
 102  		}
 103  		out[c.Name] = res
 104  	}
 105  	return out, nil
 106  }
 107