Browse Source

rework logging, remove some unused code

patch-2
Stephen Pair 11 years ago
parent
commit
f9e86a5e83
  1. 18
      Connection.js
  2. 4
      PeerManager.js
  3. 16
      RpcClient.js
  4. 32
      Script.js
  5. 2
      ScriptInterpreter.js
  6. 2
      Transaction.js
  7. 10
      util/EncodedData.js
  8. 12
      util/log.js
  9. 45
      util/util.js

18
Connection.js

@ -2,7 +2,7 @@ require('classtool');
function spec(b) { function spec(b) {
var config = b.config || require('./config'); var config = b.config || require('./config');
var log = b.log || require('./util/log')(config); var log = b.log || require('./util/log');
var network = b.network || require('./networks')[config.network]; var network = b.network || require('./networks')[config.network];
var MAX_RECEIVE_BUFFER = 10000000; var MAX_RECEIVE_BUFFER = 10000000;
@ -152,18 +152,18 @@ function spec(b) {
} }
break; break;
} }
this.emit(message.command, {
conn: this,
socket: this.socket,
peer: this.peer,
message: message
});
} catch (e) { } catch (e) {
log.err('Error while handling message '+message.command+' from ' + log.err('Error while handling "'+message.command+'" message from ' +
this.peer + ':\n' + this.peer + ':\n' +
(e.stack ? e.stack : e.toString())); (e.stack ? e.stack : e.toString()));
return;
} }
this.emit(message.command, {
conn: this,
socket: this.socket,
peer: this.peer,
message: message
});
}; };
Connection.prototype.sendPong = function (nonce) { Connection.prototype.sendPong = function (nonce) {

4
PeerManager.js

@ -2,7 +2,7 @@ require('classtool');
function spec(b) { function spec(b) {
var config = b.config || require('./config'); var config = b.config || require('./config');
var log = b.log || require('./util/log')(config); var log = b.log || require('./util/log');
var network = b.network || require('./networks')[config.network]; var network = b.network || require('./networks')[config.network];
var Connection = b.Connection || require('./Connection').createClass({config: config}); var Connection = b.Connection || require('./Connection').createClass({config: config});
var Peer = b.Peer || require('./Peer').class(); var Peer = b.Peer || require('./Peer').class();
@ -166,7 +166,7 @@ function spec(b) {
}; };
PeerManager.prototype.handleError = function(e) { PeerManager.prototype.handleError = function(e) {
log.err(e.peer, e.err); log.err('unkown error with peer '+e.peer+' (disconnecting): '+e.err);
this.handleDisconnect.apply(this, [].slice.call(arguments)); this.handleDisconnect.apply(this, [].slice.call(arguments));
}; };

16
RpcClient.js

@ -6,7 +6,7 @@ require('classtool');
function ClassSpec(b) { function ClassSpec(b) {
var http = b.http || require('http'); var http = b.http || require('http');
var https = b.https || require('https'); var https = b.https || require('https');
var log = b.log || {err: function(){}}; var log = b.log || require('./util/log');
function RpcClient(opts) { function RpcClient(opts) {
opts = opts || {}; opts = opts || {};
@ -64,7 +64,7 @@ function ClassSpec(b) {
help: '', help: '',
importAddress: 'str str bool', importAddress: 'str str bool',
importPrivKey: 'str str bool', importPrivKey: 'str str bool',
keypoolRefill: '', keyPoolRefill: '',
listAccounts: 'int', listAccounts: 'int',
listAddressGroupings: '', listAddressGroupings: '',
listReceivedByAccount: 'int bool', listReceivedByAccount: 'int bool',
@ -152,12 +152,22 @@ function ClassSpec(b) {
options[k] = self.httpOptions[k]; options[k] = self.httpOptions[k];
} }
} }
var err = null;
var req = this.protocol.request(options, function(res) { var req = this.protocol.request(options, function(res) {
var buf = ''; var buf = '';
res.on('data', function(data) { res.on('data', function(data) {
buf += data; buf += data;
}); });
res.on('end', function() { res.on('end', function() {
if(res.statusCode == 401) {
callback(new Error('bitcoin JSON-RPC connection rejected: unauthorized'));
return;
}
if(err) {
callback(err);
return;
}
try { try {
var parsedBuf = JSON.parse(buf); var parsedBuf = JSON.parse(buf);
} catch(e) { } catch(e) {
@ -170,7 +180,7 @@ function ClassSpec(b) {
}); });
}); });
req.on('error', function(e) { req.on('error', function(e) {
callback(e); log.err('Could not connect to bitcoin via RPC: '+e);
}); });
req.setHeader('Content-Length', request.length); req.setHeader('Content-Length', request.length);

32
Script.js

@ -2,7 +2,7 @@ require('classtool');
function spec(b) { function spec(b) {
var config = b.config || require('./config'); var config = b.config || require('./config');
var log = b.log || require('./util/log')(config); var log = b.log || require('./util/log');
var Opcode = require('./Opcode').class(); var Opcode = require('./Opcode').class();
@ -21,13 +21,13 @@ function spec(b) {
var TX_MULTISIG = 3; var TX_MULTISIG = 3;
var TX_SCRIPTHASH = 4; var TX_SCRIPTHASH = 4;
var TX_TYPES = { var TX_TYPES = [
TX_UNKNOWN: 'unknown', 'unknown',
TX_PUBKEY: 'pubkey', 'pubkey',
TX_PUBKEYHASH: 'pubkeyhash', 'pubkeyhash',
TX_MULTISIG: 'multisig', 'multisig',
TX_SCRIPTHASH: 'scripthash', 'scripthash'
}; ];
function Script(buffer) { function Script(buffer) {
if(buffer) { if(buffer) {
@ -168,12 +168,16 @@ function spec(b) {
Script.prototype.getOutType = function () Script.prototype.getOutType = function ()
{ {
var txType = this.classify(); var txType = this.classify();
switch (txType) { switch (txType) {
case TX_PUBKEY: return 'Pubkey'; case TX_PUBKEY: return 'Pubkey';
case TX_PUBKEYHASH: return 'Address'; case TX_PUBKEYHASH: return 'Address';
default: return 'Strange'; default: return 'Strange';
} }
};
Script.prototype.getRawOutType = function() {
return TX_TYPES[this.classify()];
}; };
Script.prototype.simpleOutHash = function () Script.prototype.simpleOutHash = function ()

2
ScriptInterpreter.js

@ -3,7 +3,7 @@ require('classtool');
function spec(b) { function spec(b) {
var assert = require('assert'); var assert = require('assert');
var config = b.config || require('./config'); var config = b.config || require('./config');
var log = b.log || require('./util/log')(config); var log = b.log || require('./util/log');
var Opcode = require('./Opcode').class(); var Opcode = require('./Opcode').class();

2
Transaction.js

@ -2,7 +2,7 @@ require('classtool');
function spec(b) { function spec(b) {
var config = b.config || require('./config'); var config = b.config || require('./config');
var log = b.log || require('./util/log')(config); var log = b.log || require('./util/log');
var Script = b.Script || require('./Script').class(); var Script = b.Script || require('./Script').class();
var ScriptInterpreter = b.ScriptInterpreter || require('./ScriptInterpreter').class(); var ScriptInterpreter = b.ScriptInterpreter || require('./ScriptInterpreter').class();
var util = b.util || require('./util/util'); var util = b.util || require('./util/util');

10
util/EncodedData.js

@ -57,6 +57,16 @@ function ClassSpec(b) {
this._validate(); this._validate();
}; };
// Boolean protocol for testing if valid
EncodedData.prototype.isValid = function() {
try {
this.validate();
return true;
} catch(e) {
return false;
}
};
// convert to a string (in base58 form) // convert to a string (in base58 form)
EncodedData.prototype.toString = function() { EncodedData.prototype.toString = function() {
return this.as('base58'); return this.as('base58');

12
util/log.js

@ -6,9 +6,9 @@ var loggers = {
debug: {info: console.log, warn: console.log, err: console.log, debug: console.log}, debug: {info: console.log, warn: console.log, err: console.log, debug: console.log},
}; };
module.exports = function(config) { var config = require('../config');
config = config || {}; if(config.log) {
if(config.log) return config.log; module.exports = config.log;
if(config.loggers) return config.loggers[config.logging || 'normal']; } else {
return loggers[config.logging || 'normal']; module.exports = loggers[config.logger || 'normal'];
}; }

45
util/util.js

@ -3,6 +3,7 @@ var crypto = require('crypto');
var bignum = require('bignum'); var bignum = require('bignum');
var Binary = require('binary'); var Binary = require('binary');
var Put = require('bufferput'); var Put = require('bufferput');
var Address = require('../Address').class();
var sha256 = exports.sha256 = function (data) { var sha256 = exports.sha256 = function (data) {
return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary'); return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
@ -107,50 +108,6 @@ var formatValue = exports.formatValue = function (valueBuffer) {
return integerPart+"."+decimalPart; return integerPart+"."+decimalPart;
}; };
var pubKeyHashToAddress = exports.pubKeyHashToAddress = function (pubKeyHash, addressVersion) {
if (!pubKeyHash) return "";
var put = new Put();
// Version
if(addressVersion) {
put.word8le(addressVersion);
} else {
put.word8le(0);
}
// Hash
put.put(pubKeyHash);
// Checksum (four bytes)
put.put(twoSha256(put.buffer()).slice(0,4));
return encodeBase58(put.buffer());
};
var addressToPubKeyHash = exports.addressToPubKeyHash = function (address) {
// Trim
address = String(address).replace(/\s/g, '');
// Check sanity
if (!address.match(/^[1-9A-HJ-NP-Za-km-z]{27,35}$/)) {
return null;
}
// Decode
var buffer = decodeBase58(address);
// Parse
var parser = Binary.parse(buffer);
parser.word8('version');
parser.buffer('hash', 20);
parser.buffer('checksum', 4);
// Check checksum
var checksum = twoSha256(buffer.slice(0, 21)).slice(0, 4);
if (checksum.compare(parser.vars.checksum) !== 0) {
return null;
}
return parser.vars.hash;
};
// Utility that synchronizes function calls based on a key // Utility that synchronizes function calls based on a key
var createSynchrotron = exports.createSynchrotron = function (fn) { var createSynchrotron = exports.createSynchrotron = function (fn) {
var table = {}; var table = {};

Loading…
Cancel
Save