Cleanup, fix jshint warnings

This commit is contained in:
wltsmrz
2013-08-03 08:13:36 +09:00
parent bb6ec3b5c9
commit 5f017b2b85

View File

@@ -80,12 +80,9 @@ function Remote(opts, trace) {
this.trusted = opts.trusted; this.trusted = opts.trusted;
this.local_sequence = opts.local_sequence; // Locally track sequence numbers this.local_sequence = opts.local_sequence; // Locally track sequence numbers
this.local_fee = opts.local_fee; // Locally set fees this.local_fee = opts.local_fee; // Locally set fees
this.local_signing = (typeof opts.local_signing === 'undefined') this.local_signing = (typeof opts.local_signing === 'undefined') ? true : Boolean(opts.local_signing);
? true : Boolean(opts.local_signing); this.fee_cushion = (typeof opts.fee_cushion === 'undefined') ? 1.5 : Number(opts.fee_cushion);
this.fee_cushion = (typeof opts.fee_cushion === 'undefined') this.max_fee = (typeof opts.max_fee === 'undefined') ? Infinity : Number(opts.max_fee);
? 1.5 : Number(opts.fee_cushion);
this.max_fee = (typeof opts.max_fee === 'undefined')
? Infinity: Number(opts.max_fee);
this.id = 0; this.id = 0;
this.trace = opts.trace || trace; this.trace = opts.trace || trace;
this._server_fatal = false; // True, if we know server exited. this._server_fatal = false; // True, if we know server exited.
@@ -161,7 +158,7 @@ function Remote(opts, trace) {
secure: opts.websocket_ssl, secure: opts.websocket_ssl,
trusted: opts.trusted trusted: opts.trusted
} }
] ];
} }
opts.servers.forEach(function(server) { opts.servers.forEach(function(server) {
@@ -192,7 +189,7 @@ function Remote(opts, trace) {
} }
} }
}); });
}; }
util.inherits(Remote, EventEmitter); util.inherits(Remote, EventEmitter);
@@ -211,7 +208,7 @@ Remote.from_config = function (obj, trace) {
var remote = new Remote(serverConfig, trace); var remote = new Remote(serverConfig, trace);
for (var account in config.accounts) { Object.keys(config.accounts).forEach(function(account) {
var accountInfo = config.accounts[account]; var accountInfo = config.accounts[account];
if (typeof accountInfo === 'object') { if (typeof accountInfo === 'object') {
if (accountInfo.secret) { if (accountInfo.secret) {
@@ -221,7 +218,7 @@ Remote.from_config = function (obj, trace) {
remote.set_secret(accountInfo.account, accountInfo.secret); remote.set_secret(accountInfo.account, accountInfo.secret);
} }
} }
} });
return remote; return remote;
}; };
@@ -243,9 +240,9 @@ var isTefFailure = function (engine_result_code) {
Remote.prototype.add_server = function (opts) { Remote.prototype.add_server = function (opts) {
var self = this; var self = this;
var url = ((opts.secure || opts.websocket_ssl) ? 'wss://' : 'ws://') var url = ((opts.secure || opts.websocket_ssl) ? 'wss://' : 'ws://') +
+ (opts.host || opts.websocket_ip) + ':' (opts.host || opts.websocket_ip) + ':' +
+ (opts.port || opts.websocket_port) (opts.port || opts.websocket_port)
; ;
var server = new Server(this, {url: url}); var server = new Server(this, {url: url});
@@ -284,7 +281,9 @@ Remote.prototype.server_fatal = function () {
// Set the emitted state: 'online' or 'offline' // Set the emitted state: 'online' or 'offline'
Remote.prototype._set_state = function (state) { Remote.prototype._set_state = function (state) {
if (this.trace) console.log('remote: set_state: %s', state); if (this.trace) {
console.log('remote: set_state: %s', state);
}
if (this.state !== state) { if (this.state !== state) {
this.state = state; this.state = state;
@@ -328,8 +327,9 @@ Remote.prototype.connect = function (online) {
break; break;
default: default:
if (!Boolean(online)) if (!Boolean(online)) {
return this.disconnect() return this.disconnect();
}
break; break;
} }
@@ -371,7 +371,7 @@ Remote.prototype.ledger_hash = function () {
Remote.prototype._handle_message = function (json) { Remote.prototype._handle_message = function (json) {
var self = this; var self = this;
var unexpected = false; var unexpected = false;
var message var message;
try { try {
message = JSON.parse(json); message = JSON.parse(json);
@@ -414,24 +414,33 @@ Remote.prototype._handle_message = function (json) {
// De-duplicate transactions that are immediately following each other // De-duplicate transactions that are immediately following each other
// XXX Should have a cache of n txs so we can dedup out of order txs // XXX Should have a cache of n txs so we can dedup out of order txs
if (this._last_tx === message.transaction.hash) break; if (this._last_tx === message.transaction.hash) {
break;
}
this._last_tx = message.transaction.hash; this._last_tx = message.transaction.hash;
if (this.trace) utils.logObject('remote: tx: %s', message); if (this.trace) {
utils.logObject('remote: tx: %s', message);
}
// Process metadata // Process metadata
message.mmeta = new Meta(message.meta); message.mmeta = new Meta(message.meta);
// Pass the event on to any related Account objects // Pass the event on to any related Account objects
message.mmeta.getAffectedAccounts().forEach(function(account) { message.mmeta.getAffectedAccounts().forEach(function(account) {
var account = self._accounts[account]; account = self._accounts[account];
if (account) account.notifyTx(message); if (account) {
account.notifyTx(message);
}
}); });
// Pass the event on to any related OrderBooks // Pass the event on to any related OrderBooks
message.mmeta.getAffectedBooks().forEach(function(book) { message.mmeta.getAffectedBooks().forEach(function(book) {
var book = self._books[book]; book = self._books[book];
if (book) book.notifyTx(message); if (book) {
book.notifyTx(message);
}
}); });
this.emit('transaction', message); this.emit('transaction', message);
@@ -451,9 +460,12 @@ Remote.prototype._handle_message = function (json) {
case 'serverStatus': case 'serverStatus':
self.emit('server_status', message); self.emit('server_status', message);
if ('load_base' in message && 'load_factor' in message && var load_changed = message.hasOwnProperty('load_base')
(message.load_base !== self._load_base || message.load_factor != self._load_factor)) && message.hasOwnProperty('load_factor')
{ && (message.load_base !== self._load_base || message.load_factor !== self._load_factor)
;
if (load_changed) {
self._load_base = message.load_base; self._load_base = message.load_base;
self._load_factor = message.load_factor; self._load_factor = message.load_factor;
self.emit('load', { 'load_base' : self._load_base, 'load_factor' : self.load_factor }); self.emit('load', { 'load_base' : self._load_base, 'load_factor' : self.load_factor });
@@ -462,7 +474,9 @@ Remote.prototype._handle_message = function (json) {
// All other messages // All other messages
default: default:
if (this.trace) utils.logObject('remote: '+message.type+': %s', message); if (this.trace) {
utils.logObject('remote: '+message.type+': %s', message);
}
this.emit('net_' + message.type, message); this.emit('net_' + message.type, message);
break; break;
} }
@@ -501,7 +515,9 @@ Remote.prototype._get_server = function () {
server = this._primary_server; server = this._primary_server;
} else { } else {
server = this._next_server(); server = this._next_server();
if (server) this._set_primary_server(server); if (server) {
this._set_primary_server(server);
}
} }
return server; return server;
@@ -543,10 +559,12 @@ Remote.prototype.request_ledger = function (ledger, opts, callback) {
switch (typeof opts) { switch (typeof opts) {
case 'object': case 'object':
if (opts.full) request.message.full = true; var valid_properties = [ 'full', 'expand', 'transactions', 'accounts' ];
if (opts.expand) request.message.expand = true; valid_properties.forEach(function(prop) {
if (opts.transactions) request.message.transactions = true; if (opts.hasOwnProperty(prop)) {
if (opts.accounts) request.message.accounts = true; request.message[prop] = true;
}
});
break; break;
case 'function': case 'function':
@@ -609,14 +627,14 @@ Remote.prototype.request_ledger_entry = function (type, callback) {
// console.log('request_ledger_entry: caught'); // console.log('request_ledger_entry: caught');
if (self._ledger_hash) { //if (self._ledger_hash) {
// A specific ledger is requested. // A specific ledger is requested.
// XXX Add caching. // XXX Add caching.
}
// else if (req.ledger_index) // else if (req.ledger_index)
// else if ('ripple_state' === request.type) // YYY Could be cached per ledger. // else if ('ripple_state' === request.type) // YYY Could be cached per ledger.
else if (type === 'account_root') { //}
if (!self._ledger_hash && type === 'account_root') {
var cache = self.ledgers.current.account_root; var cache = self.ledgers.current.account_root;
if (!cache) { if (!cache) {
@@ -630,12 +648,11 @@ Remote.prototype.request_ledger_entry = function (type, callback) {
// console.log('request_ledger_entry: emulating'); // console.log('request_ledger_entry: emulating');
request.emit('success', { request.emit('success', {
// YYY Missing lots of fields. // YYY Missing lots of fields.
'node' : node, node : node
}); });
bDefault = false; bDefault = false;
} else { // Was not cached. } else { // Was not cached.
// XXX Only allow with trusted mode. Must sync response with advance. // XXX Only allow with trusted mode. Must sync response with advance.
switch (type) { switch (type) {
case 'account_root': case 'account_root':
@@ -657,8 +674,8 @@ Remote.prototype.request_ledger_entry = function (type, callback) {
// console.log('request_ledger_entry: invoking'); // console.log('request_ledger_entry: invoking');
request.request_default(); request.request_default();
} }
}
}; };
}
request.callback(callback); request.callback(callback);
@@ -781,10 +798,6 @@ Remote.prototype.request_account_tx = function (obj, callback) {
request.message.account = obj.account; request.message.account = obj.account;
if (false && ledger_min === ledger_max) {
//request.message.ledger = ledger_min;
}
else {
if (typeof obj.ledger_index_min !== 'undefined') {request.message.ledger_index_min = obj.ledger_index_min;} if (typeof obj.ledger_index_min !== 'undefined') {request.message.ledger_index_min = obj.ledger_index_min;}
if (typeof obj.ledger_index_max !== 'undefined') {request.message.ledger_index_max = obj.ledger_index_max;} if (typeof obj.ledger_index_max !== 'undefined') {request.message.ledger_index_max = obj.ledger_index_max;}
if (typeof obj.binary !== 'undefined') {request.message.binary = obj.binary;} if (typeof obj.binary !== 'undefined') {request.message.binary = obj.binary;}
@@ -792,7 +805,6 @@ Remote.prototype.request_account_tx = function (obj, callback) {
if (typeof obj.descending !== 'undefined') {request.message.descending = obj.descending;} if (typeof obj.descending !== 'undefined') {request.message.descending = obj.descending;}
if (typeof obj.offset !== 'undefined') {request.message.offset = obj.offset;} if (typeof obj.offset !== 'undefined') {request.message.offset = obj.offset;}
if (typeof obj.limit !== 'undefined') {request.message.limit = obj.limit;} if (typeof obj.limit !== 'undefined') {request.message.limit = obj.limit;}
}
request.callback(callback); request.callback(callback);
@@ -944,7 +956,7 @@ Remote.prototype.request_account_balance = function (account, current, callback)
.on('success', function (message) { .on('success', function (message) {
// If the caller also waits for 'success', they might run before this. // If the caller also waits for 'success', they might run before this.
request.emit('account_balance', Amount.from_json(message.node.Balance)); request.emit('account_balance', Amount.from_json(message.node.Balance));
}) });
request.callback(callback, 'account_balance'); request.callback(callback, 'account_balance');
@@ -960,7 +972,7 @@ Remote.prototype.request_account_flags = function (account, current, callback) {
.on('success', function (message) { .on('success', function (message) {
// If the caller also waits for 'success', they might run before this. // If the caller also waits for 'success', they might run before this.
request.emit('account_flags', message.node.Flags); request.emit('account_flags', message.node.Flags);
}) });
request.callback(callback, 'account_flags'); request.callback(callback, 'account_flags');
@@ -976,7 +988,7 @@ Remote.prototype.request_owner_count = function (account, current, callback) {
.on('success', function (message) { .on('success', function (message) {
// If the caller also waits for 'success', they might run before this. // If the caller also waits for 'success', they might run before this.
request.emit('owner_count', message.node.OwnerCount); request.emit('owner_count', message.node.OwnerCount);
}) });
request.callback(callback, 'owner_count'); request.callback(callback, 'owner_count');
@@ -990,19 +1002,17 @@ Remote.prototype.account = function (accountId) {
if (!account) { if (!account) {
account = new Account(this, accountId); account = new Account(this, accountId);
if (!account.is_valid()) return account; if (!account.is_valid()) {
return account;
}
this._accounts[accountId] = account; this._accounts[accountId] = account;
} }
return account; return account;
}; };
Remote.prototype.path_find = function (src_account, dst_account, Remote.prototype.path_find = function (src_account, dst_account, dst_amount, src_currencies) {
dst_amount, src_currencies) var path_find = new PathFind(this, src_account, dst_account, dst_amount, src_currencies);
{
var path_find = new PathFind(this,
src_account, dst_account,
dst_amount, src_currencies);
if (this._cur_path_find) { if (this._cur_path_find) {
this._cur_path_find.notify_superceded(); this._cur_path_find.notify_superceded();
@@ -1015,12 +1025,16 @@ Remote.prototype.path_find = function (src_account, dst_account,
return path_find; return path_find;
}; };
Remote.prototype.book = function (currency_gets, issuer_gets, Remote.prototype.book = function (currency_gets, issuer_gets, currency_pays, issuer_pays) {
currency_pays, issuer_pays) {
var gets = currency_gets; var gets = currency_gets;
if (gets !== 'XRP') gets += '/' + issuer_gets; if (gets !== 'XRP') {
gets += '/' + issuer_gets;
}
var pays = currency_pays; var pays = currency_pays;
if (pays !== 'XRP') pays += '/' + issuer_pays; if (pays !== 'XRP') {
pays += '/' + issuer_pays;
}
var key = gets + ':' + pays; var key = gets + ':' + pays;
@@ -1030,13 +1044,15 @@ Remote.prototype.book = function (currency_gets, issuer_gets,
currency_pays, issuer_pays currency_pays, issuer_pays
); );
if (!book.is_valid()) return book; if (!book.is_valid()) {
return book;
}
this._books[key] = book; this._books[key] = book;
} }
return this._books[key]; return this._books[key];
} };
// Return the next account sequence if possible. // Return the next account sequence if possible.
// <-- undefined or Sequence // <-- undefined or Sequence
@@ -1048,16 +1064,19 @@ Remote.prototype.account_seq = function (account, advance) {
if (account_info && account_info.seq) { if (account_info && account_info.seq) {
seq = account_info.seq; seq = account_info.seq;
if (advance === 'ADVANCE') account_info.seq += 1; if (advance === 'ADVANCE') {
if (advance === 'REWIND') account_info.seq -= 1; account_info.seq += 1;
}
if (advance === 'REWIND') {
account_info.seq -= 1;
}
// console.log('cached: %s current=%d next=%d', account, seq, account_info.seq); // console.log('cached: %s current=%d next=%d', account, seq, account_info.seq);
} else {
// console.log('uncached: %s', account);
} }
return seq; return seq;
} };
Remote.prototype.set_account_seq = function (account, seq) { Remote.prototype.set_account_seq = function (account, seq) {
var account = UInt160.json_rewrite(account); var account = UInt160.json_rewrite(account);
@@ -1189,10 +1208,7 @@ Remote.prototype.request_ripple_path_find = function (src_account, dst_account,
return request; return request;
}; };
Remote.prototype.request_path_find_create = function (src_account, dst_account, Remote.prototype.request_path_find_create = function (src_account, dst_account, dst_amount, src_currencies, callback) {
dst_amount,
src_currencies, callback)
{
var self = this; var self = this;
var request = new Request(this, 'path_find'); var request = new Request(this, 'path_find');
@@ -1205,11 +1221,13 @@ Remote.prototype.request_path_find_create = function (src_account, dst_account,
request.message.source_currencies = src_currencies.map(function (ci) { request.message.source_currencies = src_currencies.map(function (ci) {
var ci_new = {}; var ci_new = {};
if ('issuer' in ci) if ('issuer' in ci) {
ci_new.issuer = UInt160.json_rewrite(ci.issuer); ci_new.issuer = UInt160.json_rewrite(ci.issuer);
}
if ('currency' in ci) if ('currency' in ci) {
ci_new.currency = Currency.json_rewrite(ci.currency); ci_new.currency = Currency.json_rewrite(ci.currency);
}
return ci_new; return ci_new;
}); });
@@ -1220,8 +1238,7 @@ Remote.prototype.request_path_find_create = function (src_account, dst_account,
return request; return request;
}; };
Remote.prototype.request_path_find_close = function () Remote.prototype.request_path_find_close = function () {
{
var self = this; var self = this;
var request = new Request(this, 'path_find'); var request = new Request(this, 'path_find');