package utxobuild
import (
"fmt"
)
// ChainInput is one chain to build over.
type ChainInput struct {
// Name is the chain, and the name its snapshot directory is derived from.
Name string
// Tip is the chain's last height.
Tip uint32
// Blocks supplies this chain's bodies.
Blocks BlockSource
}
// ChainsConfig is a shared-prefix build over several chains.
type ChainsConfig struct {
// Dir is the dataset's root:
/canonical holds the shared snapshots,
// /chain- a chain's own, and /diff/{shared,} the
// diffs.
Dir string
// Interval is the snapshot cadence, the chain's retarget interval.
Interval uint32
// SharedLast is the last height whose block every chain produces.
SharedLast uint32
// Chains are the chains to cover.
Chains []ChainInput
// Logf receives progress; nil discards it.
Logf func(string, ...any)
}
// BuildChains walks the shared prefix once and each chain past it.
//
// One diff per block and one snapshot per retarget height. Filters are built by
// internal/filterbuild from the same bodies, because they need the scripts of
// spent outputs and this walk holds only entries. The prefix is built against
// the first chain's bodies, because that is what
// shared means: the same block, at the same height, on every chain that carries
// it. Each chain then continues from the set the prefix walk ended with, so no
// second walk reconstructs the prefix and no second copy of it is stored.
//
// The heights each walk covers are blocks: a snapshot at H is the set after
// block H, and a diff is the step from H-1 to H.
func BuildChains(cfg ChainsConfig) (map[string]Result, error) {
if len(cfg.Chains) == 0 {
return nil, fmt.Errorf("utxobuild: no chains")
}
if cfg.Interval == 0 {
return nil, fmt.Errorf("utxobuild: the interval is required")
}
sharedTo := cfg.Chains[0].Tip + 1
for _, c := range cfg.Chains[1:] {
if t := c.Tip + 1; t < sharedTo {
sharedTo = t
}
}
if limit := cfg.SharedLast + 1; sharedTo > limit {
sharedTo = limit
}
out := make(map[string]Result, len(cfg.Chains))
shared, err := BuildStream(Config{
Dir: cfg.Dir + "/canonical",
DiffDir: cfg.Dir + "/diff/shared",
Chain: "shared",
Interval: cfg.Interval,
From: 0,
To: sharedTo,
Blocks: cfg.Chains[0].Blocks,
Logf: cfg.Logf,
})
if err != nil {
return nil, fmt.Errorf("shared prefix: %w", err)
}
for _, c := range cfg.Chains {
if c.Tip+1 <= sharedTo {
out[c.Name] = shared
continue
}
// The chain continues from the last snapshot at or below the fork, which
// is the shared walk's own snapshot, and re-applies the few blocks between
// that height and the fork without writing their diffs: the shared walk
// already wrote those, once.
from := sharedTo - sharedTo%cfg.Interval
if from == 0 && sharedTo > 0 {
from = 0
}
res, err := BuildStream(Config{
Dir: cfg.Dir + "/chain-" + c.Name,
BaseDir: cfg.Dir + "/canonical",
DiffDir: cfg.Dir + "/diff/" + c.Name,
Chain: c.Name,
Interval: cfg.Interval,
From: from,
To: c.Tip + 1,
DiffFrom: sharedTo,
Blocks: c.Blocks,
Logf: cfg.Logf,
})
if err != nil {
return nil, fmt.Errorf("%s: %w", c.Name, err)
}
out[c.Name] = res
}
return out, nil
}