conn.go raw

   1  package filler
   2  
   3  import (
   4  	"context"
   5  	"net"
   6  	"time"
   7  
   8  	"github.com/btcsuite/btcd/chainhash/v2"
   9  	"github.com/btcsuite/btcd/wire/v2"
  10  
  11  	"git.smesh.lol/btcd-lite/internal/v2peer"
  12  )
  13  
  14  // conn is one peer connection owned by a lane: btcd's handshake and btcd's
  15  // BIP324 transport through internal/v2peer, and one block request.
  16  //
  17  // Nothing here speaks the peer protocol itself. The frame, the encoding and
  18  // the transport are btcd's; a lane makes a connection, asks for blocks and
  19  // closes it.
  20  type conn struct {
  21  	peer *v2peer.Peer
  22  	// block is the last block answered for, in its wire form.
  23  	block []byte
  24  }
  25  
  26  // connFrom wraps a connection a caller already has open, which is how an
  27  // instance that synced headers over it keeps it for the bodies rather than
  28  // asking the node for a second connection.
  29  func connFrom(p *v2peer.Peer) *conn { return &conn{peer: p} }
  30  
  31  // connect dials a peer and completes the handshake.
  32  func connect(ctx context.Context, cfg dialConfig, addr string, lastBlock int32, timeout time.Duration) (*conn, error) {
  33  	dial := func(ctx context.Context, a string) (net.Conn, error) {
  34  		if cfg.dial != nil {
  35  			return cfg.dial(ctx, a)
  36  		}
  37  		var d net.Dialer
  38  		return d.DialContext(ctx, "tcp", a)
  39  	}
  40  	if !cfg.v2 {
  41  		nc, err := dial(ctx, addr)
  42  		if err != nil {
  43  			return nil, err
  44  		}
  45  		p, err := v2peer.Dial(v2peer.Config{
  46  			Conn: nc, Net: cfg.net, UserAgent: cfg.ua, Services: cfg.services,
  47  			LastBlock: lastBlock, Timeout: timeout, Remote: addr, Logf: cfg.logf,
  48  		})
  49  		if err != nil {
  50  			_ = nc.Close()
  51  			return nil, err
  52  		}
  53  		return &conn{peer: p}, nil
  54  	}
  55  	p, err := v2peer.DialWithFallback(ctx, dial, addr, v2peer.Config{
  56  		Net:       cfg.net,
  57  		UserAgent: cfg.ua,
  58  		Services:  cfg.services,
  59  		LastBlock: lastBlock,
  60  		Timeout:   timeout,
  61  		Remote:    addr,
  62  		Logf:      cfg.logf,
  63  	})
  64  	if err != nil {
  65  		return nil, err
  66  	}
  67  	return &conn{peer: p}, nil
  68  }
  69  
  70  // request asks for one block and keeps the bytes.
  71  func (c *conn) request(hash chainhash.Hash, timeout time.Duration) error {
  72  	raw, err := c.peer.BlockFor(hash, timeout)
  73  	if err != nil {
  74  		return err
  75  	}
  76  	c.block = raw
  77  	return nil
  78  }
  79  
  80  func (c *conn) userAgent() string { return c.peer.Agent() }
  81  
  82  func (c *conn) close() error { return c.peer.Close() }
  83  
  84  // dialConfig is what a pool hands a lane to make a connection.
  85  type dialConfig struct {
  86  	net      wire.BitcoinNet
  87  	ua       string
  88  	services wire.ServiceFlag
  89  	dial     func(ctx context.Context, addr string) (net.Conn, error)
  90  	logf     func(string, ...any)
  91  	v2       bool
  92  }
  93