dial.go raw

   1  // Package litedial is the instance's one dialler, so the daemon, the gates and
   2  // the handshake tests all build the same connection.
   3  //
   4  // It exists because a test and the daemon disagreed about whether the same node
   5  // completes a BIP324 handshake: the test dialed with net.Dial and completed, the
   6  // daemon dialed with a context and did not. The connection is now built in one
   7  // place, so a disagreement cannot be a difference in the connection.
   8  package litedial
   9  
  10  import (
  11  	"context"
  12  	"fmt"
  13  	"net"
  14  	"time"
  15  
  16  	btcdwire "github.com/btcsuite/btcd/wire/v2"
  17  
  18  	"git.smesh.lol/btcd-lite/internal/v2peer"
  19  )
  20  
  21  // Connect dials addr and negotiates with userAgent, in one call. It tries
  22  // BIP324 first and falls back to v1 on a second connection, because a public
  23  // node that does not speak v2 answers the ellswift with nothing and closes,
  24  // and a fill that only speaks v2 cannot use most of the network.
  25  //
  26  // The dial takes ctx for cancellation and has its own deadline, and the
  27  // resulting socket carries no deadline into the handshake: v2peer sets its own
  28  // and a leftover one is a deadline nobody meant.
  29  func Connect(ctx context.Context, addr, userAgent string, magic btcdwire.BitcoinNet, timeout time.Duration, useV2 bool, logf func(string, ...any)) (*v2peer.Peer, error) {
  30  	if timeout <= 0 {
  31  		timeout = 2 * time.Minute
  32  	}
  33  	dial := func(ctx context.Context, a string) (net.Conn, error) {
  34  		dialCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
  35  		defer cancel()
  36  		start := time.Now()
  37  		nc, err := (&net.Dialer{Timeout: 30 * time.Second}).DialContext(dialCtx, "tcp", a)
  38  		if err != nil {
  39  			return nil, fmt.Errorf("dial %s after %s: %w", a, time.Since(start).Round(time.Millisecond), err)
  40  		}
  41  		if logf != nil {
  42  			logf("dial: tcp to %s in %s", a, time.Since(start).Round(time.Millisecond))
  43  		}
  44  		return nc, nil
  45  	}
  46  	cfg := v2peer.Config{
  47  		Net: magic, UserAgent: userAgent,
  48  		// NODE_COMPACT_FILTERS is not advertised: it says a peer can *serve*
  49  		// filters, which an instance that asks for them cannot. A node decides
  50  		// whether to answer from its own services, not from this field.
  51  		Services: btcdwire.SFNodeNetwork,
  52  		Timeout:  timeout,
  53  		Remote:   addr, Logf: logf,
  54  	}
  55  	if !useV2 {
  56  		// v1 only: a plaintext connection is what every node accepts, and the
  57  		// BIP324 path closes connections part way through a sync against some
  58  		// real peers. See docs/STATUS.md.
  59  		nc, err := dial(ctx, addr)
  60  		if err != nil {
  61  			return nil, err
  62  		}
  63  		cfg.Conn = nc
  64  		p, err := v2peer.Dial(cfg)
  65  		if err != nil {
  66  			_ = nc.Close()
  67  			return nil, err
  68  		}
  69  		return p, nil
  70  	}
  71  	return v2peer.DialWithFallback(ctx, dial, addr, cfg)
  72  }
  73