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 StaticWriter = require('../utils/staticwriter');
var encoding = require('../utils/encoding');
var co = require('../utils/co');
var crypto = require('../crypto/crypto');
var assert = require('assert');
var constants = require('../protocol/constants');
@ -45,8 +44,6 @@ function CompactBlock(options) {
this.idMap = {};
this.count = 0;
this.sipKey = null;
this.timeout = null;
this.callback = null;
if (options)
this.fromOptions(options);
@ -404,37 +401,6 @@ CompactBlock.fromBlock = function 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() {
return Headers.fromBlock(this);
};

View File

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

View File

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