peer: abstract all deps.
This commit is contained in:
parent
6016d96202
commit
4692849593
@ -1347,7 +1347,7 @@ Chain.prototype.verifyCheckpoint = function verifyCheckpoint(prev, hash) {
|
||||
if (!this.options.useCheckpoints)
|
||||
return true;
|
||||
|
||||
checkpoint = this.network.checkpoints[height];
|
||||
checkpoint = this.network.checkpointMap[height];
|
||||
|
||||
if (!checkpoint)
|
||||
return true;
|
||||
|
||||
@ -18,14 +18,11 @@ var Bloom = require('../utils/bloom');
|
||||
var Address = require('../primitives/address');
|
||||
var Coin = require('../primitives/coin');
|
||||
var Script = require('../script/script');
|
||||
var Lock = require('../utils/lock');
|
||||
var Outpoint = require('../primitives/outpoint');
|
||||
var TX = require('../primitives/tx');
|
||||
var Coin = require('../primitives/coin');
|
||||
var TXMeta = require('../primitives/txmeta');
|
||||
var MempoolEntry = require('./mempoolentry');
|
||||
var CoinView = require('../coins/coinview');
|
||||
var Coins = require('../coins/coins');
|
||||
var Network = require('../protocol/network');
|
||||
var VerifyError = errors.VerifyError;
|
||||
var VerifyResult = errors.VerifyResult;
|
||||
|
||||
@ -7,28 +7,22 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var Network = require('../protocol/network');
|
||||
var crypto = require('../crypto/crypto');
|
||||
var assert = require('assert');
|
||||
|
||||
/**
|
||||
* Protocol packet framer
|
||||
* @exports Framer
|
||||
* @constructor
|
||||
* @param {Object} options
|
||||
* @param {Network} network
|
||||
*/
|
||||
|
||||
function Framer(options) {
|
||||
function Framer(network) {
|
||||
if (!(this instanceof Framer))
|
||||
return new Framer(options);
|
||||
return new Framer(network);
|
||||
|
||||
if (!options)
|
||||
options = {};
|
||||
|
||||
this.options = options;
|
||||
|
||||
this.network = Network.get(options.network);
|
||||
this.bip151 = options.bip151;
|
||||
this.network = Network.get(network);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -43,9 +37,6 @@ Framer.prototype.packet = function packet(cmd, payload, checksum) {
|
||||
|
||||
assert(payload, 'No payload.');
|
||||
|
||||
if (this.bip151 && this.bip151.handshake)
|
||||
return this.bip151.packet(cmd, payload);
|
||||
|
||||
assert(cmd.length < 12);
|
||||
assert(payload.length <= 0xffffffff);
|
||||
|
||||
|
||||
@ -7,11 +7,11 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var Network = require('../protocol/network');
|
||||
var assert = require('assert');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var Network = require('../protocol/network');
|
||||
var util = require('../utils/util');
|
||||
var crypto = require('../crypto/crypto');
|
||||
var assert = require('assert');
|
||||
var common = require('./common');
|
||||
var packets = require('./packets');
|
||||
|
||||
@ -19,56 +19,27 @@ var packets = require('./packets');
|
||||
* Protocol packet parser
|
||||
* @exports Parser
|
||||
* @constructor
|
||||
* @param {Object?} options
|
||||
* @param {Network} network
|
||||
* @emits Parser#error
|
||||
* @emits Parser#packet
|
||||
*/
|
||||
|
||||
function Parser(options) {
|
||||
function Parser(network) {
|
||||
if (!(this instanceof Parser))
|
||||
return new Parser(options);
|
||||
|
||||
if (!options)
|
||||
options = {};
|
||||
return new Parser(network);
|
||||
|
||||
EventEmitter.call(this);
|
||||
|
||||
this.network = Network.get(options.network);
|
||||
this.bip151 = options.bip151;
|
||||
this.network = Network.get(network);
|
||||
|
||||
this.pending = [];
|
||||
this.total = 0;
|
||||
this.waiting = 24;
|
||||
this.header = null;
|
||||
|
||||
this._init();
|
||||
}
|
||||
|
||||
util.inherits(Parser, EventEmitter);
|
||||
|
||||
/**
|
||||
* Initialize. Bind to events.
|
||||
* @private
|
||||
* @param {String} str
|
||||
*/
|
||||
|
||||
Parser.prototype._init = function _init(str) {
|
||||
var self = this;
|
||||
|
||||
if (!this.bip151)
|
||||
return;
|
||||
|
||||
this.bip151.on('packet', function(cmd, body) {
|
||||
var payload;
|
||||
try {
|
||||
payload = self.parsePayload(cmd, body);
|
||||
} catch (e) {
|
||||
return self.error(e);
|
||||
}
|
||||
self.emit('packet', payload);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit an error.
|
||||
* @private
|
||||
@ -88,9 +59,6 @@ Parser.prototype.error = function error() {
|
||||
Parser.prototype.feed = function feed(data) {
|
||||
var chunk, off, len;
|
||||
|
||||
if (this.bip151 && this.bip151.handshake)
|
||||
return this.bip151.feed(data);
|
||||
|
||||
this.total += data.length;
|
||||
this.pending.push(data);
|
||||
|
||||
@ -135,7 +103,8 @@ Parser.prototype.parse = function parse(data) {
|
||||
if (checksum !== this.header.checksum) {
|
||||
this.waiting = 24;
|
||||
this.header = null;
|
||||
return this.error('Invalid checksum: %d.', util.hex32(checksum));
|
||||
this.error('Invalid checksum: %d.', util.hex32(checksum));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
@ -179,7 +148,8 @@ Parser.prototype.parseHeader = function parseHeader(data) {
|
||||
|
||||
if (size > common.MAX_MESSAGE) {
|
||||
this.waiting = 24;
|
||||
return this.error('Packet length too large: %dmb.', util.mb(size));
|
||||
this.error('Packet length too large: %dmb.', util.mb(size));
|
||||
return;
|
||||
}
|
||||
|
||||
this.waiting = size;
|
||||
|
||||
989
lib/net/peer.js
989
lib/net/peer.js
File diff suppressed because it is too large
Load Diff
617
lib/net/pool.js
617
lib/net/pool.js
File diff suppressed because it is too large
Load Diff
@ -30,8 +30,9 @@ function Network(options) {
|
||||
this.seeds = options.seeds;
|
||||
this.magic = options.magic;
|
||||
this.port = options.port;
|
||||
this.checkpoints = options.checkpoints;
|
||||
this.checkpointMap = options.checkpointMap;
|
||||
this.lastCheckpoint = options.lastCheckpoint;
|
||||
this.checkpoints = [];
|
||||
this.halvingInterval = options.halvingInterval;
|
||||
this.genesis = options.genesis;
|
||||
this.genesisBlock = options.genesisBlock;
|
||||
@ -92,7 +93,7 @@ Network.simnet = null;
|
||||
|
||||
Network.prototype._init = function _init() {
|
||||
var bits = 0;
|
||||
var i, deployment;
|
||||
var i, deployment, keys, key, hash, height;
|
||||
|
||||
for (i = 0; i < this.deploys.length; i++) {
|
||||
deployment = this.deploys[i];
|
||||
@ -102,6 +103,18 @@ Network.prototype._init = function _init() {
|
||||
bits |= consensus.VERSION_TOP_MASK;
|
||||
|
||||
this.unknownBits = ~bits;
|
||||
|
||||
keys = Object.keys(this.checkpointMap);
|
||||
|
||||
for (i = 0; i < keys.length; i++) {
|
||||
key = keys[i];
|
||||
hash = this.checkpointMap[key];
|
||||
height = +key;
|
||||
|
||||
this.checkpoints.push({ hash: hash, height: height });
|
||||
}
|
||||
|
||||
this.checkpoints.sort(cmpNode);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -298,6 +311,10 @@ function cmpBit(a, b) {
|
||||
return a.bit - b;
|
||||
}
|
||||
|
||||
function cmpNode(a, b) {
|
||||
return a.height - b.height;
|
||||
}
|
||||
|
||||
/*
|
||||
* Expose
|
||||
*/
|
||||
|
||||
@ -77,7 +77,7 @@ main.port = 8333;
|
||||
* @const {Object}
|
||||
*/
|
||||
|
||||
main.checkpoints = {
|
||||
main.checkpointMap = {
|
||||
11111: '1d7c6eb2fd42f55925e92efad68b61edd22fba29fde8783df744e26900000000',
|
||||
33333: 'a6d0b5df7d0df069ceb1e736a216ad187a50b07aaa4e78748a58d52d00000000',
|
||||
74000: '201a66b853f9e7814a820e2af5f5dc79c07144e31ce4c9a39339570000000000',
|
||||
@ -497,7 +497,7 @@ testnet.magic = 0x0709110b;
|
||||
|
||||
testnet.port = 18333;
|
||||
|
||||
testnet.checkpoints = {
|
||||
testnet.checkpointMap = {
|
||||
546: '70cb6af7ebbcb1315d3414029c556c55f3e2fc353c4c9063a76c932a00000000',
|
||||
// Custom checkpoints
|
||||
10000: '02a1b43f52591e53b660069173ac83b675798e12599dbb0442b7580000000000',
|
||||
@ -668,7 +668,7 @@ regtest.magic = 0xdab5bffa;
|
||||
|
||||
regtest.port = 48444;
|
||||
|
||||
regtest.checkpoints = {};
|
||||
regtest.checkpointMap = {};
|
||||
regtest.lastCheckpoint = 0;
|
||||
|
||||
regtest.halvingInterval = 150;
|
||||
@ -819,7 +819,7 @@ segnet4.magic = 0xc4a1abdc;
|
||||
|
||||
segnet4.port = 28901;
|
||||
|
||||
segnet4.checkpoints = {};
|
||||
segnet4.checkpointMap = {};
|
||||
segnet4.lastCheckpoint = 0;
|
||||
|
||||
segnet4.halvingInterval = 210000;
|
||||
@ -963,7 +963,7 @@ simnet.magic = 0x12141c16;
|
||||
|
||||
simnet.port = 18555;
|
||||
|
||||
simnet.checkpoints = {};
|
||||
simnet.checkpointMap = {};
|
||||
|
||||
simnet.lastCheckpoint = 0;
|
||||
|
||||
|
||||
315
lib/utils/asyncemitter.js
Normal file
315
lib/utils/asyncemitter.js
Normal file
@ -0,0 +1,315 @@
|
||||
/*!
|
||||
* asyncemitter.js - event emitter which resolves promises.
|
||||
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
|
||||
* https://github.com/bcoin-org/bcoin
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var co = require('./co');
|
||||
|
||||
/**
|
||||
* Represents a promise-resolving event emitter.
|
||||
* @see EventEmitter
|
||||
* @constructor
|
||||
*/
|
||||
|
||||
function AsyncEmitter() {
|
||||
if (!(this instanceof AsyncEmitter))
|
||||
return new AsyncEmitter();
|
||||
|
||||
this._events = Object.create(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a listener.
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.addListener = function addListener(type, handler) {
|
||||
return this._push(type, handler, false);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a listener.
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.on = function on(type, handler) {
|
||||
return this.addListener(type, handler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a listener to execute once.
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.once = function once(type, handler) {
|
||||
return this._push(type, handler, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend a listener.
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.prependListener = function prependListener(type, handler) {
|
||||
return this._unshift(type, handler, false);
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepend a listener to execute once.
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.prependOnceListener = function prependOnceListener(type, handler) {
|
||||
return this._unshift(type, handler, true);
|
||||
};
|
||||
|
||||
/**
|
||||
* Push a listener.
|
||||
* @private
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
* @param {Boolean} once
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype._push = function _push(type, handler, once) {
|
||||
assert(typeof type === 'string', '`type` must be a string.');
|
||||
|
||||
if (!this._events[type])
|
||||
this._events[type] = [];
|
||||
|
||||
this._events[type].push(new Listener(handler, once));
|
||||
|
||||
this.tryEmit('newListener', type, handler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Unshift a listener.
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
* @param {Boolean} once
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype._unshift = function _unshift(type, handler, once) {
|
||||
assert(typeof type === 'string', '`type` must be a string.');
|
||||
|
||||
if (!this._events[type])
|
||||
this._events[type] = [];
|
||||
|
||||
this._events[type].unshift(new Listener(handler, once));
|
||||
|
||||
this.tryEmit('newListener', type, handler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove a listener.
|
||||
* @param {String} type
|
||||
* @param {Function} handler
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.removeListener = function removeListener(type, handler) {
|
||||
var i, listeners, listener;
|
||||
var index = -1;
|
||||
|
||||
assert(typeof type === 'string', '`type` must be a string.');
|
||||
|
||||
listeners = this._events[type];
|
||||
|
||||
if (!listeners)
|
||||
return;
|
||||
|
||||
for (i = 0; i < listeners.length; i++) {
|
||||
listener = listeners[i];
|
||||
if (listener.handler === handler) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index === -1)
|
||||
return;
|
||||
|
||||
listeners.splice(index, 1);
|
||||
|
||||
if (listeners.length === 0)
|
||||
delete this._events[type];
|
||||
|
||||
this.tryEmit('removeListener', type, handler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set max listeners.
|
||||
* @param {Number} max
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.setMaxListeners = function setMaxListeners(max) {
|
||||
assert(typeof max === 'number', '`max` must be a number.');
|
||||
assert(max >= 0, '`max` must be non-negative.');
|
||||
assert(max % 1 === 0, '`max` must be an integer.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove all listeners.
|
||||
* @param {String?} type
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.removeAllListeners = function removeAllListeners(type) {
|
||||
if (arguments.length === 0) {
|
||||
this._events = Object.create(null);
|
||||
return;
|
||||
}
|
||||
|
||||
assert(typeof type === 'string', '`type` must be a string.');
|
||||
|
||||
delete this._events[type];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get listeners array.
|
||||
* @param {String} type
|
||||
* @returns {Function[]}
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.listeners = function listeners(type) {
|
||||
var i, listeners, listener;
|
||||
var result = [];
|
||||
|
||||
assert(typeof type === 'string', '`type` must be a string.');
|
||||
|
||||
listeners = this._events[type];
|
||||
|
||||
if (!listeners)
|
||||
return result;
|
||||
|
||||
for (i = 0; i < listeners.length; i++) {
|
||||
listener = listeners[i];
|
||||
result.push(listener.handler);
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get listener count for an event.
|
||||
* @param {String} type
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.listenerCount = function listenerCount(type) {
|
||||
var listeners;
|
||||
|
||||
assert(typeof type === 'string', '`type` must be a string.');
|
||||
|
||||
listeners = this._events[type];
|
||||
|
||||
if (!listeners)
|
||||
return 0;
|
||||
|
||||
return listeners.length;
|
||||
};
|
||||
|
||||
/**
|
||||
* Emit an event. Wait for promises to resolve.
|
||||
* @param {String} type
|
||||
* @param {...Object} args
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.emit = co(function* emit(type) {
|
||||
var i, j, listeners, error, err, args, listener, handler;
|
||||
|
||||
assert(typeof type === 'string', '`type` must be a string.');
|
||||
|
||||
listeners = this._events[type];
|
||||
|
||||
if (!listeners || listeners.length === 0) {
|
||||
if (type === 'error') {
|
||||
error = arguments[1];
|
||||
|
||||
if (error instanceof Error)
|
||||
throw error;
|
||||
|
||||
err = new Error('Uncaught, unspecified "error" event. (' + error + ')');
|
||||
err.context = error;
|
||||
throw err;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < listeners.length; i++) {
|
||||
listener = listeners[i];
|
||||
handler = listener.handler;
|
||||
|
||||
if (listener.once) {
|
||||
listeners.splice(i, 1);
|
||||
i--;
|
||||
}
|
||||
|
||||
switch (arguments.length) {
|
||||
case 1:
|
||||
yield handler();
|
||||
break;
|
||||
case 2:
|
||||
yield handler(arguments[1]);
|
||||
break;
|
||||
case 3:
|
||||
yield handler(arguments[1], arguments[2]);
|
||||
break;
|
||||
case 4:
|
||||
yield handler(arguments[1], arguments[2], arguments[3]);
|
||||
break;
|
||||
default:
|
||||
if (!args) {
|
||||
args = new Array(arguments.length - 1);
|
||||
for (j = 1; j < arguments.length; j++)
|
||||
args[j - 1] = arguments[j];
|
||||
}
|
||||
yield handler.apply(null, args);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Emit an event. Ignore rejections.
|
||||
* @param {String} type
|
||||
* @param {...Object} args
|
||||
* @returns {Promise}
|
||||
*/
|
||||
|
||||
AsyncEmitter.prototype.tryEmit = co(function* tryEmit() {
|
||||
try {
|
||||
yield this.emit.apply(this, arguments);
|
||||
} catch (e) {
|
||||
;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Event Listener
|
||||
* @constructor
|
||||
* @param {Function} handler
|
||||
* @param {Boolean} once
|
||||
* @property {Function} handler
|
||||
* @property {Boolean} once
|
||||
*/
|
||||
|
||||
function Listener(handler, once) {
|
||||
assert(typeof handler === 'function', '`handler` must be a function.');
|
||||
assert(typeof once === 'boolean', '`once` must be a function.');
|
||||
this.handler = handler;
|
||||
this.once = once;
|
||||
}
|
||||
|
||||
/*
|
||||
* Expose
|
||||
*/
|
||||
|
||||
module.exports = AsyncEmitter;
|
||||
Loading…
Reference in New Issue
Block a user