bcoin: cleanup. fix bsock calls.

This commit is contained in:
Christopher Jeffrey 2017-10-31 20:08:21 -07:00
parent 401829db5a
commit cbef19047a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 41 additions and 57 deletions

View File

@ -61,7 +61,7 @@ WSProxy.prototype.handleSocket = function handleSocket(ws) {
this.emit('error', err);
});
ws.listen('tcp connect', (port, host, nonce) => {
ws.bind('tcp connect', (port, host, nonce) => {
this.handleConnect(ws, port, host, nonce);
});
};
@ -184,33 +184,33 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce)
ws.destroy();
});
ws.listen('tcp data', (data) => {
ws.bind('tcp data', (data) => {
if (typeof data !== 'string')
return;
socket.write(Buffer.from(data, 'hex'));
});
ws.listen('tcp keep alive', (enable, delay) => {
ws.bind('tcp keep alive', (enable, delay) => {
socket.setKeepAlive(enable, delay);
});
ws.listen('tcp no delay', (enable) => {
ws.bind('tcp no delay', (enable) => {
socket.setNoDelay(enable);
});
ws.listen('tcp set timeout', (timeout) => {
ws.bind('tcp set timeout', (timeout) => {
socket.setTimeout(timeout);
});
ws.listen('tcp pause', () => {
ws.bind('tcp pause', () => {
socket.pause();
});
ws.listen('tcp resume', () => {
ws.bind('tcp resume', () => {
socket.resume();
});
ws.listen('disconnect', () => {
ws.bind('disconnect', () => {
socket.destroy();
});
};
@ -229,7 +229,7 @@ function SocketState(server, socket) {
this.target = server.target;
this.snonce = nonce();
this.socket = null;
this.host = IP.normalize(socket.conn.remoteAddress);
this.host = socket.host;
this.remoteHost = null;
}

View File

@ -12,18 +12,18 @@ const assert = require('assert');
const path = require('path');
const EventEmitter = require('events');
const fs = require('bfile');
const co = require('../utils/co');
const IP = require('binet');
const Logger = require('blgr');
const ccmp = require('bcrypto/lib/ccmp');
const digest = require('bcrypto/lib/digest');
const random = require('bcrypto/lib/random');
const ccmp = require('bcrypto/lib/ccmp');
const packets = require('./packets');
const secp256k1 = require('bcrypto/lib/secp256k1');
const StaticWriter = require('bbuf/lib/staticwriter');
const base58 = require('bstr/lib/base58');
const encoding = require('bbuf/lib/encoding');
const IP = require('binet');
const base58 = require('bstr/lib/base58');
const co = require('../utils/co');
const packets = require('./packets');
const dns = require('./dns');
const Logger = require('blgr');
/**
* Represents a BIP150 input/output stream.

View File

@ -38,7 +38,7 @@ function ProxySocket(uri) {
Object.setPrototypeOf(ProxySocket.prototype, EventEmitter.prototype);
ProxySocket.prototype._init = function _init() {
this.socket.listen('info', (info) => {
this.socket.bind('info', (info) => {
if (this.closed)
return;
@ -56,7 +56,7 @@ ProxySocket.prototype._init = function _init() {
console.error(err);
});
this.socket.listen('tcp connect', (addr, port) => {
this.socket.bind('tcp connect', (addr, port) => {
if (this.closed)
return;
this.remoteAddress = addr;
@ -64,7 +64,7 @@ ProxySocket.prototype._init = function _init() {
this.emit('connect');
});
this.socket.listen('tcp data', (data) => {
this.socket.bind('tcp data', (data) => {
data = Buffer.from(data, 'hex');
if (this.paused) {
this.recvBuffer.push(data);
@ -74,24 +74,24 @@ ProxySocket.prototype._init = function _init() {
this.emit('data', data);
});
this.socket.listen('tcp close', (data) => {
this.socket.bind('tcp close', (data) => {
if (this.closed)
return;
this.closed = true;
this.emit('close');
});
this.socket.listen('tcp error', (e) => {
this.socket.bind('tcp error', (e) => {
const err = new Error(e.message);
err.code = e.code;
this.emit('error', err);
});
this.socket.listen('tcp timeout', () => {
this.socket.bind('tcp timeout', () => {
this.emit('timeout');
});
this.socket.listen('disconnect', () => {
this.socket.bind('disconnect', () => {
if (this.closed)
return;
this.closed = true;

View File

@ -610,7 +610,7 @@ Proxy.prototype.connect = async function connect(port, host) {
});
for (const op of this.ops)
op.call(this);
op();
this.ops.length = 0;

View File

@ -8,13 +8,12 @@
'use strict';
const assert = require('assert');
const AsyncObject = require('../utils/asyncobject');
const util = require('../utils/util');
const Network = require('../protocol/network');
const fs = require('bfile');
const Logger = require('blgr');
const WorkerPool = require('../workers/workerpool');
const secp256k1 = require('bcrypto/lib/secp256k1');
const Config = require('bcfg');
const Network = require('../protocol/network');
const AsyncObject = require('../utils/asyncobject');
const WorkerPool = require('../workers/workerpool');
/**
* Base class from which every other
@ -141,8 +140,11 @@ Node.prototype.init = function init(file) {
* @returns {Promise}
*/
Node.prototype.ensure = function ensure() {
return this.config.ensure();
Node.prototype.ensure = async function ensure() {
if (fs.unsupported)
return undefined;
return fs.mkdirp(this.config.prefix);
};
/**
@ -186,7 +188,7 @@ Node.prototype.handlePreopen = async function handlePreopen() {
*/
Node.prototype.handleOpen = async function handleOpen() {
this.startTime = util.now();
this.startTime = Date.now();
if (!this.workers.enabled) {
this.logger.warning('Warning: worker pool is disabled.');
@ -252,7 +254,7 @@ Node.prototype.uptime = function uptime() {
if (this.startTime === -1)
return 0;
return util.now() - this.startTime;
return Math.floor((Date.now() - this.startTime) / 1000);
};
/**

View File

@ -749,7 +749,13 @@ class RPC extends RPCBase {
if (fs.unsupported)
throw new RPCError(errs.INTERNAL_ERROR, 'FS not available.');
const data = await fs.readFile(file, 'utf8');
let data;
try {
data = await fs.readFile(file, 'utf8');
} catch (e) {
throw new RPCError(errs.INTERNAL_ERROR, e.code || '');
}
const lines = data.split(/\n+/);
const keys = [];

View File

@ -27,7 +27,6 @@
'use strict';
const assert = require('./util/assert');
const bech32 = require('bstr/lib/bech32');
const Address = require('../lib/primitives/address');
const validAddresses = [
@ -94,29 +93,6 @@ const invalidAddresses = [
'tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv'
];
function fromAddress(hrp, addr) {
const dec = bech32.decode(addr);
if (dec.hrp !== hrp)
throw new Error('Invalid bech32 prefix or data length.');
if (dec.version === 0 && dec.hash.length !== 20 && dec.hash.length !== 32)
throw new Error('Malformed witness program.');
return {
version: dec.version,
program: dec.hash
};
}
function toAddress(hrp, version, program) {
const ret = bech32.encode(hrp, version, program);
fromAddress(hrp, ret);
return ret;
}
function createProgram(version, program) {
const data = Buffer.allocUnsafe(2 + program.length);
data[0] = version ? version + 0x80 : 0;