// Command nostrino-peers finds Bitcoin peers that serve compact block filters. // // A direct peer is the way to derive a checkpoint without a local node: the filter header at a // height is the fold of every compact filter hash below it, and only a peer with the filter index // will serve that range. Most public peers do not run one, so this scans the DNS seeds and reports // the ones whose version message advertises the service. package main import ( "context" "flag" "fmt" "net" "os" "sort" "strings" "sync" "time" "git.smesh.lol/nostrino/pkg/btcwire" "git.smesh.lol/nostrino/pkg/chain" "git.smesh.lol/nostrino/pkg/p2p" ) // nodeBlake2b is the fork's service bit (NODE_BLAKE2B = 1 << 28 in the node's protocol.h). Only a // peer that advertises it serves the header chain past the fork. const nodeBlake2b uint64 = 1 << 28 // seeds are the mainnet DNS seeds the fork's chainparams inherits. var seeds = []string{ "seed.bitcoin.sipa.be", "dnsseed.bluematt.me", "dnsseed.bitcoin.dashjr-list-of-p2p-nodes.us", "seed.bitcoin.haf.ovh", "seed.bitcoin.jonasschnelli.ch", "seed.bitcoin.sprovoost.nl", "dnsseed.emzy.de", "seed.bitcoin.wiz.biz", "seed.mainnet.achownodes.xyz", } type found struct { addr string services uint64 tip int32 user string err error } func main() { port := flag.String("port", "8333", "peer port") only := flag.Bool("filters", true, "print only peers that serve compact filters") seedList := flag.String("seed", "", "extra seed names, comma separated (x10000009. asks for fork peers)") all := flag.Bool("all", false, "print every reachable peer, with its service bits") limit := flag.Int("limit", 400, "how many addresses to try") timeout := flag.Duration("timeout", 5*time.Second, "per-peer handshake timeout") flag.Parse() profile, err := chain.ByName("btc") if err != nil { fmt.Fprintln(os.Stderr, "nostrino-peers:", err) os.Exit(1) } seen := map[string]bool{} var addresses []string allSeeds := seeds if *seedList != "" { for _, extra := range strings.Split(*seedList, ",") { if extra = strings.TrimSpace(extra); extra != "" { allSeeds = append(allSeeds, extra) } } } for _, seed := range allSeeds { hosts, err := net.LookupHost(seed) if err != nil { continue } for _, host := range hosts { if net.ParseIP(host).To4() == nil { continue } addr := net.JoinHostPort(host, *port) if !seen[addr] { seen[addr] = true addresses = append(addresses, addr) } } } sort.Strings(addresses) if len(addresses) > *limit { addresses = addresses[:*limit] } results := make([]found, len(addresses)) var wg sync.WaitGroup sem := make(chan struct{}, 48) for i, addr := range addresses { wg.Add(1) go func(i int, addr string) { defer wg.Done() sem <- struct{}{} defer func() { <-sem }() results[i] = handshake(addr, profile, *timeout) }(i, addr) } wg.Wait() reachable := 0 for _, r := range results { if r.err != nil { continue } reachable++ if !*all && *only && r.services&btcwire.ServiceNodeCompactFilters == 0 && r.services&nodeBlake2b == 0 { continue } fmt.Printf("%s services=%#x filters=%v blake2b=%v tip=%d user=%q\n", r.addr, r.services, r.services&btcwire.ServiceNodeCompactFilters != 0, r.services&nodeBlake2b != 0, r.tip, r.user) } fmt.Fprintf(os.Stderr, "tried %d, reachable %d\n", len(addresses), reachable) } func handshake(addr string, profile *chain.Profile, timeout time.Duration) found { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() var dialer net.Dialer conn, err := dialer.DialContext(ctx, "tcp", addr) if err != nil { return found{addr: addr, err: err} } defer conn.Close() peer := p2p.New(conn, p2p.Config{ Magic: profile.Magic, UserAgent: "/nostrino-peers:0.2/", ReadTimeout: timeout, }) version, err := peer.Handshake(ctx) if err != nil { return found{addr: addr, err: err} } return found{ addr: addr, services: version.Services, tip: version.LastBlock, user: version.UserAgent, } }