How to create bootstrap node correctly? always "Searching for other peers..."

Hi, I’ve read the topic about Create own DHT bootstrap node, and I created a bootstrap node via NewDHT function
but, when I run the chat example,it always wait for “Searching for other peers…”
I run two chat peers , I think the first may be wait for search ,but why the second one also wait for search?
both of two chat peers connect my bootstrap node success, but they don’t seem to find each other…
I think I might be doing something wrong in creating the “bootstrap node”
how to create a bootstrap node correctly? PLEASE!!

below is my codes and logs
#bootstrap node code main.go#

package main

import (
	"context"

	"github.com/ipfs/go-log/v2"

	"github.com/libp2p/go-libp2p"

	"github.com/multiformats/go-multiaddr"

	ds "github.com/ipfs/go-datastore"
	dsync "github.com/ipfs/go-datastore/sync"
	dht "github.com/libp2p/go-libp2p-kad-dht"
)

var logger = log.Logger("rendezvous")

func main() {
	log.SetAllLoggers(log.LevelWarn)
	log.SetLogLevel("rendezvous", "debug")
	logger.Info("crate host")

	host, err := libp2p.New(libp2p.ListenAddrs([]multiaddr.Multiaddr(nil)...))
	if err != nil {
		panic(err)
	}
	logger.Info("Host created. We are:", host.ID())
	logger.Info(host.Addrs())

	ctx := context.Background()
	dstore := dsync.MutexWrap(ds.NewMapDatastore())
	// kademliaDHT := dht.New(ctx, host)
	kademliaDHT := dht.NewDHT(ctx, host, dstore)
	if err != nil {
		panic(err)
	}

	// Bootstrap the DHT. In the default configuration, this spawns a Background
	// thread that will refresh the peer table every five minutes.
	logger.Info("Bootstrapping the DHT")
	if err = kademliaDHT.Bootstrap(ctx); err != nil {
		panic(err)
	}

	// logger.Infof("%v%v", host.ID().Pretty(), host.Addrs())

	select {}

}

#bootstrap run log#

 > go run main.go

2022-04-14T15:35:50.232+0800	INFO	rendezvous	bootstrapnode/main.go:22	crate host
2022-04-14T15:35:50.340+0800	INFO	rendezvous	bootstrapnode/main.go:28	Host created. We are:QmboMwKV2AdRfdMqeYjy28d9RR9bv6Nt4kgWmXfMBAFn8o
2022-04-14T15:35:50.340+0800	INFO	rendezvous	bootstrapnode/main.go:29	[/ip4/192.168.3.88/tcp/60786 /ip4/127.0.0.1/tcp/60786 /ip6/::1/tcp/60789]
2022-04-14T15:35:50.341+0800	INFO	rendezvous	bootstrapnode/main.go:43	Bootstrapping the DHT
2022-04-14T15:35:50.342+0800	WARN	dht/RtRefreshManager	rtrefresh/rt_refresh_manager.go:136	failed when refreshing routing table2 errors occurred:
	* failed to query for self, err=failed to find any peer in table
	* failed to refresh cpl=0, err=failed to find any peer in table


2022-04-14T15:35:50.344+0800	WARN	dht/RtRefreshManager	rtrefresh/rt_refresh_manager.go:199	failed when refreshing routing table	{"error": "2 errors occurred:\n\t* failed to query for self, err=failed to find any peer in table\n\t* failed to refresh cpl=0, err=failed to find any peer in table\n\n"}
2022-04-14T15:45:50.347+0800	WARN	dht/RtRefreshManager	rtrefresh/rt_refresh_manager.go:199	failed when refreshing routing table	{"error": "2 errors occurred:\n\t* failed to query for self, err=failed to find any peer in table\n\t* failed to refresh cpl=0, err=failed to find any peer in table\n\n"}

#chat-with-rendezvous run log#

> go run . -peer=/ip4/127.0.0.1/tcp/60786/ipfs/QmboMwKV2AdRfdMqeYjy28d9RR9bv6Nt4kgWmXfMBAFn8o
2022-04-14T15:36:21.479+0800	INFO	rendezvous	chat-with-rendezvous/chat.go:104	Host created. We are:QmTGMQhxKqMp7AijWgCXZFDDqWPeLFxgmsEzjE1VFQcVH1
2022-04-14T15:36:21.480+0800	INFO	rendezvous	chat-with-rendezvous/chat.go:105	[/ip4/192.168.3.88/tcp/60795 /ip4/127.0.0.1/tcp/60795 /ip6/::1/tcp/60798]
2022-04-14T15:36:21.480+0800	DEBUG	rendezvous	chat-with-rendezvous/chat.go:123	Bootstrapping the DHT
2022-04-14T15:36:21.482+0800	WARN	dht/RtRefreshManager	rtrefresh/rt_refresh_manager.go:136	failed when refreshing routing table2 errors occurred:
	* failed to query for self, err=failed to find any peer in table
	* failed to refresh cpl=0, err=failed to find any peer in table


