more pool and spv stuff.

This commit is contained in:
Christopher Jeffrey 2016-06-02 16:14:04 -07:00
parent 6872a18538
commit 6176b0d228
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 26 additions and 56 deletions

View File

@ -476,4 +476,4 @@ exports = Bloom;
exports.murmur3 = murmur3; exports.murmur3 = murmur3;
exports.rolling = RollingFilter; exports.rolling = RollingFilter;
module.exports = Bloom; module.exports = exports;

View File

@ -311,7 +311,8 @@ MerkleBlock.prototype.inspect = function inspect() {
totalTX: this.totalTX, totalTX: this.totalTX,
hashes: this.hashes, hashes: this.hashes,
flags: this.flags, flags: this.flags,
map: this.map map: this.map,
txs: this.txs.length
}; };
}; };

View File

@ -93,6 +93,8 @@ function Peer(pool, options) {
this.feeRate = -1; this.feeRate = -1;
this.addrFilter = new bcoin.bloom.rolling(5000, 0.001); this.addrFilter = new bcoin.bloom.rolling(5000, 0.001);
this.invFilter = new bcoin.bloom.rolling(50000, 0.000001); this.invFilter = new bcoin.bloom.rolling(50000, 0.000001);
this.lastBlock = null;
this.waiting = 0;
this.challenge = null; this.challenge = null;
this.lastPong = -1; this.lastPong = -1;
@ -195,6 +197,9 @@ Peer.prototype._init = function init() {
return; return;
} }
self.ack = true;
self.ts = utils.now();
// Setup the ping interval. // Setup the ping interval.
self.ping.timer = setInterval(function() { self.ping.timer = setInterval(function() {
self.sendPing(); self.sendPing();
@ -229,8 +234,6 @@ Peer.prototype._init = function init() {
// Finally we can let the pool know // Finally we can let the pool know
// that this peer is ready to go. // that this peer is ready to go.
self.ack = true;
self.ts = utils.now();
self.emit('ack'); self.emit('ack');
}); });
@ -679,16 +682,21 @@ Peer.prototype._onPacket = function onPacket(packet) {
break; break;
case 'merkleblock': case 'merkleblock':
payload = new bcoin.merkleblock(payload); payload = new bcoin.merkleblock(payload);
payload.verifyPartial();
this.lastBlock = payload; this.lastBlock = payload;
this.waiting = payload.matches.length;
if (this.waiting === 0)
this._flushMerkle();
break; break;
case 'tx': case 'tx':
payload = new bcoin.tx(payload); payload = new bcoin.tx(payload);
if (this.lastBlock) { if (this.lastBlock) {
if (this.lastBlock.hasTX(payload)) { if (this.lastBlock.hasTX(payload)) {
this.lastBlock.addTX(payload); this.lastBlock.addTX(payload);
if (--this.waiting === 0)
this._flushMerkle();
break; break;
} }
this._flushMerkle();
} }
this.fire(cmd, payload); this.fire(cmd, payload);
break; break;
@ -722,6 +730,7 @@ Peer.prototype._flushMerkle = function _flushMerkle() {
if (this.lastBlock) if (this.lastBlock)
this.fire('merkleblock', this.lastBlock); this.fire('merkleblock', this.lastBlock);
this.lastBlock = null; this.lastBlock = null;
this.waiting = 0;
}; };
Peer.prototype._handleFilterLoad = function _handleFilterLoad(payload) { Peer.prototype._handleFilterLoad = function _handleFilterLoad(payload) {

View File

@ -138,12 +138,10 @@ function Pool(options) {
this.requestTimeout = options.requestTimeout || 20 * 60000; this.requestTimeout = options.requestTimeout || 20 * 60000;
this.watchMap = {};
this.feeRate = options.feeRate != null ? options.feeRate : -1; this.feeRate = options.feeRate != null ? options.feeRate : -1;
this.spvFilter = options.spv this.spvFilter = options.spv
? bcoin.bloom.fromRate(10000, 0.01, constants.bloom.NONE) ? bcoin.bloom.fromRate(10000, 0.001, constants.bloom.NONE)
: null; : null;
this.localNonce = utils.nonce(); this.localNonce = utils.nonce();
@ -1296,49 +1294,16 @@ Pool.prototype._removePeer = function _removePeer(peer) {
*/ */
Pool.prototype.watch = function watch(data, enc) { Pool.prototype.watch = function watch(data, enc) {
var key = data;
if (Buffer.isBuffer(key))
key = key.toString('hex');
if (this.watchMap[data]) {
this.watchMap[data]++;
return;
}
this.watchMap[data] = 1;
this.spvFilter.add(data, enc); this.spvFilter.add(data, enc);
this.updateWatch(); this.updateWatch();
}; };
/** /**
* Unwatch an address hash (filterload, SPV-only). * Reset the spv filter (filterload, SPV-only).
* @param {Buffer|Hash} data
* @param {String?} enc
*/ */
Pool.prototype.unwatch = function unwatch(data, enc) { Pool.prototype.unwatch = function unwatch() {
var key = data;
if (Buffer.isBuffer(key))
key = key.toString('hex');
if (!this.watchMap[data])
return;
if (--this.watchMap[data] !== 0)
return;
delete this.watchMap[data];
this.spvFilter.reset(); this.spvFilter.reset();
Object.keys(this.watchMap).forEach(function(data) {
this.spvFilter.add(data, 'hex');
}, this);
this.updateWatch(); this.updateWatch();
}; };
@ -1375,15 +1340,6 @@ Pool.prototype.watchAddress = function watchAddress(address) {
this.watch(bcoin.address.getHash(address), 'hex'); this.watch(bcoin.address.getHash(address), 'hex');
}; };
/**
* Remove an address from the bloom filter (SPV-only).
* @param {Address|Base58Address} address
*/
Pool.prototype.unwatchAddress = function unwatchAddress(address) {
this.unwatch(bcoin.address.getHash(address), 'hex');
};
/** /**
* Reset the chain to the wallet's last * Reset the chain to the wallet's last
* active transaction's timestamp/height (SPV-only). * active transaction's timestamp/height (SPV-only).

View File

@ -1609,6 +1609,7 @@ TX.prototype.getPrevout = function getPrevout() {
*/ */
TX.prototype.isWatched = function isWatched(filter) { TX.prototype.isWatched = function isWatched(filter) {
var found = false;
var i, input, output, hash, index, outpoint; var i, input, output, hash, index, outpoint;
if (!filter) if (!filter)
@ -1632,7 +1633,7 @@ TX.prototype.isWatched = function isWatched(filter) {
// 1. Test the tx hash // 1. Test the tx hash
if (filter.test(this.hash())) if (filter.test(this.hash()))
return true; found = true;
// 2. Test data elements in output scripts // 2. Test data elements in output scripts
// (may need to update filter on match) // (may need to update filter on match)
@ -1649,10 +1650,13 @@ TX.prototype.isWatched = function isWatched(filter) {
filter.add(outpoint); filter.add(outpoint);
} }
} }
return true; found = true;
} }
} }
if (found)
return found;
// 3. Test prev_out structure // 3. Test prev_out structure
// 4. Test data elements in input scripts // 4. Test data elements in input scripts
for (i = 0; i < this.inputs.length; i++) { for (i = 0; i < this.inputs.length; i++) {
@ -1670,8 +1674,8 @@ TX.prototype.isWatched = function isWatched(filter) {
return true; return true;
// Test the witness // Test the witness
if (testScript(input.witness.items)) // if (testScript(input.witness.items))
return true; // return true;
} }
// 5. No match // 5. No match