main.go raw

   1  // Command nostrino-peers finds Bitcoin peers that serve compact block filters.
   2  //
   3  // A direct peer is the way to derive a checkpoint without a local node: the filter header at a
   4  // height is the fold of every compact filter hash below it, and only a peer with the filter index
   5  // will serve that range. Most public peers do not run one, so this scans the DNS seeds and reports
   6  // the ones whose version message advertises the service.
   7  package main
   8  
   9  import (
  10  	"context"
  11  	"flag"
  12  	"fmt"
  13  	"net"
  14  	"os"
  15  	"sort"
  16  	"strings"
  17  	"sync"
  18  	"time"
  19  
  20  	"git.smesh.lol/nostrino/pkg/btcwire"
  21  	"git.smesh.lol/nostrino/pkg/chain"
  22  	"git.smesh.lol/nostrino/pkg/p2p"
  23  )
  24  
  25  // nodeBlake2b is the fork's service bit (NODE_BLAKE2B = 1 << 28 in the node's protocol.h). Only a
  26  // peer that advertises it serves the header chain past the fork.
  27  const nodeBlake2b uint64 = 1 << 28
  28  
  29  // seeds are the mainnet DNS seeds the fork's chainparams inherits.
  30  var seeds = []string{
  31  	"seed.bitcoin.sipa.be",
  32  	"dnsseed.bluematt.me",
  33  	"dnsseed.bitcoin.dashjr-list-of-p2p-nodes.us",
  34  	"seed.bitcoin.haf.ovh",
  35  	"seed.bitcoin.jonasschnelli.ch",
  36  	"seed.bitcoin.sprovoost.nl",
  37  	"dnsseed.emzy.de",
  38  	"seed.bitcoin.wiz.biz",
  39  	"seed.mainnet.achownodes.xyz",
  40  }
  41  
  42  type found struct {
  43  	addr     string
  44  	services uint64
  45  	tip      int32
  46  	user     string
  47  	err      error
  48  }
  49  
  50  func main() {
  51  	port := flag.String("port", "8333", "peer port")
  52  	only := flag.Bool("filters", true, "print only peers that serve compact filters")
  53  	seedList := flag.String("seed", "", "extra seed names, comma separated (x10000009.<seed> asks for fork peers)")
  54  	all := flag.Bool("all", false, "print every reachable peer, with its service bits")
  55  	limit := flag.Int("limit", 400, "how many addresses to try")
  56  	timeout := flag.Duration("timeout", 5*time.Second, "per-peer handshake timeout")
  57  	flag.Parse()
  58  
  59  	profile, err := chain.ByName("btc")
  60  
  61  	if err != nil {
  62  		fmt.Fprintln(os.Stderr, "nostrino-peers:", err)
  63  		os.Exit(1)
  64  	}
  65  
  66  	seen := map[string]bool{}
  67  	var addresses []string
  68  
  69  	allSeeds := seeds
  70  
  71  	if *seedList != "" {
  72  		for _, extra := range strings.Split(*seedList, ",") {
  73  			if extra = strings.TrimSpace(extra); extra != "" {
  74  				allSeeds = append(allSeeds, extra)
  75  			}
  76  		}
  77  	}
  78  
  79  	for _, seed := range allSeeds {
  80  		hosts, err := net.LookupHost(seed)
  81  
  82  		if err != nil {
  83  			continue
  84  		}
  85  
  86  		for _, host := range hosts {
  87  			if net.ParseIP(host).To4() == nil {
  88  				continue
  89  			}
  90  
  91  			addr := net.JoinHostPort(host, *port)
  92  
  93  			if !seen[addr] {
  94  				seen[addr] = true
  95  				addresses = append(addresses, addr)
  96  			}
  97  		}
  98  	}
  99  
 100  	sort.Strings(addresses)
 101  
 102  	if len(addresses) > *limit {
 103  		addresses = addresses[:*limit]
 104  	}
 105  
 106  	results := make([]found, len(addresses))
 107  
 108  	var wg sync.WaitGroup
 109  
 110  	sem := make(chan struct{}, 48)
 111  
 112  	for i, addr := range addresses {
 113  		wg.Add(1)
 114  
 115  		go func(i int, addr string) {
 116  			defer wg.Done()
 117  
 118  			sem <- struct{}{}
 119  			defer func() { <-sem }()
 120  
 121  			results[i] = handshake(addr, profile, *timeout)
 122  		}(i, addr)
 123  	}
 124  
 125  	wg.Wait()
 126  
 127  	reachable := 0
 128  
 129  	for _, r := range results {
 130  		if r.err != nil {
 131  			continue
 132  		}
 133  
 134  		reachable++
 135  
 136  		if !*all && *only && r.services&btcwire.ServiceNodeCompactFilters == 0 && r.services&nodeBlake2b == 0 {
 137  			continue
 138  		}
 139  
 140  		fmt.Printf("%s services=%#x filters=%v blake2b=%v tip=%d user=%q\n",
 141  			r.addr, r.services, r.services&btcwire.ServiceNodeCompactFilters != 0,
 142  			r.services&nodeBlake2b != 0, r.tip, r.user)
 143  	}
 144  
 145  	fmt.Fprintf(os.Stderr, "tried %d, reachable %d\n", len(addresses), reachable)
 146  }
 147  
 148  func handshake(addr string, profile *chain.Profile, timeout time.Duration) found {
 149  	ctx, cancel := context.WithTimeout(context.Background(), timeout)
 150  	defer cancel()
 151  
 152  	var dialer net.Dialer
 153  
 154  	conn, err := dialer.DialContext(ctx, "tcp", addr)
 155  
 156  	if err != nil {
 157  		return found{addr: addr, err: err}
 158  	}
 159  
 160  	defer conn.Close()
 161  
 162  	peer := p2p.New(conn, p2p.Config{
 163  		Magic:       profile.Magic,
 164  		UserAgent:   "/nostrino-peers:0.2/",
 165  		ReadTimeout: timeout,
 166  	})
 167  
 168  	version, err := peer.Handshake(ctx)
 169  
 170  	if err != nil {
 171  		return found{addr: addr, err: err}
 172  	}
 173  
 174  	return found{
 175  		addr:     addr,
 176  		services: version.Services,
 177  		tip:      version.LastBlock,
 178  		user:     version.UserAgent,
 179  	}
 180  }
 181