net: redesign stall behavior and block management.

This commit is contained in:
Christopher Jeffrey 2017-01-04 17:31:32 -08:00
parent eb12b0e608
commit cd8e464079
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 774 additions and 913 deletions

View File

@ -11,7 +11,6 @@ var BufferReader = require('../utils/reader');
var BufferWriter = require('../utils/writer'); var BufferWriter = require('../utils/writer');
var StaticWriter = require('../utils/staticwriter'); var StaticWriter = require('../utils/staticwriter');
var encoding = require('../utils/encoding'); var encoding = require('../utils/encoding');
var co = require('../utils/co');
var crypto = require('../crypto/crypto'); var crypto = require('../crypto/crypto');
var assert = require('assert'); var assert = require('assert');
var constants = require('../protocol/constants'); var constants = require('../protocol/constants');
@ -45,8 +44,6 @@ function CompactBlock(options) {
this.idMap = {}; this.idMap = {};
this.count = 0; this.count = 0;
this.sipKey = null; this.sipKey = null;
this.timeout = null;
this.callback = null;
if (options) if (options)
this.fromOptions(options); this.fromOptions(options);
@ -404,37 +401,6 @@ CompactBlock.fromBlock = function fromBlock(block, witness, nonce) {
return new CompactBlock().fromBlock(block, witness, nonce); return new CompactBlock().fromBlock(block, witness, nonce);
}; };
CompactBlock.prototype.wait = function wait(time) {
var self = this;
return new Promise(function(resolve, reject) {
self._wait(time, co.wrap(resolve, reject));
});
};
CompactBlock.prototype._wait = function wait(time, callback) {
var self = this;
assert(this.timeout == null);
this.callback = callback;
this.timeout = setTimeout(function() {
self.complete(new Error('Timed out.'));
}, time);
};
CompactBlock.prototype.complete = function complete(err) {
if (this.timeout != null) {
clearTimeout(this.timeout);
this.timeout = null;
this.callback(err);
}
};
CompactBlock.prototype.destroy = function destroy() {
if (this.timeout != null) {
clearTimeout(this.timeout);
this.timeout = null;
}
};
CompactBlock.prototype.toHeaders = function toHeaders() { CompactBlock.prototype.toHeaders = function toHeaders() {
return Headers.fromBlock(this); return Headers.fromBlock(this);
}; };

View File

