Bitcore-p2p: Can't connect peer to local node

(FedFranz) #1
    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, network: tBTC})
peer.on('connect', function () { console.log('connect'); })
peer.on('connected', 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.
The port is the one specified on the node config file:
{
“network”: “testnet”,
“port”: 3001,
“services”: [
“bitcoind”,
“bitcore-wallet-service”,
“insight-api”,
“insight-ui”,
“web”
],
“servicesConfig”: {
“bitcoind”: {
“spawn”: {
“datadir”: “./data”,
“exec”: “/home/user/.nvm/versions/node/v4.6.0/lib/node_modules/bitcore/node_modules/bitcore-node/bin/bitcoind”
}
}
}
}

What am I doing wrong?

0 Likes

(FedFranz) #2

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.

1 Like

(Hung Nguyen) #3

@frz.dev: Have you found out the solution for Point 2? I got the same problem when I tried to send message to testnet peer.

0 Likes

(FedFranz) #4

I’m not really sure (I finally followed a different strategy) but as I remember correctly I had to play a bit with the parameters and at some point it worked…

Anyway, the following code works on my machine:

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

var tBTC = bitcore.Networks.testnet

var peer = new Peer({host: 'localhost', port: 18333, network: tBTC})

peer.on('connect', function () { console.log('connect'); })
peer.on('connected', function(){  console.log('connected'); });
peer.on('disconnect', function() {  console.log('connection closed'); });
peer.connect()

The bitcore-node.json is more or less the same

1 Like

(Hung Nguyen) #5

My snippet likes below. When I tried to send message to a remote node, an event “disconnect” will be always emitted. But when I tried with mainnet, this issues does not appear.

var bitcore = require(‘bitcore-lib’);
var Peer = require(‘bitcore-p2p’).Peer;
var Networks = bitcore.Networks;
var p2p = require(‘bitcore-p2p’)
var Messages = p2p.Messages;

var peer = new Peer({host: ‘104.237.131.138’, port: 18333, network: Networks.testnet})

peer.on(‘ready’, function() {
// peer info
console.log(peer.version, peer.subversion, peer.bestHeight);
var messages = new Messages();
var message = messages.Ping();
// var message = messages.GetAddr()
peer.sendMessage(message);
});

peer.on(‘connect’, function() {
// peer info
console.log(“Node is connecting”);
});

peer.on(‘connected’, function() {
// peer info
console.log(“Node is connected”);
});

peer.on(‘disconnect’, function() {
console.log(‘connection closed’);
});

peer.on(‘addr’, function(message) {
// message.addresses[]
message.addresses.forEach(function(address) {
// do something
console.log('Address is ’ + JSON.stringify(address))
});
});

peer.connect();

0 Likes