wip
This commit is contained in:
parent
bf31eb44a3
commit
019851e48f
@ -36,7 +36,7 @@ var P2P = function(options) {
|
|||||||
this._peerHeights = [];
|
this._peerHeights = [];
|
||||||
this._peers = [];
|
this._peers = [];
|
||||||
this._peerIndex = 0;
|
this._peerIndex = 0;
|
||||||
this._filters = [];
|
this._filters = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(P2P, BaseService);
|
util.inherits(P2P, BaseService);
|
||||||
@ -108,26 +108,28 @@ P2P.prototype._getPeer = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
P2P.prototype._getBestHeight = function(peer) {
|
P2P.prototype._getBestHeight = function(peer) {
|
||||||
|
|
||||||
this._peerHeights.push(peer.bestHeight);
|
this._peerHeights.push(peer.bestHeight);
|
||||||
if (this._peerHeights >= this._minPeers) {
|
|
||||||
return Math.max(this._bestHeights);
|
if (this._peerHeights.length >= this._minPeers) {
|
||||||
|
// spread operator '...', fancy way of converting an array arg to discrete args
|
||||||
|
return Math.max(...this._peerHeights);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
P2P.prototype._setFilterScalar = function(peer, scalar) {
|
||||||
|
|
||||||
P2P.prototype._appendFilters = function(peer, filter) {
|
if (!this._filters[peer]) {
|
||||||
var filterList = this._filters[peer];
|
|
||||||
|
|
||||||
if (!filterList || filterList.length !== 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('here', filter);
|
|
||||||
if (filter.length >= 10) {
|
this._filters[peer] = scalar;
|
||||||
this._filters[peer] = filter;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
P2P.prototype._setupListeners = function() {
|
P2P.prototype._setupListeners = function() {
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
self._pool.on('peerready', function(peer, addr) {
|
self._pool.on('peerready', function(peer, addr) {
|
||||||
@ -139,6 +141,7 @@ P2P.prototype._setupListeners = function() {
|
|||||||
|
|
||||||
self._addPeer(peer);
|
self._addPeer(peer);
|
||||||
var bestHeight = self._getBestHeight(peer);
|
var bestHeight = self._getBestHeight(peer);
|
||||||
|
|
||||||
if (bestHeight >= 0) {
|
if (bestHeight >= 0) {
|
||||||
self.emit('bestHeight', bestHeight);
|
self.emit('bestHeight', bestHeight);
|
||||||
}
|
}
|
||||||
@ -155,22 +158,23 @@ P2P.prototype._setupListeners = function() {
|
|||||||
|
|
||||||
var newDataNeeded = [];
|
var newDataNeeded = [];
|
||||||
|
|
||||||
// the bitcoin p2p network can send unsolicited inventory messages
|
// The bitcoin p2p network can send unsolicited inventory messages for
|
||||||
// because we've asked it to when connecting. We want to know about new
|
// entities such as transactions and blocks. This behavior is configurable
|
||||||
// blocks and transactions asynchronoously. The problem is that when we
|
// via the 'relay' field in the version message that we send when making
|
||||||
// do ask for inventory synchronuously, we can't tell messages arriving
|
// outbound connections. The relay option is important to keep enabled so
|
||||||
// after we've asked if they are a response to our request or unsolicited.
|
// that we can reduce the chatter on the network, overall.
|
||||||
// There is no request id that we can match against the response.
|
// The problem is that when we do ask for inventory synchronuously,
|
||||||
// The safest way to get around this is to disconnect the peer and reconnect
|
// such as when our indexes are syncing or catching up, we can't tell the
|
||||||
// without relaying turned om, so the peer will not spontaneously send us inv
|
// the difference between unsolicited new messages and actual responses.
|
||||||
// messages. This way we know for sure that the inv message is a response to
|
// There does not seem to be identifying information to link requests and
|
||||||
// our request.
|
// responses.
|
||||||
|
|
||||||
// have we asked for inventory from a particular peer? Check our filters list
|
// What we will do is set a filter scalar on the peer being queried. We
|
||||||
// based on filters that may have been previously set by a request from this service,
|
// will have to assume that the next message from that peer will be our
|
||||||
// we need to set the inventory list on the peer's filter so that when the inventory
|
// response. The length of the inventory vector will then become our filter
|
||||||
// is retrieved, it can be returned to the caller and not sent out on the main bux
|
// scalar. As we respond to the inbound inventory (getdata), we will decrement
|
||||||
self._appendFilters(peer, message.inventory);
|
// this filter scalar. This is how we will match request anf response.
|
||||||
|
self._setFilterScalar(peer, message.inventory.length);
|
||||||
|
|
||||||
message.inventory.forEach(function(inv) {
|
message.inventory.forEach(function(inv) {
|
||||||
|
|
||||||
@ -200,35 +204,25 @@ P2P.prototype._setupListeners = function() {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
P2P.prototype._processFilter = function(filter, message) {
|
|
||||||
var existsInFilter = false;
|
|
||||||
if (!filter) {
|
|
||||||
return existsInFilter;
|
|
||||||
}
|
|
||||||
filter.forEach(function(inv, index) {
|
|
||||||
console.log(inv.hash.toString('hex'), message.transaction.hash);
|
|
||||||
if (message.transaction.hash === inv.hash) {
|
|
||||||
existsInFilter = true;
|
|
||||||
filter.splice(index, 1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return existsInFilter;
|
|
||||||
};
|
|
||||||
|
|
||||||
P2P.prototype._filterTx = function(peer, message) {
|
P2P.prototype._filterTx = function(peer, message) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
// if this tx matches any of this peer's filters, then emit the tx internally only
|
// if this tx matches any of this peer's filters, then emit the tx internally only
|
||||||
// and not to the external bus
|
// and not to the external bus
|
||||||
var existsInFilter = self._processFilter(self._filters[peer], message);
|
var filterExists = self._filters[peer];
|
||||||
|
|
||||||
if (!existsInFilter) {
|
if (!filterExists) {
|
||||||
self._broadcastTx(peer, message);
|
self._broadcastTx(peer, message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.emit('tx', message.transaction);
|
self.emit('tx', message.transaction);
|
||||||
|
|
||||||
|
if (--self._filters[peer] === 0) {
|
||||||
|
self._filters[peer] = false;
|
||||||
|
self.emit('end');
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
P2P.prototype._broadcastTx = function(peer, message) {
|
P2P.prototype._broadcastTx = function(peer, message) {
|
||||||
@ -247,7 +241,7 @@ P2P.prototype.getAPIMethods = function() {
|
|||||||
var methods = [
|
var methods = [
|
||||||
['createHeaderStream', this, this.getHeaders, 2],
|
['createHeaderStream', this, this.getHeaders, 2],
|
||||||
['getMempool', this, this.getMempool, 1],
|
['getMempool', this, this.getMempool, 1],
|
||||||
['createBlockStream', this, this.getBlocks, 2]
|
['getBlocks', this, this.getBlocks, 2]
|
||||||
];
|
];
|
||||||
return methods;
|
return methods;
|
||||||
};
|
};
|
||||||
@ -298,7 +292,7 @@ P2P.prototype.getMempool = function(callback) {
|
|||||||
return callback(new Error('Could not get a peer to retrieve mempool.'));
|
return callback(new Error('Could not get a peer to retrieve mempool.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
self._filters[peer] = [];
|
self._filters[peer] = true;
|
||||||
|
|
||||||
peer.sendMessage(self.messages.MemPool());
|
peer.sendMessage(self.messages.MemPool());
|
||||||
var mempool = [];
|
var mempool = [];
|
||||||
@ -312,15 +306,32 @@ P2P.prototype.getMempool = function(callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
P2P.prototype.createBlockStream = function(startBlockHash, endBlockHash) {
|
P2P.prototype.getBlocks = function(startBlockHash, endBlockHash) {
|
||||||
|
|
||||||
|
var self = this;
|
||||||
if (!endBlockHash) {
|
if (!endBlockHash) {
|
||||||
endBlockHash = startBlockHash;
|
endBlockHash = startBlockHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
var message = this.messages.GetBlocks(startBlockHash, endBlockHash);
|
var peer = self._getPeer();
|
||||||
var peer = this._getPeer();
|
|
||||||
|
if (!peer) {
|
||||||
|
return callback(new Error('Could not get a peer to retrieve blocks.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
self._filters[peer] = true;
|
||||||
|
|
||||||
|
var message = this.messages.GetBlocks({ starts: [startBlockHash] });
|
||||||
peer.sendMessage(message);
|
peer.sendMessage(message);
|
||||||
|
var blocks = [];
|
||||||
|
|
||||||
|
self.on('block', function(block) {
|
||||||
|
blocks.push(block);
|
||||||
|
});
|
||||||
|
|
||||||
|
self.on('end', function() {
|
||||||
|
callback(null, blocks);
|
||||||
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -163,7 +163,17 @@ describe('P2P Operations', function() {
|
|||||||
], done);
|
], done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it.only('should connect to the p2p network and stream the mempool to clients', function(done) {
|
//it('should get blocks from peer that we do not have on startup', function(done) {
|
||||||
|
|
||||||
|
// setTimeout(function() {
|
||||||
|
// expect(blocks.length).to.equal(10);
|
||||||
|
// done();
|
||||||
|
// }, 1000);
|
||||||
|
|
||||||
|
// done();
|
||||||
|
//});
|
||||||
|
|
||||||
|
it('should connect to the p2p network and stream the mempool to clients', function(done) {
|
||||||
async.series([
|
async.series([
|
||||||
utils.unlockWallet.bind(utils),
|
utils.unlockWallet.bind(utils),
|
||||||
utils.setupInitialTxs.bind(utils),
|
utils.setupInitialTxs.bind(utils),
|
||||||
@ -203,7 +213,7 @@ describe('P2P Operations', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should send new transactions as they are broadcasted by our trusted peer', function(done) {
|
it('should send new transactions as they are broadcasted by our trusted peer (unsoliticted)', function(done) {
|
||||||
var tx;
|
var tx;
|
||||||
async.series([
|
async.series([
|
||||||
|
|
||||||
@ -244,13 +254,8 @@ describe('P2P Operations', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get blocks from peer that we do not have on startup', function(done) {
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should send new blocks as they are broadcasted by our trusted peer', function(done) {
|
it('should send new blocks as they are broadcasted by our trusted peer', function(done) {
|
||||||
expect(blocks.length).to.equal(1);
|
//expect(blocks.length).to.equal(1);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,8 @@ var inherits = require('util').inherits;
|
|||||||
var zmq = require('zmq');
|
var zmq = require('zmq');
|
||||||
var index = require('../lib');
|
var index = require('../lib');
|
||||||
var log = index.log;
|
var log = index.log;
|
||||||
|
var constants = require('../lib/constants');
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
var TestBusService = function(options) {
|
var TestBusService = function(options) {
|
||||||
BaseService.call(this, options);
|
BaseService.call(this, options);
|
||||||
@ -37,12 +39,15 @@ TestBusService.prototype.start = function(callback) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
self.node.services.p2p.on('bestHeight', function(height) {
|
||||||
|
self._bestHeight = height;
|
||||||
|
});
|
||||||
|
|
||||||
self.bus.on('p2p/block', function(block) {
|
self.bus.on('p2p/block', function(block) {
|
||||||
var self = this;
|
|
||||||
self._cache.block.push(block);
|
self._cache.block.push(block);
|
||||||
if (self._ready) {
|
if (self._ready) {
|
||||||
for(var i = 0; i < self._cache.block.length; i++) {
|
for(var i = 0; i < self._cache.block.length; i++) {
|
||||||
var blk = self._cache.block.unshift();
|
var blk = self._cache.block.shift();
|
||||||
self.pubSocket.send([ 'block', blk.toBuffer() ]);
|
self.pubSocket.send([ 'block', blk.toBuffer() ]);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -54,13 +59,34 @@ TestBusService.prototype.start = function(callback) {
|
|||||||
|
|
||||||
self.node.on('ready', function() {
|
self.node.on('ready', function() {
|
||||||
|
|
||||||
|
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
self._ready = true;
|
self._ready = true;
|
||||||
self.node.services.p2p.getMempool(function(err, mempool) {
|
self.node.services.p2p.getMempool(function(err, mempool) {
|
||||||
|
|
||||||
console.log(mempool);
|
if(err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
mempool.forEach(function(tx) {
|
||||||
|
self.pubSocket.send([ 'transaction', new Buffer(tx.uncheckedSerialize(), 'hex') ]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assert(self._bestHeight, 'best height not set on a time after ready');
|
||||||
|
self.node.services.p2p.getBlocks(
|
||||||
|
constants.BITCOIN_GENESIS_HASH.regtest,
|
||||||
|
self._bestHeight,
|
||||||
|
function(err, blocks) {
|
||||||
|
|
||||||
|
if(err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
blocks.forEach(function(block) {
|
||||||
|
self.pubSocket.send([ 'block', block.toBuffer() ]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
}, 2000);
|
}, 2000);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user