]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add NoDefaultBootstrap and use it and testify in a few tests
authorMatt Joiner <anacrolix@gmail.com>
Wed, 16 Dec 2015 04:13:32 +0000 (15:13 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 16 Dec 2015 04:13:32 +0000 (15:13 +1100)
dht/dht.go
dht/dht_test.go
dht/server.go

index 4bc4a5c61f592a2ddaba326c066e06a6a283372c..31bef132b41891bece6d4f8dc90ae26e94925ae2 100644 (file)
@@ -38,12 +38,19 @@ type transactionKey struct {
 // ServerConfig allows to set up a  configuration of the `Server` instance
 // to be created with NewServer
 type ServerConfig struct {
-       Addr string // Listen address. Used if Conn is nil.
+       // Listen address. Used if Conn is nil.
+       Addr string
        Conn net.PacketConn
        // Don't respond to queries from other nodes.
        Passive bool
        // DHT Bootstrap nodes
        BootstrapNodes []string
+       // Disable bootstrapping from global servers even if given no BootstrapNodes.
+       // This creates a solitary node that awaits other nodes; it's only useful if
+       // you're creating your own DHT and want to avoid accidental crossover, without
+       // spoofing a bootstrap node and filling your logs with connection errors.
+       NoDefaultBootstrap bool
+
        // Disable the DHT security extension:
        // http://www.libtorrent.org/dht_sec.html.
        NoSecurity bool
@@ -171,6 +178,7 @@ func (n *node) DefinitelyGood() bool {
        }
        return true
 }
+
 func jitterDuration(average time.Duration, plusMinus time.Duration) time.Duration {
        return average - plusMinus/2 + time.Duration(rand.Int63n(int64(plusMinus)))
 }
index 7dd5ef58657e5c9a9a2765880f473c4ef7ced019..c3afe6a5547fc2a3fd18489ba44d18a54ee4a5dc 100644 (file)
@@ -107,23 +107,23 @@ func TestDHTDefaultConfig(t *testing.T) {
 }
 
 func TestPing(t *testing.T) {
-       srv, err := NewServer(nil)
-       if err != nil {
-               t.Fatal(err)
-       }
+       srv, err := NewServer(&ServerConfig{
+               Addr:               "127.0.0.1:5680",
+               NoDefaultBootstrap: true,
+       })
+       require.NoError(t, err)
        defer srv.Close()
-       srv0, err := NewServer(nil)
-       if err != nil {
-               t.Fatal(err)
-       }
+       srv0, err := NewServer(&ServerConfig{
+               Addr:           "127.0.0.1:5681",
+               BootstrapNodes: []string{"127.0.0.1:5680"},
+       })
+       require.NoError(t, err)
        defer srv0.Close()
        tn, err := srv.Ping(&net.UDPAddr{
                IP:   []byte{127, 0, 0, 1},
                Port: srv0.Addr().(*net.UDPAddr).Port,
        })
-       if err != nil {
-               t.Fatal(err)
-       }
+       require.NoError(t, err)
        defer tn.Close()
        ok := make(chan bool)
        tn.SetResponseHandler(func(msg Msg, msgOk bool) {
@@ -159,9 +159,7 @@ func TestDHTSec(t *testing.T) {
        } {
                ip := net.ParseIP(case_.ipStr)
                id, err := hex.DecodeString(case_.nodeIDHex)
-               if err != nil {
-                       t.Fatal(err)
-               }
+               require.NoError(t, err)
                secure := NodeIdSecure(string(id), ip)
                if secure != case_.valid {
                        t.Fatalf("case failed: %v", case_)
index e20360126f9d05749e5c8c7902720b363ea1e6ec..b755289d6684dca2fe5f77caa832210d6bb21f93 100644 (file)
@@ -541,7 +541,7 @@ func (s *Server) addRootNodes() error {
 func (s *Server) bootstrap() (err error) {
        s.mu.Lock()
        defer s.mu.Unlock()
-       if len(s.nodes) == 0 {
+       if len(s.nodes) == 0 && !s.config.NoDefaultBootstrap {
                err = s.addRootNodes()
        }
        if err != nil {