2022-04-14T15:36:21.484+0800	WARN	dht/RtRefreshManager	rtrefresh/rt_refresh_manager.go:199	failed when refreshing routing table	{"error": "2 errors occurred:\n\t* failed to query for self, err=failed to find any peer in table\n\t* failed to refresh cpl=0, err=failed to find any peer in table\n\n"}
2022-04-14T15:36:21.496+0800	INFO	rendezvous	chat-with-rendezvous/chat.go:139	Connection established with bootstrap node:{QmboMwKV2AdRfdMqeYjy28d9RR9bv6Nt4kgWmXfMBAFn8o: [/ip4/127.0.0.1/tcp/60786]}
2022-04-14T15:36:21.496+0800	INFO	rendezvous	chat-with-rendezvous/chat.go:147	Announcing ourselves...
2022-04-14T15:36:21.496+0800	DEBUG	rendezvous	chat-with-rendezvous/chat.go:150	Successfully announced!
2022-04-14T15:36:21.496+0800	DEBUG	rendezvous	chat-with-rendezvous/chat.go:154	Searching for other peers...
2022-04-14T15:46:21.481+0800	WARN	dht/RtRefreshManager	rtrefresh/rt_refresh_manager.go:199	failed when refreshing routing table	{"error": "2 errors occurred:\n\t* failed to query for self, err=failed to find any peer in table\n\t* failed to refresh cpl=0, err=failed to find any peer in table\n\n"}

I solve it .
new DHT should set Mode on first Bootstrap node :
kademliaDHT, err := dht.New(ctx, host, dht.Mode(dht.ModeAutoServer))

2 Likes

Hi, I’m having the same problem as you. But I didn’t solve it.

privateKey, err := crypto.UnmarshalPrivateKey([]byte(inputKey))
	if err != nil {
		return
	}

	ip6quic := fmt.Sprintf("/ip6/::/udp/%d/quic", port)
	ip4quic := fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port)

	ip6ws := fmt.Sprintf("/ip6/::/tcp/%d/ws", port)
	ip4ws := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/ws", port)

	node, err = libp2p.New(
		libp2p.ListenAddrStrings(ip6quic, ip4quic, ip6ws, ip4ws),
		libp2p.Identity(privateKey),
		libp2p.Security(tls.ID, tls.New),
		libp2p.NATPortMap(),
		libp2p.DefaultMuxers,
		libp2p.ChainOptions(
			libp2p.Transport(ws.New),
			libp2p.Transport(quic.NewTransport),
		),
		libp2p.EnableRelay(),
		libp2p.EnableAutoRelay(),
		libp2p.EnableRelayService(),
		libp2p.EnableHolePunching(),
	)
	if err != nil {
		return
	}

	node.SetStreamHandler(Protocol, handler)

	kademliaDHT, err := dht.New(ctx, node, dht.Mode(dht.ModeServer))
	if err != nil {
		panic(err)
	}

	// Bootstrap the DHT. In the default configuration, this spawns a Background
	// thread that will refresh the peer table every five minutes.
	logger.Debug("Bootstrapping the DHT")
	if err = kademliaDHT.Bootstrap(ctx); err != nil {
		panic(err)
	}

	peers := []string{
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
		"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
		"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
	}

	var wg sync.WaitGroup
	for _, peerAddr := range peers {
		addr, err := ma.NewMultiaddr(peerAddr)
		if err != nil {
			return node, route, err
		}
		peerInfo, _ := peer.AddrInfoFromP2pAddr(addr)
		wg.Add(1)
		go func() {
			defer wg.Done()
			if err := node.Connect(ctx, *peerInfo); err != nil {
				logger.Warning(err)
			} else {
				logger.Info("Connection established with bootstrap node:", *peerInfo)
			}
		}()
	}
	wg.Wait()

	route = discovery.NewRoutingDiscovery(kademliaDHT)
	discovery.Advertise(ctx, route, "bakpal")
	logger.Debug("Successfully announced!")

run log

2022-04-28T10:50:06.156+0800    DEBUG   p2p     p2p/node.go:65  Bootstrapping the DHT
2022-04-28T10:50:06.156+0800    WARN    dht/RtRefreshManager    rtrefresh/rt_refresh_manager.go:136     failed when refreshing routing table2 errors occurred:
        * failed to query for self, err=failed to find any peer in table
        * failed to refresh cpl=0, err=failed to find any peer in table


