// Package process turns a fill's staging log into stored blocks. // // It is the second half of the split the plan makes: the download writes bytes // it has not checked to a log nothing serves, and this pass hashes each body // against the claim the downloader made and against the header chain at that // height. Only a body that passes both becomes a stored block. A body that // fails is dropped and its height is written down, and the next fill asks for // it again because a height with no block is a height its walk visits. // // It is the only reader of the staging log. Nothing that serves data reads it, // which is the property the split rests on: unverified bytes are not reachable // from any serving path. package process import ( "errors" "fmt" "os" "path/filepath" "git.smesh.lol/btcd-lite/internal/serve" "git.smesh.lol/btcd-lite/internal/staging" "git.smesh.lol/btcd-lite/internal/store" ) // Config is what a processing pass reads from and writes to. type Config struct { // Chain names the pass in the log lines and selects the header store's // height-to-hash answers. It is not used to file anything: bodies are // filed by hash. Chain string // Staging is the log the download wrote. Staging *staging.Log // Bodies is where a body that passes goes. Bodies *store.BlockStore // Headers is the chain's header store, the authority for height to hash. Headers *store.HeaderStore // Missing, when set, is where a dropped height is recorded: one line per // drop, appended. Missing string // Logf receives diagnostics; nil discards them. Logf func(string, ...any) } // Result is what a pass did. type Result struct { // Kept is the number of bodies now stored. Kept int // Dropped is the number of bodies refused. Dropped int // Failed lists the heights whose bodies were refused, in claim order. Failed []uint32 // Bytes is how much the pass read from the log. Bytes int64 } // ErrNoHeader is a claim at a height the chain does not have. var ErrNoHeader = errors.New("process: the header chain has no such height") // Run walks the log, checks every body against its claim and its header, files // the ones that pass and drains the log. // // A failed body does not stop the pass: one bad answer from one peer is not a // reason to throw away the rest of a fill. The pass is idempotent per body, so // a rerun after a crash stores what was already stored without complaint. func Run(cfg Config) (Result, error) { if cfg.Staging == nil || cfg.Bodies == nil || cfg.Headers == nil { return Result{}, errors.New("process: staging, bodies and headers are required") } if cfg.Logf == nil { cfg.Logf = func(string, ...any) {} } var res Result var missing []byte err := cfg.Staging.Walk(func(c staging.Claim, body []byte) error { res.Bytes += int64(len(body)) reason := cfg.check(c, body) if reason != "" { res.Dropped++ res.Failed = append(res.Failed, c.Height) cfg.Logf("process %s: height %d dropped: %s", cfg.Chain, c.Height, reason) if cfg.Missing != "" { missing = append(missing, fmt.Sprintf("%s %d %s %s\n", cfg.Chain, c.Height, c.Hash, reason)...) } return nil } if err := cfg.Bodies.PutBlock(c.Hash, body); err != nil { if errors.Is(err, store.ErrBlockExists) { res.Kept++ return nil } return fmt.Errorf("process: store height %d: %w", c.Height, err) } res.Kept++ return nil }) if err != nil { return res, err } if len(missing) > 0 && cfg.Missing != "" { if err := os.MkdirAll(filepath.Dir(cfg.Missing), 0o755); err != nil { return res, err } f, err := os.OpenFile(cfg.Missing, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) if err != nil { return res, fmt.Errorf("process: %w", err) } if _, err := f.Write(missing); err != nil { f.Close() return res, fmt.Errorf("process: %w", err) } if err := f.Close(); err != nil { return res, fmt.Errorf("process: %w", err) } } if err := cfg.Staging.Drain(); err != nil { return res, err } cfg.Logf("process %s: %d bodies kept, %d dropped, %d bytes read", cfg.Chain, res.Kept, res.Dropped, res.Bytes) return res, nil } // check returns why a body cannot be stored, or the empty string. func (cfg Config) check(c staging.Claim, body []byte) string { got := serve.HeaderHash(body) if got != c.Hash { return fmt.Sprintf("the body hashes to %s, the claim %s", got, c.Hash) } hdr, ok := cfg.Headers.Header(c.Height) if !ok { return ErrNoHeader.Error() } if got := hdr.BlockHash(); got != c.Hash { return fmt.Sprintf("height %d is %s on the chain, the claim %s", c.Height, got, c.Hash) } return "" } // MissingName is the file a dropped height is recorded in, under an instance's // blocks directory. func MissingName(data, chainName string) string { return filepath.Join(data, "blocks", "missing", chainName+".log") }