bitcore-p2p icon indicating copy to clipboard operation
bitcore-p2p copied to clipboard

Peer's tx event isn't emitted

Open luisflma opened this issue 9 years ago • 3 comments

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

luisflma avatar Nov 27 '15 10:11 luisflma

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...

stefanhuber avatar Nov 30 '15 16:11 stefanhuber

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

braydonf avatar Dec 01 '15 15:12 braydonf

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

luisflma avatar Dec 02 '15 08:12 luisflma