2022-04-28T10:50:06.171+0800    WARN    p2p     p2p/node.go:90  no good addresses
2022-04-28T10:50:06.317+0800    INFO    p2p     p2p/node.go:92  Connection established with bootstrap node:{QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-28T10:50:06.512+0800    INFO    p2p     p2p/node.go:92  Connection established with bootstrap node:{QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ: [/ip4/104.131.131.82/udp/4001/quic]}
2022-04-28T10:50:06.519+0800    INFO    p2p     p2p/node.go:92  Connection established with bootstrap node:{QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-28T10:50:06.658+0800    INFO    p2p     p2p/node.go:92  Connection established with bootstrap node:{QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-28T10:50:07.002+0800    INFO    p2p     p2p/node.go:92  Connection established with bootstrap node:{QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-28T10:50:07.002+0800    DEBUG   p2p     p2p/node.go:100 Successfully announced!

Where did it go wrong!!!

Hi, I have the same problem as you, but using your method didn’t solve it.
My codes:

func CreateNode(ctx context.Context, inputKey, peerId string, port int, handler network.StreamHandler) (node host.Host, route *routing.RoutingDiscovery, err error) {
	privateKey, err := crypto.UnmarshalPrivateKey([]byte(inputKey))
	if err != nil {
		return
	}

	ip6quic := fmt.Sprintf("/ip6/::/udp/%d/quic", port)
	ip4quic := fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port)

	ip6ws := fmt.Sprintf("/ip6/::/tcp/%d/ws", port)
	ip4ws := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d/ws", port)

	node, err = libp2p.New(
		libp2p.ListenAddrStrings(ip6quic, ip4quic, ip6ws, ip4ws),
		libp2p.Identity(privateKey),
		libp2p.Security(tls.ID, tls.New),
		libp2p.NATPortMap(),
		libp2p.DefaultMuxers,
		libp2p.ChainOptions(
			libp2p.Transport(ws.New),
			libp2p.Transport(quic.NewTransport),
		),
		libp2p.EnableRelay(),
		libp2p.EnableAutoRelay(),
		libp2p.EnableRelayService(),
		libp2p.EnableHolePunching(),
		libp2p.FallbackDefaults,
	)
	if err != nil {
		return
	}

	node.SetStreamHandler(Protocol, handler)

	kademliaDHT, err := dht.New(ctx, node, dht.Mode(dht.ModeAuto))
	if err != nil {
		panic(err)
	}

	// Bootstrap the DHT. In the default configuration, this spawns a Background
	// thread that will refresh the peer table every five minutes.
	logger.Debug("Bootstrapping the DHT")
	if err = kademliaDHT.Bootstrap(ctx); err != nil {
		panic(err)
	}

	peers := []string{
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
		"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
		"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
		"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
	}

	var wg sync.WaitGroup
	for _, peerAddr := range peers {
		addr, err := ma.NewMultiaddr(peerAddr)
		if err != nil {
			return node, route, err
		}
		peerInfo, _ := peer.AddrInfoFromP2pAddr(addr)
		wg.Add(1)
		go func() {
			defer wg.Done()
			if err := node.Connect(ctx, *peerInfo); err != nil {
				logger.Warning(err)
			} else {
				logger.Info("Connection established with bootstrap node:", *peerInfo)
			}
		}()
	}
	wg.Wait()

	route = routing.NewRoutingDiscovery(kademliaDHT)
	_, err = route.Advertise(ctx, "bakpal")
	if err != nil {
		return nil, nil, err
	}
	logger.Debug("Successfully announced!")

	return
}

*** run log ***

2022-04-29T10:02:56.681+0800    DEBUG   p2p     p2p/node.go:66  Bootstrapping the DHT
2022-04-29T10:02:56.681+0800    WARN    dht/RtRefreshManager    rtrefresh/rt_refresh_manager.go:136     failed when refreshing routing table2 errors occurred:
        * failed to query for self, err=failed to find any peer in table
        * failed to refresh cpl=0, err=failed to find any peer in table


2022-04-29T10:02:56.697+0800    WARN    p2p     p2p/node.go:91  no good addresses
2022-04-29T10:02:56.839+0800    INFO    p2p     p2p/node.go:93  Connection established with bootstrap node:{QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-29T10:02:57.032+0800    INFO    p2p     p2p/node.go:93  Connection established with bootstrap node:{QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ: [/ip4/104.131.131.82/udp/4001/quic]}
2022-04-29T10:02:57.174+0800    INFO    p2p     p2p/node.go:93  Connection established with bootstrap node:{QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-29T10:02:57.216+0800    INFO    p2p     p2p/node.go:93  Connection established with bootstrap node:{QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-29T10:02:57.224+0800    INFO    p2p     p2p/node.go:93  Connection established with bootstrap node:{QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb: [/dnsaddr/bootstrap.libp2p.io]}
2022-04-29T10:03:04.845+0800    DEBUG   p2p     p2p/node.go:104 Successfully announced!