// Package httpblock is neutrino.BlockSource over a sidecar's block service. // // Blocks are the one piece of chain data a relay cannot hold, so the sidecar keeps the recent // ones on disk and serves them over HTTP. This is the client end of that: one GET by block // hash, with a 404 meaning "not in that cache, ask the tunnel" rather than a failure. // // Nothing here decides whether the block is right. neutrino hands the bytes to the caller, // which hashes them against the header it already accepted, so a sidecar that served the wrong // block would be refused exactly as a peer's wrong block is. package httpblock import ( "context" "errors" "fmt" "io" "net/http" "net/url" "strings" "time" "git.smesh.lol/nostrino/pkg/crypto/chainhash" "git.smesh.lol/nostrino/pkg/neutrino" ) // MaxBlockBytes bounds one response. It is the P2P block ceiling with room to spare: a block // larger than this is not a block, and reading it would be a way to make the client spend // memory on a stranger's answer. const MaxBlockBytes = 32 << 20 // Config is where the service is and which chain it serves. type Config struct { // BaseURL is the service's root, for example http://127.0.0.1:3350. BaseURL string // Chain is the profile name the service is asked for. Chain string // Timeout bounds one request. Zero means thirty seconds. Timeout time.Duration // Logf receives one line per miss; nil discards them. Logf func(string, ...any) } // New returns the source. func New(cfg Config) (neutrino.BlockSource, error) { parsed, err := url.Parse(strings.TrimSpace(cfg.BaseURL)) if err != nil { return nil, fmt.Errorf("httpblock: %w", err) } if parsed.Scheme != "http" && parsed.Scheme != "https" { return nil, errors.New("httpblock: the base URL must be http or https") } if parsed.Host == "" { return nil, errors.New("httpblock: the base URL has no host") } if strings.TrimSpace(cfg.Chain) == "" { return nil, errors.New("httpblock: no chain") } if cfg.Timeout <= 0 { cfg.Timeout = 30 * time.Second } if cfg.Logf == nil { cfg.Logf = func(string, ...any) {} } client := &http.Client{Timeout: cfg.Timeout} base := strings.TrimSuffix(parsed.String(), "/") return func(ctx context.Context, hash chainhash.Hash) ([]byte, error) { address := base + "/block/" + cfg.Chain + "/" + hash.String() request, err := http.NewRequestWithContext(ctx, http.MethodGet, address, nil) if err != nil { return nil, fmt.Errorf("httpblock: %w", err) } response, err := client.Do(request) if err != nil { return nil, fmt.Errorf("httpblock: %w", err) } defer func() { _ = response.Body.Close() }() switch response.StatusCode { case http.StatusOK: case http.StatusNotFound: // The cache does not hold this one, which is what the tunnel is for. cfg.Logf("httpblock: %s is not in the block service", hash.String()) return nil, nil default: return nil, fmt.Errorf("httpblock: %s answered %s", address, response.Status) } raw, err := io.ReadAll(io.LimitReader(response.Body, MaxBlockBytes+1)) if err != nil { return nil, fmt.Errorf("httpblock: %w", err) } if len(raw) > MaxBlockBytes { return nil, fmt.Errorf("httpblock: %s answered more than %d bytes", address, MaxBlockBytes) } if len(raw) == 0 { return nil, nil } return raw, nil }, nil }