more pool and spv stuff.
This commit is contained in:
parent
6872a18538
commit
6176b0d228
@ -476,4 +476,4 @@ exports = Bloom;
|
||||
exports.murmur3 = murmur3;
|
||||
exports.rolling = RollingFilter;
|
||||
|
||||
module.exports = Bloom;
|
||||
module.exports = exports;
|
||||
|
||||
@ -311,7 +311,8 @@ MerkleBlock.prototype.inspect = function inspect() {
|
||||
totalTX: this.totalTX,
|
||||
hashes: this.hashes,
|
||||
flags: this.flags,
|
||||
map: this.map
|
||||
map: this.map,
|
||||
txs: this.txs.length
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@ -93,6 +93,8 @@ function Peer(pool, options) {
|
||||
this.feeRate = -1;
|
||||
this.addrFilter = new bcoin.bloom.rolling(5000, 0.001);
|
||||
this.invFilter = new bcoin.bloom.rolling(50000, 0.000001);
|
||||
this.lastBlock = null;
|
||||
this.waiting = 0;
|
||||
|
||||
this.challenge = null;
|
||||
this.lastPong = -1;
|
||||
@ -195,6 +197,9 @@ Peer.prototype._init = function init() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.ack = true;
|
||||
self.ts = utils.now();
|
||||
|
||||
// Setup the ping interval.
|
||||
self.ping.timer = setInterval(function() {
|
||||
self.sendPing();
|
||||
@ -229,8 +234,6 @@ Peer.prototype._init = function init() {
|
||||
|
||||
// Finally we can let the pool know
|
||||
// that this peer is ready to go.
|
||||
self.ack = true;
|
||||
self.ts = utils.now();
|
||||
self.emit('ack');
|
||||
});
|
||||
|
||||
@ -679,16 +682,21 @@ Peer.prototype._onPacket = function onPacket(packet) {
|
||||
break;
|
||||
case 'merkleblock':
|
||||
payload = new bcoin.merkleblock(payload);
|
||||
payload.verifyPartial();
|
||||
this.lastBlock = payload;
|
||||
this.waiting = payload.matches.length;
|
||||
if (this.waiting === 0)
|
||||
this._flushMerkle();
|
||||
break;
|
||||
case 'tx':
|
||||
payload = new bcoin.tx(payload);
|
||||
if (this.lastBlock) {
|
||||
if (this.lastBlock.hasTX(payload)) {
|
||||
this.lastBlock.addTX(payload);
|
||||
if (--this.waiting === 0)
|
||||
this._flushMerkle();
|
||||
break;
|
||||
}
|
||||
this._flushMerkle();
|
||||
}
|
||||
this.fire(cmd, payload);
|
||||
break;
|
||||
@ -722,6 +730,7 @@ Peer.prototype._flushMerkle = function _flushMerkle() {
|
||||
if (this.lastBlock)
|
||||
this.fire('merkleblock', this.lastBlock);
|
||||
this.lastBlock = null;
|
||||
this.waiting = 0;
|
||||
};
|
||||
|
||||
Peer.prototype._handleFilterLoad = function _handleFilterLoad(payload) {
|
||||
|
||||
@ -138,12 +138,10 @@ function Pool(options) {
|
||||
|
||||
this.requestTimeout = options.requestTimeout || 20 * 60000;
|
||||
|
||||
this.watchMap = {};
|
||||
|
||||
this.feeRate = options.feeRate != null ? options.feeRate : -1;
|
||||
|
||||
this.spvFilter = options.spv
|
||||
? bcoin.bloom.fromRate(10000, 0.01, constants.bloom.NONE)
|
||||
? bcoin.bloom.fromRate(10000, 0.001, constants.bloom.NONE)
|
||||
: null;
|
||||
|
||||
this.localNonce = utils.nonce();
|
||||
@ -1296,49 +1294,16 @@ Pool.prototype._removePeer = function _removePeer(peer) {
|
||||
*/
|
||||
|
||||
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.updateWatch();
|
||||
};
|
||||
|
||||
/**
|
||||
* Unwatch an address hash (filterload, SPV-only).
|
||||
* @param {Buffer|Hash} data
|
||||
* @param {String?} enc
|
||||
* Reset the spv filter (filterload, SPV-only).
|
||||
*/
|
||||
|
||||
Pool.prototype.unwatch = function unwatch(data, enc) {
|
||||
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];
|
||||
|
||||
Pool.prototype.unwatch = function unwatch() {
|
||||
this.spvFilter.reset();
|
||||
|
||||
Object.keys(this.watchMap).forEach(function(data) {
|
||||
this.spvFilter.add(data, 'hex');
|
||||
}, this);
|
||||
|
||||
this.updateWatch();
|
||||
};
|
||||
|
||||
@ -1375,15 +1340,6 @@ Pool.prototype.watchAddress = function watchAddress(address) {
|
||||
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
|
||||
* active transaction's timestamp/height (SPV-only).
|
||||
|
||||
@ -1609,6 +1609,7 @@ TX.prototype.getPrevout = function getPrevout() {
|
||||
*/
|
||||
|
||||
TX.prototype.isWatched = function isWatched(filter) {
|
||||
var found = false;
|
||||
var i, input, output, hash, index, outpoint;
|
||||
|
||||
if (!filter)
|
||||
@ -1632,7 +1633,7 @@ TX.prototype.isWatched = function isWatched(filter) {
|
||||
|
||||
// 1. Test the tx hash
|
||||
if (filter.test(this.hash()))
|
||||
return true;
|
||||
found = true;
|
||||
|
||||
// 2. Test data elements in output scripts
|
||||
// (may need to update filter on match)
|
||||
@ -1649,10 +1650,13 @@ TX.prototype.isWatched = function isWatched(filter) {
|
||||
filter.add(outpoint);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
return found;
|
||||
|
||||
// 3. Test prev_out structure
|
||||
// 4. Test data elements in input scripts
|
||||
for (i = 0; i < this.inputs.length; i++) {
|
||||
@ -1670,8 +1674,8 @@ TX.prototype.isWatched = function isWatched(filter) {
|
||||
return true;
|
||||
|
||||
// Test the witness
|
||||
if (testScript(input.witness.items))
|
||||
return true;
|
||||
// if (testScript(input.witness.items))
|
||||
// return true;
|
||||
}
|
||||
|
||||
// 5. No match
|
||||
|
||||
Loading…
Reference in New Issue
Block a user