package serve import ( "context" "fmt" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // HeaderSource returns neutrino's header source for one chain of this view. // // One call answers one window. A height the chain does not hold ends the // answer: that is how a source says "nothing more from me", and the client // goes to its peer for the rest. Nothing is trusted because it came from here: // every header a source supplies goes through the same proof-of-work and parent // checks as a peer's. // // The headers come from the chain's header store and not from the bodies: a // client syncing headers wants the chain this instance verified, which exists // before the bodies do. func (v *View) HeaderSource(chainName string) func(context.Context, uint32, uint32) ([]btcwire.BlockHeader, error) { return func(ctx context.Context, from, to uint32) ([]btcwire.BlockHeader, error) { headers, ok := v.Headers[chainName] if !ok { return nil, fmt.Errorf("serve: this instance holds no chain %q", chainName) } var out []btcwire.BlockHeader for h := from; h <= to; h++ { hdr, ok := headers.Header(h) if !ok { break } out = append(out, hdr) } return out, nil } } // BlockSource returns neutrino's block source over this view: a body by hash. // // The body store is keyed by hash, so the answer needs no chain and no height: // the hash is the name. A body the store does not hold is not answered and the // client falls back to its peer. Nothing here is trusted: the client checks the // body against the header it already accepted, so a wrong body costs a refused // block and not a wrong chain. func (v *View) BlockSource() func(context.Context, chainhash.Hash) ([]byte, error) { return func(ctx context.Context, hash chainhash.Hash) ([]byte, error) { if v.Bodies == nil { return nil, fmt.Errorf("serve: this view holds no blocks") } raw, err := v.Bodies.GetBlock(hash) if err != nil { return nil, fmt.Errorf("serve: %w", err) } return raw, nil } }