@ -37,42 +37,53 @@ var DUMMY = new Buffer(0);
exports.types = { exports.types = {
VERSION: 0, VERSION: 0,
VERACK: 1, VERACK: 1,
PING: 1, PING: 2,
PONG: 2, PONG: 3,
ALERT: 3, ALERT: 4,
GETADDR: 4, GETADDR: 5,
ADDR: 5, ADDR: 6,
INV: 6, INV: 7,
GETDATA: 7, GETDATA: 8,
NOTFOUND: 8, NOTFOUND: 9,
GETBLOCKS: 9, GETBLOCKS: 10,
GETHEADERS: 10, GETHEADERS: 11,
HEADERS: 11, HEADERS: 12,
SENDHEADERS: 12, SENDHEADERS: 13,
BLOCK: 13, BLOCK: 14,
TX: 14, TX: 15,
REJECT: 15, REJECT: 16,
MEMPOOL: 16, MEMPOOL: 17,
FILTERLOAD: 17, FILTERLOAD: 18,
FILTERADD: 18, FILTERADD: 19,
FILTERCLEAR: 19, FILTERCLEAR: 20,
MERKLEBLOCK: 20, MERKLEBLOCK: 21,
GETUTXOS: 21, GETUTXOS: 22,
UTXOS: 22, UTXOS: 23,
HAVEWITNESS: 23, HAVEWITNESS: 24,
FEEFILTER: 24, FEEFILTER: 25,
SENDCMPCT: 25, SENDCMPCT: 26,
CMPCTBLOCK: 26, CMPCTBLOCK: 27,
GETBLOCKTXN: 27, GETBLOCKTXN: 28,
BLOCKTXN: 28, BLOCKTXN: 29,
ENCINIT: 29, ENCINIT: 30,
ENCACK: 30, ENCACK: 31,
AUTHCHALLENGE: 31, AUTHCHALLENGE: 32,
AUTHREPLY: 32, AUTHREPLY: 33,
AUTHPROPOSE: 33, AUTHPROPOSE: 34,
UNKNOWN: 34 UNKNOWN: 35,
// Internal
INTERNAL: 100,
DATA: 101
}; };
/**
* Packet types by value.
* @const {Object}
* @default
*/
exports.typesByVal = util.revMap(exports.types);
/** /**
* Base Packet * Base Packet
* @constructor * @constructor
@ -141,7 +152,7 @@ Packet.prototype.fromRaw = function fromRaw(data) {
* @param {Buffer} options.nonce * @param {Buffer} options.nonce
* @param {String} options.agent - User agent string. * @param {String} options.agent - User agent string.
* @param {Number} options.height - Chain height. * @param {Number} options.height - Chain height.
* @param {Boolean} options.relay - Whether transactions * @param {Boolean} options.noRelay - Whether transactions
* should be relayed immediately. * should be relayed immediately.
* @property {Number} version - Protocol version. * @property {Number} version - Protocol version.
* @property {Number} services - Service bits. * @property {Number} services - Service bits.
@ -151,7 +162,7 @@ Packet.prototype.fromRaw = function fromRaw(data) {
* @property {Buffer} nonce * @property {Buffer} nonce
* @property {String} agent - User agent string. * @property {String} agent - User agent string.
* @property {Number} height - Chain height. * @property {Number} height - Chain height.
* @property {Boolean} relay - Whether transactions * @property {Boolean} noRelay - Whether transactions
* should be relayed immediately. * should be relayed immediately.
*/ */
@ -169,7 +180,7 @@ function VersionPacket(options) {
this.nonce = constants.ZERO_U64; this.nonce = constants.ZERO_U64;
this.agent = constants.USER_AGENT; this.agent = constants.USER_AGENT;
this.height = 0; this.height = 0;
this.relay = true; this.noRelay = false;
if (options) if (options)
this.fromOptions(options); this.fromOptions(options);
@ -211,8 +222,8 @@ VersionPacket.prototype.fromOptions = function fromOptions(options) {
if (options.height != null) if (options.height != null)
this.height = options.height; this.height = options.height;
if (options.relay != null) if (options.noRelay != null)
this.relay = options.relay; this.noRelay = options.noRelay;
return this; return this;
}; };
@ -258,7 +269,7 @@ VersionPacket.prototype.toWriter = function toWriter(bw) {
bw.writeBytes(this.nonce); bw.writeBytes(this.nonce);
bw.writeVarString(this.agent, 'ascii'); bw.writeVarString(this.agent, 'ascii');
bw.write32(this.height); bw.write32(this.height);
bw.writeU8(this.relay ? 1 : 0); bw.writeU8(this.noRelay ? 0 : 1);
return bw; return bw;
}; };
@ -366,7 +377,7 @@ VersionPacket.prototype.fromReader = function fromReader(br) {
this.height = br.read32(); this.height = br.read32();
if (br.left() > 0) if (br.left() > 0)
this.relay = br.readU8() === 1; this.noRelay = br.readU8() === 0;
if (this.version === 10300) if (this.version === 10300)
this.version = 300; this.version = 300;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -142,6 +142,15 @@ InvItem.prototype.hasWitness = function hasWitness() {
return (this.type & constants.WITNESS_MASK) !== 0; return (this.type & constants.WITNESS_MASK) !== 0;
}; };
/**
* Get little-endian hash.
* @returns {Hash}
*/
InvItem.prototype.rhash = function() {
return util.revHex(this.hash);
};
/* /*
* Expose * Expose
*/ */

View File

@ -56,6 +56,7 @@
"browserify": "13.1.0", "browserify": "13.1.0",
"hash.js": "1.0.3", "hash.js": "1.0.3",
"jsdoc": "3.4.0", "jsdoc": "3.4.0",
"jshint": "2.9.4",
"level-js": "2.2.4", "level-js": "2.2.4",
"mocha": "3.0.2", "mocha": "3.0.2",
"uglify-js": "2.7.3" "uglify-js": "2.7.3"