nazar-pc / webtorrent-dht

This is an example implementation of something that might become WebTorrent DHT
29 stars 3 forks source link

'ready'-event without successful bootstrapping #4

Closed derpsteb closed 6 years ago

derpsteb commented 6 years ago

I am not sure if this intended so I am asking to clarify or correct my understanding:

According to https://github.com/webtorrent/bittorrent-dht#dhtonready-function---- the 'ready'-event is supposed to fire when a sufficient (I suppose >= 1) amount of bootstrap nodes are known and connected to the current node. If the bootstrap option is set to false the event is fired right away.

When running the demo code of this repo the ready event is fired even though no bootstrap node was found yet. Is this a bug or just me not understanding what is going on?

(function(){
  var DHT;
  DHT = require('../webtorrent-dht');
  global.dht = new DHT({
    nodes: [{
      host: '127.0.0.1',
      port: 16881
    }],
    simple_peer_opts: {
      config: {
        iceServers: []
      }
    }
  });
  global.dht.on("ready", () => {
    console.log("i am ready");
    let dict = global.dht._rpc.socket.socket._peer_connections;
    for (var key in dict) {
      console.log(dict[key]["_id"]);
    }
  });
}).call(this);
nazar-pc commented 6 years ago

With current version (0.11.2) I'm getting:

i am ready
71a9a8e

I'm running demo/node-server.js and demo/node-client.js.

derpsteb commented 6 years ago

Yeah. But see what happens when not running both programms but only node-client.js. You still get the ready print but no connected nodes.

nazar-pc commented 6 years ago

Interesting idea, haven't done that, will take a look after some sleep :)

derpsteb commented 6 years ago

Sure, thank you.

nazar-pc commented 6 years ago

I observer the same behavior with bittorrent-dht:

(function(){
  var DHT;
  DHT = require('bittorrent-dht');
  global.dht = new DHT({
    nodes: [{
      host: '127.0.0.1',
      port: 16881
    }]
  });
  global.dht.on("ready", () => {
    console.log("i am ready");
  });
}).call(this);

In fact, it seems to fire ready whenever each bootstrap is processed. Processed means either success or failure, so if specified bootstrap node is not available, ready event is fired too as there is nothing left to wait and waiting forever doesn't make much sense anyway.

Also note that if you try to interact with the network afterwards (like making a lookup) it should try specified bootstrap nodes again.

Not sure this is a perfect behavior, but it seems reasonable.

derpsteb commented 6 years ago

Great. Thank you very much for the explanation. Did you gather that information by reading the source code or did you use some kind of debugging tool? Just asking so I can hopefully answer those kind of questions by myself in the future :D

nazar-pc commented 6 years ago

Both, but debugger is definitely very useful, this is how I use it: 1) Open Chromium 2) Go to chrome://inspect 3) Click Open dedicated DevTools for Node 4) node --inspect-brk demo/node-client.js

--inspect-brk means it will wait for connection from debugger (Chromium in this case) and will pause on the first line of code, so you can press F8 (Linux) to run it. Whenever debugger statement of other breakpoint is reached you'll be able to inspect local variables and stacktrace.

derpsteb commented 6 years ago

That seems like a very handy tool. Thank you very much again for the tips.