misc fixes and improvements.

This commit is contained in:
Christopher Jeffrey 2016-01-27 19:43:24 -08:00
parent 4cc9b93fdd
commit 07ddae9d33
6 changed files with 58 additions and 15 deletions

View File

@ -91,6 +91,10 @@ Input.prototype.__defineGetter__('redeem', function() {
return this.data.redeem; return this.data.redeem;
}); });
Input.prototype.__defineGetter__('scripthash', function() {
return this.data.scripthash;
});
Input.prototype.__defineGetter__('scriptaddress', function() { Input.prototype.__defineGetter__('scriptaddress', function() {
return this.data.scriptaddress; return this.data.scriptaddress;
}); });

View File

@ -89,6 +89,10 @@ Output.prototype.__defineGetter__('addresses', function() {
return this.data.addresses || []; return this.data.addresses || [];
}); });
Output.prototype.__defineGetter__('scripthash', function() {
return this.data.scripthash;
});
Output.prototype.__defineGetter__('scriptaddress', function() { Output.prototype.__defineGetter__('scriptaddress', function() {
return this.data.scriptaddress; return this.data.scriptaddress;
}); });

View File

@ -396,6 +396,9 @@ Peer.prototype._onPacket = function onPacket(packet) {
if (cmd === 'getaddr') if (cmd === 'getaddr')
return this._handleGetAddr(); return this._handleGetAddr();
if (cmd === 'reject')
return this._handleReject(payload);
if (cmd === 'merkleblock' || cmd === 'block') { if (cmd === 'merkleblock' || cmd === 'block') {
payload.network = true; payload.network = true;
payload.relayedBy = this.host || '0.0.0.0'; payload.relayedBy = this.host || '0.0.0.0';
@ -574,6 +577,18 @@ Peer.prototype._handleHeaders = function handleHeaders(headers) {
this.emit('headers', 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) { Peer.prototype.loadHeaders = function loadHeaders(hashes, stop) {
utils.debug( utils.debug(
'Requesting headers packet from %s with getheaders', 'Requesting headers packet from %s with getheaders',
@ -613,6 +628,7 @@ Peer.prototype.reject = function reject(details) {
utils.debug( utils.debug(
'Sending reject packet to %s', 'Sending reject packet to %s',
this.host); this.host);
this._write(this.framer.reject(details)); this._write(this.framer.reject(details));
}; };

View File

@ -27,7 +27,10 @@ function Pool(options) {
EventEmitter.call(this); EventEmitter.call(this);
this.options = options || {}; if (!options)
options = {};
this.options = options;
if (this.options.debug) if (this.options.debug)
bcoin.debug = this.options.debug; bcoin.debug = this.options.debug;
@ -36,6 +39,12 @@ function Pool(options) {
network.set(this.options.network); network.set(this.options.network);
this.options.fullNode = !!this.options.fullNode; 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.headers = this.options.headers;
this.options.multiplePeers = this.options.multiplePeers; this.options.multiplePeers = this.options.multiplePeers;
this.options.relay = this.options.relay == null this.options.relay = this.options.relay == null
@ -45,7 +54,6 @@ function Pool(options) {
this.originalSeeds = (options.seeds || network.seeds).map(utils.parseHost); this.originalSeeds = (options.seeds || network.seeds).map(utils.parseHost);
this.setSeeds([]); this.setSeeds([]);
this.storage = this.options.storage;
this.destroyed = false; this.destroyed = false;
this.size = options.size || 32; this.size = options.size || 32;
this.parallel = options.parallel || 2000; this.parallel = options.parallel || 2000;
@ -93,7 +101,6 @@ function Pool(options) {
this.requestTimeout = options.requestTimeout || 10000; this.requestTimeout = options.requestTimeout || 10000;
this.chain = new bcoin.chain({ this.chain = new bcoin.chain({
storage: this.storage,
fullNode: this.options.fullNode, fullNode: this.options.fullNode,
multiplePeers: this.options.multiplePeers multiplePeers: this.options.multiplePeers
}); });
@ -143,10 +150,10 @@ function Pool(options) {
map: {} map: {}
}; };
// Currently broadcasted TXs // Currently broadcasted objects
this.inv = { this.inv = {
list: [], list: [],
timeout: options.txTimeout || 60000 timeout: options.invTimeout || 60000
}; };
// Added and watched wallets // Added and watched wallets
@ -191,7 +198,7 @@ Pool.prototype._init = function _init() {
peer ? peer.host : '' peer ? peer.host : ''
); );
self.emit('reorg', [data.expected, data.received]); self.emit('fork', [data.expected, data.received]);
if (!peer) if (!peer)
return; return;
@ -693,9 +700,12 @@ Pool.prototype._handleBlock = function _handleBlock(block, peer) {
Pool.prototype._addIndex = function _addIndex(block, peer) { Pool.prototype._addIndex = function _addIndex(block, peer) {
var added = this.chain.add(block, peer); var added = this.chain.add(block, peer);
if (added === 0)
return false;
this.emit('chain-progress', this.chain.fillPercent(), peer); this.emit('chain-progress', this.chain.fillPercent(), peer);
return added > 0; return true;
}; };
Pool.prototype.isFull = function isFull() { Pool.prototype.isFull = function isFull() {
@ -883,6 +893,10 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
result[0].once('request', function() { result[0].once('request', function() {
entry.e.emit('ack', peer); entry.e.emit('ack', peer);
}); });
result[0].once('reject', function(payload) {
entry.e.emit('reject', payload, peer);
});
}); });
self._scheduleRequests(); self._scheduleRequests();

View File

@ -1269,13 +1269,6 @@ script.lockTime = function lockTime(s) {
return script.num(lockTime, true); 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) { script.getInputData = function getData(s, prev) {
var output, type; var output, type;

View File

@ -28,7 +28,7 @@ function TX(data, block) {
this.outputs = []; this.outputs = [];
this.lock = data.lock || 0; this.lock = data.lock || 0;
this.ts = data.ts || 0; this.ts = data.ts || 0;
this.block = null; this.block = data.block || null;
this._hash = null; this._hash = null;
this._raw = data._raw || null; this._raw = data._raw || null;
@ -62,6 +62,7 @@ function TX(data, block) {
this.unspent = data.unspent || null; this.unspent = data.unspent || null;
this.hardFee = data.hardFee || null; this.hardFee = data.hardFee || null;
this.subtractFee = data.subtractFee || null;
this.changeAddress = data.changeAddress || null; this.changeAddress = data.changeAddress || null;
this.changeIndex = data.changeIndex != null ? data.changeIndex : -1; this.changeIndex = data.changeIndex != null ? data.changeIndex : -1;
@ -922,6 +923,17 @@ TX.prototype.getUnspent = function getUnspent(unspent, address, fee) {
value: new bn(0) 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 // Change fee value if it is more than 1024
// bytes (10000 satoshi for every 1024 bytes). // bytes (10000 satoshi for every 1024 bytes).
do { do {