bitpay / bitcore-p2p

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

Peer's tx event isn't emitted #84

Open luisflma opened 8 years ago

luisflma commented 8 years ago

Hi, I can't get transaction's information. In this code, the tx event is never emitted. Connected, ready and inv events are emitted but not tx.

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

var peer = new Peer({host: '127.0.0.1', port: 8333});

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

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

peer.on('inv', function (msg) {
  console.log('inv');
});

peer.on('ready', function() {
  console.log('ready');
});

peer.on('tx', function(message) {
  // message.transaction 
  console.log(message);
});

peer.connect();

In this file: https://github.com/bitpay/bitcore-p2p/blob/master/lib/peer.js#L218 I have edited the _readMessage function to debug it:

Peer.prototype._readMessage = function() {
  var message = this.messages.parseBuffer(this.dataBuffer);
  if(message == undefined) console.log(message);
  else console.log(message.command);
  if (message) {
    this.emit(message.command, message);
    this._readMessage();
  }
};

output:

inv
undefined
inv
undefined
inv
undefined
stefanhuber commented 8 years ago

the tx message is only sent by the peer, if a getdata request is made. See the bitcoin wiki: https://en.bitcoin.it/wiki/Protocol_documentation#inv

so after you received the inv message, you need to create an getdata response and only then you'll get the tx or block messages...

braydonf commented 8 years ago

Related: https://github.com/bitpay/bitcore-p2p/issues/82

luisflma commented 8 years ago

Thank you for your answer, I'll try it.