httpblock.go raw

   1  // Package httpblock is neutrino.BlockSource over a sidecar's block service.
   2  //
   3  // Blocks are the one piece of chain data a relay cannot hold, so the sidecar keeps the recent
   4  // ones on disk and serves them over HTTP. This is the client end of that: one GET by block
   5  // hash, with a 404 meaning "not in that cache, ask the tunnel" rather than a failure.
   6  //
   7  // Nothing here decides whether the block is right. neutrino hands the bytes to the caller,
   8  // which hashes them against the header it already accepted, so a sidecar that served the wrong
   9  // block would be refused exactly as a peer's wrong block is.
  10  package httpblock
  11  
  12  import (
  13  	"context"
  14  	"errors"
  15  	"fmt"
  16  	"io"
  17  	"net/http"
  18  	"net/url"
  19  	"strings"
  20  	"time"
  21  
  22  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
  23  	"git.smesh.lol/nostrino/pkg/neutrino"
  24  )
  25  
  26  // MaxBlockBytes bounds one response. It is the P2P block ceiling with room to spare: a block
  27  // larger than this is not a block, and reading it would be a way to make the client spend
  28  // memory on a stranger's answer.
  29  const MaxBlockBytes = 32 << 20
  30  
  31  // Config is where the service is and which chain it serves.
  32  type Config struct {
  33  	// BaseURL is the service's root, for example http://127.0.0.1:3350.
  34  	BaseURL string
  35  	// Chain is the profile name the service is asked for.
  36  	Chain string
  37  	// Timeout bounds one request. Zero means thirty seconds.
  38  	Timeout time.Duration
  39  	// Logf receives one line per miss; nil discards them.
  40  	Logf func(string, ...any)
  41  }
  42  
  43  // New returns the source.
  44  func New(cfg Config) (neutrino.BlockSource, error) {
  45  	parsed, err := url.Parse(strings.TrimSpace(cfg.BaseURL))
  46  
  47  	if err != nil {
  48  		return nil, fmt.Errorf("httpblock: %w", err)
  49  	}
  50  
  51  	if parsed.Scheme != "http" && parsed.Scheme != "https" {
  52  		return nil, errors.New("httpblock: the base URL must be http or https")
  53  	}
  54  
  55  	if parsed.Host == "" {
  56  		return nil, errors.New("httpblock: the base URL has no host")
  57  	}
  58  
  59  	if strings.TrimSpace(cfg.Chain) == "" {
  60  		return nil, errors.New("httpblock: no chain")
  61  	}
  62  
  63  	if cfg.Timeout <= 0 {
  64  		cfg.Timeout = 30 * time.Second
  65  	}
  66  
  67  	if cfg.Logf == nil {
  68  		cfg.Logf = func(string, ...any) {}
  69  	}
  70  
  71  	client := &http.Client{Timeout: cfg.Timeout}
  72  	base := strings.TrimSuffix(parsed.String(), "/")
  73  
  74  	return func(ctx context.Context, hash chainhash.Hash) ([]byte, error) {
  75  		address := base + "/block/" + cfg.Chain + "/" + hash.String()
  76  
  77  		request, err := http.NewRequestWithContext(ctx, http.MethodGet, address, nil)
  78  
  79  		if err != nil {
  80  			return nil, fmt.Errorf("httpblock: %w", err)
  81  		}
  82  
  83  		response, err := client.Do(request)
  84  
  85  		if err != nil {
  86  			return nil, fmt.Errorf("httpblock: %w", err)
  87  		}
  88  
  89  		defer func() { _ = response.Body.Close() }()
  90  
  91  		switch response.StatusCode {
  92  		case http.StatusOK:
  93  		case http.StatusNotFound:
  94  			// The cache does not hold this one, which is what the tunnel is for.
  95  			cfg.Logf("httpblock: %s is not in the block service", hash.String())
  96  
  97  			return nil, nil
  98  		default:
  99  			return nil, fmt.Errorf("httpblock: %s answered %s", address, response.Status)
 100  		}
 101  
 102  		raw, err := io.ReadAll(io.LimitReader(response.Body, MaxBlockBytes+1))
 103  
 104  		if err != nil {
 105  			return nil, fmt.Errorf("httpblock: %w", err)
 106  		}
 107  
 108  		if len(raw) > MaxBlockBytes {
 109  			return nil, fmt.Errorf("httpblock: %s answered more than %d bytes", address, MaxBlockBytes)
 110  		}
 111  
 112  		if len(raw) == 0 {
 113  			return nil, nil
 114  		}
 115  
 116  		return raw, nil
 117  	}, nil
 118  }
 119