From 07ddae9d33c2daba3d7b1b87f4542967b2358fd2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 27 Jan 2016 19:43:24 -0800 Subject: [PATCH] misc fixes and improvements. --- lib/bcoin/input.js | 4 ++++ lib/bcoin/output.js | 4 ++++ lib/bcoin/peer.js | 16 ++++++++++++++++ lib/bcoin/pool.js | 28 +++++++++++++++++++++------- lib/bcoin/script.js | 7 ------- lib/bcoin/tx.js | 14 +++++++++++++- 6 files changed, 58 insertions(+), 15 deletions(-) diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 065e335d..c9b0a296 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -91,6 +91,10 @@ Input.prototype.__defineGetter__('redeem', function() { return this.data.redeem; }); +Input.prototype.__defineGetter__('scripthash', function() { + return this.data.scripthash; +}); + Input.prototype.__defineGetter__('scriptaddress', function() { return this.data.scriptaddress; }); diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 353d66ea..f8c17bde 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -89,6 +89,10 @@ Output.prototype.__defineGetter__('addresses', function() { return this.data.addresses || []; }); +Output.prototype.__defineGetter__('scripthash', function() { + return this.data.scripthash; +}); + Output.prototype.__defineGetter__('scriptaddress', function() { return this.data.scriptaddress; }); diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 8511d5aa..3b0f7b43 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -396,6 +396,9 @@ Peer.prototype._onPacket = function onPacket(packet) { if (cmd === 'getaddr') return this._handleGetAddr(); + if (cmd === 'reject') + return this._handleReject(payload); + if (cmd === 'merkleblock' || cmd === 'block') { payload.network = true; payload.relayedBy = this.host || '0.0.0.0'; @@ -574,6 +577,18 @@ Peer.prototype._handleHeaders = function handleHeaders(headers) { this.emit('headers', headers); }; +Peer.prototype._handleReject = function handleReject(payload) { + var hash = utils.toHex(payload.data); + var entry = this._broadcast.map[hash]; + + this.emit('reject', payload); + + if (!entry) + return; + + entry.e.emit('reject', payload); +}; + Peer.prototype.loadHeaders = function loadHeaders(hashes, stop) { utils.debug( 'Requesting headers packet from %s with getheaders', @@ -613,6 +628,7 @@ Peer.prototype.reject = function reject(details) { utils.debug( 'Sending reject packet to %s', this.host); + this._write(this.framer.reject(details)); }; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 24db968a..04a08ad4 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -27,7 +27,10 @@ function Pool(options) { EventEmitter.call(this); - this.options = options || {}; + if (!options) + options = {}; + + this.options = options; if (this.options.debug) bcoin.debug = this.options.debug; @@ -36,6 +39,12 @@ function Pool(options) { network.set(this.options.network); this.options.fullNode = !!this.options.fullNode; + + if (options.type === 'spv') + this.options.fullNode = false; + else if (options.type === 'full') + this.options.fullNode = true; + this.options.headers = this.options.headers; this.options.multiplePeers = this.options.multiplePeers; this.options.relay = this.options.relay == null @@ -45,7 +54,6 @@ function Pool(options) { this.originalSeeds = (options.seeds || network.seeds).map(utils.parseHost); this.setSeeds([]); - this.storage = this.options.storage; this.destroyed = false; this.size = options.size || 32; this.parallel = options.parallel || 2000; @@ -93,7 +101,6 @@ function Pool(options) { this.requestTimeout = options.requestTimeout || 10000; this.chain = new bcoin.chain({ - storage: this.storage, fullNode: this.options.fullNode, multiplePeers: this.options.multiplePeers }); @@ -143,10 +150,10 @@ function Pool(options) { map: {} }; - // Currently broadcasted TXs + // Currently broadcasted objects this.inv = { list: [], - timeout: options.txTimeout || 60000 + timeout: options.invTimeout || 60000 }; // Added and watched wallets @@ -191,7 +198,7 @@ Pool.prototype._init = function _init() { peer ? peer.host : '' ); - self.emit('reorg', [data.expected, data.received]); + self.emit('fork', [data.expected, data.received]); if (!peer) return; @@ -693,9 +700,12 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) { Pool.prototype._addIndex = function _addIndex(block, peer) { var added = this.chain.add(block, peer); + if (added === 0) + return false; + this.emit('chain-progress', this.chain.fillPercent(), peer); - return added > 0; + return true; }; Pool.prototype.isFull = function isFull() { @@ -883,6 +893,10 @@ Pool.prototype._addPeer = function _addPeer(backoff) { result[0].once('request', function() { entry.e.emit('ack', peer); }); + + result[0].once('reject', function(payload) { + entry.e.emit('reject', payload, peer); + }); }); self._scheduleRequests(); diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 66600367..f477d80e 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1269,13 +1269,6 @@ script.lockTime = function lockTime(s) { return script.num(lockTime, true); }; -script.spendable = function spendable(s, lockTime) { - if (script.lockTime(s) > lockTime) - return false; - - return true; -}; - script.getInputData = function getData(s, prev) { var output, type; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 4138c374..cf4f582b 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -28,7 +28,7 @@ function TX(data, block) { this.outputs = []; this.lock = data.lock || 0; this.ts = data.ts || 0; - this.block = null; + this.block = data.block || null; this._hash = null; this._raw = data._raw || null; @@ -62,6 +62,7 @@ function TX(data, block) { this.unspent = data.unspent || null; this.hardFee = data.hardFee || null; + this.subtractFee = data.subtractFee || null; this.changeAddress = data.changeAddress || null; this.changeIndex = data.changeIndex != null ? data.changeIndex : -1; @@ -922,6 +923,17 @@ TX.prototype.getUnspent = function getUnspent(unspent, address, fee) { value: new bn(0) }); + // if (this.subtractFee) { + // var f = new bn((Math.ceil(tx.maxSize() / 1024) - 1) * constants.tx.fee); + // for (var j = 0; j < this.outputs.length; j++) { + // if (this.outputs[j].value.cmp(f.addn(constants.tx.dust)) >= 0) { + // this.outputs[j].value = this.outputs[j].value.sub(f); + // break; + // } + // } + // total = tx.funds('out'); + // } + // Change fee value if it is more than 1024 // bytes (10000 satoshi for every 1024 bytes). do {