dht_bootstrap.go 4.1 KB
Newer Older
1 2 3
package dht

import (
Jeromy's avatar
Jeromy committed
4
	"context"
5 6 7 8
	"crypto/rand"
	"fmt"
	"time"

9
	u "github.com/ipfs/go-ipfs-util"
10
	peer "github.com/libp2p/go-libp2p-peer"
Matt Joiner's avatar
Matt Joiner committed
11
	peerstore "github.com/libp2p/go-libp2p-peerstore"
George Antoniadis's avatar
George Antoniadis committed
12
	routing "github.com/libp2p/go-libp2p-routing"
13 14
)

15
// BootstrapConfig specifies parameters used bootstrapping the DHT.
16
//
17 18 19 20 21
// Note there is a tradeoff between the bootstrap period and the
// number of queries. We could support a higher period with less
// queries.
type BootstrapConfig struct {
	Queries int           // how many queries to run per period
22 23
	Period  time.Duration // how often to run periodic bootstrap.
	Timeout time.Duration // how long to wait for a bootstrap query to run
24
}
25

26 27 28 29 30 31
var DefaultBootstrapConfig = BootstrapConfig{
	// For now, this is set to 1 query.
	// We are currently more interested in ensuring we have a properly formed
	// DHT than making sure our dht minimizes traffic. Once we are more certain
	// of our implementation's robustness, we should lower this down to 8 or 4.
	Queries: 1,
32

Cole Brown's avatar
Cole Brown committed
33
	// For now, this is set to 5 minutes, which is a medium period. We are
34
	// We are currently more interested in ensuring we have a properly formed
35 36
	// DHT than making sure our dht minimizes traffic.
	Period: time.Duration(5 * time.Minute),
37

38
	Timeout: time.Duration(10 * time.Second),
39 40
}

Matt Joiner's avatar
Matt Joiner committed
41 42
// A method in the IpfsRouting interface. It calls BootstrapWithConfig with
// the default bootstrap config.
43
func (dht *IpfsDHT) Bootstrap(ctx context.Context) error {
Matt Joiner's avatar
Matt Joiner committed
44 45
	return dht.BootstrapWithConfig(ctx, DefaultBootstrapConfig)
}
46

Matt Joiner's avatar
Matt Joiner committed
47 48 49 50 51 52
// Runs cfg.Queries bootstrap queries every cfg.Period.
func (dht *IpfsDHT) BootstrapWithConfig(ctx context.Context, cfg BootstrapConfig) error {
	if cfg.Queries <= 0 {
		return fmt.Errorf("invalid number of queries: %d", cfg.Queries)
	}
	ctx, cancel := context.WithCancel(ctx)
53
	go func() {
Matt Joiner's avatar
Matt Joiner committed
54
		defer cancel()
55 56
		select {
		case <-dht.Context().Done():
Matt Joiner's avatar
Matt Joiner committed
57
		case <-ctx.Done():
58 59
		}
	}()
Matt Joiner's avatar
Matt Joiner committed
60
	go func() {
61
		for {
Matt Joiner's avatar
Matt Joiner committed
62 63 64 65
			err := dht.runBootstrap(ctx, cfg)
			if err != nil {
				log.Warningf("error bootstrapping: %s", err)
			}
66 67
			select {
			case <-time.After(cfg.Period):
Matt Joiner's avatar
Matt Joiner committed
68
			case <-ctx.Done():
69 70 71
				return
			}
		}
Matt Joiner's avatar
Matt Joiner committed
72 73 74
	}()
	return nil
}
vyzo's avatar
vyzo committed
75

Matt Joiner's avatar
Matt Joiner committed
76 77 78 79 80
func newRandomPeerId() peer.ID {
	id := make([]byte, 32) // SHA256 is the default. TODO: Use a more canonical way to generate random IDs.
	rand.Read(id)
	id = u.Hash(id) // TODO: Feed this directly into the multihash instead of hashing it.
	return peer.ID(id)
81 82
}

Matt Joiner's avatar
Matt Joiner committed
83 84 85 86 87 88 89
// Traverse the DHT toward the given ID.
func (dht *IpfsDHT) walk(ctx context.Context, target peer.ID) (peerstore.PeerInfo, error) {
	// TODO: Extract the query action (traversal logic?) inside FindPeer,
	// don't actually call through the FindPeer machinery, which can return
	// things out of the peer store etc.
	return dht.FindPeer(ctx, target)
}
90

Matt Joiner's avatar
Matt Joiner committed
91 92 93 94 95 96 97 98 99 100 101 102 103
// Traverse the DHT toward a random ID.
func (dht *IpfsDHT) randomWalk(ctx context.Context) error {
	id := newRandomPeerId()
	p, err := dht.walk(ctx, id)
	switch err {
	case routing.ErrNotFound:
		return nil
	case nil:
		// We found a peer from a randomly generated ID. This should be very unlikely.
		log.Warningf("Bootstrap peer error: Actually FOUND peer. (%s, %s)", id, p)
		return nil
	default:
		return err
104
	}
105 106 107
}

// runBootstrap builds up list of peers by requesting random peer IDs
108
func (dht *IpfsDHT) runBootstrap(ctx context.Context, cfg BootstrapConfig) error {
Matt Joiner's avatar
Matt Joiner committed
109

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112 113 114 115
	bslog := func(msg string) {
		log.Debugf("DHT %s dhtRunBootstrap %s -- routing table size: %d", dht.self, msg, dht.routingTable.Size())
	}
	bslog("start")
	defer bslog("end")
	defer log.EventBegin(ctx, "dhtRunBootstrap").Done()
116

Matt Joiner's avatar
Matt Joiner committed
117 118
	doQuery := func(n int, target string, f func(context.Context) error) error {
		log.Debugf("Bootstrapping query (%d/%d) to %s", n, cfg.Queries, target)
119 120
		ctx, cancel := context.WithTimeout(ctx, cfg.Timeout)
		defer cancel()
Matt Joiner's avatar
Matt Joiner committed
121
		return f(ctx)
122 123
	}

Matt Joiner's avatar
Matt Joiner committed
124 125 126 127 128 129
	// Do all but one of the bootstrap queries as random walks.
	for i := 1; i < cfg.Queries; i++ {
		err := doQuery(i, "random ID", dht.randomWalk)
		if err != nil {
			return err
		}
130 131
	}

132
	// Find self to distribute peer info to our neighbors.
Matt Joiner's avatar
Matt Joiner committed
133 134 135 136
	return doQuery(cfg.Queries, fmt.Sprintf("self: %s", dht.self), func(ctx context.Context) error {
		_, err := dht.walk(ctx, dht.self)
		return err
	})
137
}