bitpay / bitcore-p2p

Interface to the bitcoin P2P network for bitcore
MIT License
79 stars 275 forks source link

Can't connect Peer to localhost node #89

Closed fed-franz closed 7 years ago

fed-franz commented 7 years ago

Hi, I'm trying to run a simple script, like this:

var bitcore = require('bitcore-lib')
var p2p = require('bitcore-p2p')
var Peer = p2p.Peer;

var peer = new Peer({host: 'localhost', port: 3001})
peer.on('connect', function () { console.log('connect'); })
peer.on('ready', function(){  console.log('connected'); });
peer.on('disconnect', function() {  console.log('connection closed'); });
peer.connect()

However, the connection seems to be never established... I only get the 'connect' and the 'connection closed' output.

What am I doing wrong?

fed-franz commented 7 years ago

After many tries, I figured out that: 1) the port number should actually be that of the Bitcoind server (default 8333, or 18333 for testnet) 2) when connecting to a livenet peer, the code works perfectly, but when connecting to a testnet node it immediately disconnect. I tried both with my node, and with a public one.

So, it seems to me that this is a bug of the bitcore-p2p library. Otherwise, there is something different with testnet peers that I am not aware of.

In any case, I need some help (I cannot develop using the mainnet...)

fed-franz commented 7 years ago

I found something I think to be wrong: In the Peer code (peer.js), when sending the version message to the peer, it sends the livenet network object, instead of the testnet one:

{
  "command": "version",
  "network": {
    "name": "livenet",
    "alias": "mainnet",
...

Is it a bug or something I set up wrong?

fanatid commented 7 years ago

@frz-dev can you point on the line in code?

fed-franz commented 7 years ago

@fanatid I went a little further, up to Peer.prototype._sendVersion (peer.js#L200)

If I print the message created at line 202, I obtain the same wrong network as above

fanatid commented 7 years ago

@frz-dev if you check Peer constructor you can see that messages are created with network and you can set custom network via options new Peer({ network: 'testnet' })

fed-franz commented 7 years ago

Just noticed. I actually removed it because it caused some error (probably due to the wrong nvm version). Now it seems to connect.

Thanks, and sorry for the storm in a tea cup :)