main.go raw

   1  // Command filterprobe dials peers and reports which of them serve BIP157
   2  // filters, which is NODE_COMPACT_FILTERS in their version message.
   3  //
   4  // It is a diagnostic, not part of the product: the number it prints is how many
   5  // of a seed list will answer getcfilters, which is the question a deployment
   6  // asks when it decides whether it can sync filters instead of building them.
   7  package main
   8  
   9  import (
  10  	"bufio"
  11  	"fmt"
  12  	"net"
  13  	"os"
  14  	"sort"
  15  	"strings"
  16  	"sync"
  17  	"time"
  18  
  19  	btcdwire "github.com/btcsuite/btcd/wire/v2"
  20  
  21  	"git.smesh.lol/btcd-lite/internal/chains"
  22  	"git.smesh.lol/btcd-lite/internal/v2peer"
  23  )
  24  
  25  // compactFilters is NODE_COMPACT_FILTERS.
  26  const compactFilters uint64 = 1 << 6
  27  
  28  type result struct {
  29  	addr     string
  30  	agent    string
  31  	services uint64
  32  	height   int32
  33  	err      error
  34  }
  35  
  36  func main() {
  37  	var addrs []string
  38  	sc := bufio.NewScanner(os.Stdin)
  39  	for sc.Scan() {
  40  		a := strings.TrimSpace(sc.Text())
  41  		if a != "" {
  42  			addrs = append(addrs, a)
  43  		}
  44  	}
  45  	addrs = append(addrs, os.Args[1:]...)
  46  	if len(addrs) == 0 {
  47  		fmt.Fprintln(os.Stderr, "filterprobe: no addresses on stdin or argv")
  48  		os.Exit(2)
  49  	}
  50  	ci, err := chains.ByName("btc")
  51  	if err != nil {
  52  		fmt.Fprintln(os.Stderr, err)
  53  		os.Exit(2)
  54  	}
  55  	results := make([]result, len(addrs))
  56  	var wg sync.WaitGroup
  57  	sem := make(chan struct{}, 24)
  58  	for i, addr := range addrs {
  59  		wg.Add(1)
  60  		go func(i int, addr string) {
  61  			defer wg.Done()
  62  			sem <- struct{}{}
  63  			defer func() { <-sem }()
  64  			results[i] = probe(addr, ci.WireMagic())
  65  		}(i, addr)
  66  	}
  67  	wg.Wait()
  68  
  69  	var ok, withFilters int
  70  	agents := map[string]int{}
  71  	for _, r := range results {
  72  		if r.err != nil {
  73  			continue
  74  		}
  75  		ok++
  76  		if r.services&compactFilters != 0 {
  77  			withFilters++
  78  		}
  79  		agents[r.agent]++
  80  	}
  81  	fmt.Printf("reached %d of %d; NODE_COMPACT_FILTERS on %d (%.0f%% of those reached)\n",
  82  		ok, len(results), withFilters, 100*float64(withFilters)/float64(max(ok, 1)))
  83  	names := make([]string, 0, len(agents))
  84  	for a := range agents {
  85  		names = append(names, a)
  86  	}
  87  	sort.Slice(names, func(i, j int) bool { return agents[names[i]] > agents[names[j]] })
  88  	for _, a := range names[:min(8, len(names))] {
  89  		fmt.Printf("  %4d  %s\n", agents[a], a)
  90  	}
  91  }
  92  
  93  func probe(addr string, magic btcdwire.BitcoinNet) result {
  94  	nc, err := net.DialTimeout("tcp", addr, 5*time.Second)
  95  	if err != nil {
  96  		return result{addr: addr, err: err}
  97  	}
  98  	defer nc.Close()
  99  	p, err := v2peer.Dial(v2peer.Config{
 100  		Conn: nc, Net: magic, UserAgent: "/filterprobe:0.1/",
 101  		V2: false, Timeout: 8 * time.Second, Remote: addr,
 102  	})
 103  	if err != nil {
 104  		return result{addr: addr, err: err}
 105  	}
 106  	defer p.Close()
 107  	return result{addr: addr, agent: p.Agent(), services: p.Services(), height: p.LastBlock()}
 108  }
 109