bitcore-p2p
bitcore-p2p copied to clipboard
Peer's tx event isn't emitted
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
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...
Related: https://github.com/bitpay/bitcore-p2p/issues/82
Thank you for your answer, I'll try it.