This commit is contained in:
wltsmrz
2014-04-17 15:19:08 -07:00
parent 969873441e
commit 8ffd0b13a3
3 changed files with 95 additions and 90 deletions

View File

@@ -239,20 +239,20 @@ function Remote(opts, trace) {
self.storage.getPendingTransactions(function(err, transactions) { self.storage.getPendingTransactions(function(err, transactions) {
if (err || !Array.isArray(transactions)) return; if (err || !Array.isArray(transactions)) return;
var properties = [
'submittedIDs',
'clientID',
'submitIndex'
];
function resubmitTransaction(tx) { function resubmitTransaction(tx) {
var transaction = self.transaction(); var transaction = self.transaction();
transaction.parseJson(tx.tx_json); transaction.parseJson(tx.tx_json);
properties.forEach(function(prop) {
if (typeof tx[prop] !== 'undefined') { Object.keys(tx).forEach(function(prop) {
transaction[prop] = tx[prop]; switch (prop) {
case 'submittedIDs':
case 'clientID':
case 'submitIndex':
transaction[prop] = tx[prop];
break;
} }
}); });
transaction.submit(); transaction.submit();
}; };
@@ -344,16 +344,16 @@ Remote.prototype.addServer = function(opts) {
server.on('message', serverMessage); server.on('message', serverMessage);
function serverConnect() { function serverConnect() {
self._connection_count += 1;
if (opts.primary || !self._primary_server) { if (opts.primary || !self._primary_server) {
self._setPrimaryServer(server); self._setPrimaryServer(server);
} }
switch (++self._connection_count) { if (self._connection_count === 1) {
case 1: self._setState('online');
self._setState('online'); }
break; if (self._connection_count === self._servers.length) {
case self._servers.length: self.emit('ready');
self.emit('ready');
break;
} }
}; };
@@ -699,20 +699,18 @@ Remote.prototype.requestLedger = function(ledger, options, callback) {
request.message.ledger = ledger; request.message.ledger = ledger;
} }
var requestFields = [
'full',
'expand',
'transactions',
'accounts'
];
switch (typeof options) { switch (typeof options) {
case 'object': case 'object':
for (var key in options) { Object.keys(options).forEach(function(o) {
if (~requestFields.indexOf(key)) { switch (o) {
request.message[key] = true; case 'full':
case 'expand':
case 'transactions':
case 'accounts':
request.message[o] = true;
break;
} }
} }, options);
break; break;
case 'function': case 'function':
@@ -732,7 +730,7 @@ Remote.prototype.requestLedger = function(ledger, options, callback) {
return request; return request;
}; };
// Only for unit testing. Remote.prototype.requestLedgerClosed =
Remote.prototype.requestLedgerHash = function(callback) { Remote.prototype.requestLedgerHash = function(callback) {
//utils.assert(this.trusted); // If not trusted, need to check proof. //utils.assert(this.trusted); // If not trusted, need to check proof.
return new Request(this, 'ledger_closed').callback(callback); return new Request(this, 'ledger_closed').callback(callback);
@@ -975,26 +973,24 @@ Remote.prototype.requestAccountTx = function(options, callback) {
var request = new Request(this, 'account_tx'); var request = new Request(this, 'account_tx');
var requestFields = [ Object.keys(options).forEach(function(o) {
'account', switch (o) {
'ledger_index_min', //earliest case 'account':
'ledger_index_max', //latest case 'ledger_index_min': //earliest
'binary', //false case 'ledger_index_max': //latest
'count', //false case 'binary': //false
'descending', //false case 'count': //false
'offset', //0 case 'descending': //false
'limit', case 'offset': //0
case 'limit':
//extended account_tx //extended account_tx
'forward', //false case 'forward': //false
'marker' case 'marker':
]; request.message[o] = this[o];
break;
for (var key in options) {
if (~requestFields.indexOf(key)) {
request.message[key] = options[key];
} }
} }, options);
function propertiesFilter(obj, transaction) { function propertiesFilter(obj, transaction) {
var properties = Object.keys(obj); var properties = Object.keys(obj);
@@ -1259,6 +1255,7 @@ Remote.accountRootRequest = function(type, responseFilter, account, ledger, call
} }
var request = this.requestLedgerEntry('account_root'); var request = this.requestLedgerEntry('account_root');
request.accountRoot(account); request.accountRoot(account);
request.ledgerChoose(ledger); request.ledgerChoose(ledger);

View File

@@ -7,6 +7,7 @@ var Account = require('./account').Account;
var Meta = require('./meta').Meta; var Meta = require('./meta').Meta;
var OrderBook = require('./orderbook').OrderBook; var OrderBook = require('./orderbook').OrderBook;
var RippleError = require('./rippleerror').RippleError; var RippleError = require('./rippleerror').RippleError;
var Server = require('./server').Server;
// Request events emitted: // Request events emitted:
// 'success' : Request successful. // 'success' : Request successful.
@@ -17,12 +18,9 @@ var RippleError = require('./rippleerror').RippleError;
function Request(remote, command) { function Request(remote, command) {
EventEmitter.call(this); EventEmitter.call(this);
this.remote = remote; this.remote = remote;
this.requested = false; this.requested = false;
this.message = { this.message = { command: command, id: void(0) };
command : command,
id : void(0)
};
}; };
util.inherits(Request, EventEmitter); util.inherits(Request, EventEmitter);
@@ -37,6 +35,7 @@ Request.prototype.request = function(remote) {
if (this.requested) return; if (this.requested) return;
this.requested = true; this.requested = true;
this.on('error', new Function); this.on('error', new Function);
this.emit('request', remote); this.emit('request', remote);
@@ -44,7 +43,7 @@ Request.prototype.request = function(remote) {
this.remote._servers.forEach(function(server) { this.remote._servers.forEach(function(server) {
this.setServer(server); this.setServer(server);
this.remote.request(this); this.remote.request(this);
}, this ); }, this);
} else { } else {
this.remote.request(this); this.remote.request(this);
} }
@@ -53,37 +52,40 @@ Request.prototype.request = function(remote) {
}; };
Request.prototype.callback = function(callback, successEvent, errorEvent) { Request.prototype.callback = function(callback, successEvent, errorEvent) {
if (callback && typeof callback === 'function') { var self = this;
var self = this;
function request_success(message) { if (this.requestsed || typeof callback !== 'function') {
callback.call(self, null, message); return this;
}
function request_error(error) {
if (!(error instanceof RippleError)) {
error = new RippleError(error);
}
callback.call(self, error);
}
this.once(successEvent || 'success', request_success);
this.once(errorEvent || 'error' , request_error);
this.request();
} }
function requestSuccess(message) {
callback.call(self, null, message);
};
function requestError(error) {
if (!(error instanceof RippleError)) {
error = new RippleError(error);
}
callback.call(self, error);
};
this.once(successEvent || 'success', requestSuccess);
this.once(errorEvent || 'error' , requestError);
this.request();
return this; return this;
}; };
Request.prototype.timeout = function(duration, callback) { Request.prototype.timeout = function(duration, callback) {
var self = this; var self = this;
function requested() {
self.timeout(duration, callback);
};
if (!this.requested) { if (!this.requested) {
function requested() { // Defer until requested
self.timeout(duration, callback); return this.once('request', requested);
}
this.once('request', requested);
return;
} }
var emit = this.emit; var emit = this.emit;
@@ -112,8 +114,11 @@ Request.prototype.setServer = function(server) {
case 'object': case 'object':
selected = server; selected = server;
break; break;
case 'string': case 'string':
// Find server with hostname string
var servers = this.remote._servers; var servers = this.remote._servers;
for (var i=0, s; s=servers[i]; i++) { for (var i=0, s; s=servers[i]; i++) {
if (s._host === server) { if (s._host === server) {
selected = s; selected = s;
@@ -123,18 +128,19 @@ Request.prototype.setServer = function(server) {
break; break;
}; };
this.server = selected; if (selected instanceof Server) {
this.server = selected;
}
return this; return this;
}; };
Request.prototype.buildPath = function(build) { Request.prototype.buildPath = function(build) {
if (this.remote.local_signing) { if (this.remote.local_signing) {
throw new Error( throw new Error(
'`build_path` is completely ignored when doing local signing as ' + '`build_path` is completely ignored when doing local signing as '
'`Paths` is a component of the signed blob. The `tx_blob` is signed,' + + '`Paths` is a component of the signed blob. The `tx_blob` is signed,'
'sealed and delivered, and the txn unmodified after' ); + 'sealed and delivered, and the txn unmodified after' );
} }
if (build) { if (build) {
@@ -144,6 +150,7 @@ Request.prototype.buildPath = function(build) {
// value being `truthy` // value being `truthy`
delete this.message.build_path delete this.message.build_path
} }
return this; return this;
}; };
@@ -153,6 +160,7 @@ Request.prototype.ledgerChoose = function(current) {
} else { } else {
this.message.ledger_hash = this.remote._ledger_hash; this.message.ledger_hash = this.remote._ledger_hash;
} }
return this; return this;
}; };
@@ -171,8 +179,8 @@ Request.prototype.ledgerIndex = function(ledger_index) {
return this; return this;
}; };
Request.prototype.ledgerSelect = function(ledger_spec) { Request.prototype.ledgerSelect = function(ledger) {
switch (ledger_spec) { switch (ledger) {
case 'current': case 'current':
case 'closed': case 'closed':
case 'verified': case 'verified':
@@ -180,10 +188,10 @@ Request.prototype.ledgerSelect = function(ledger_spec) {
break; break;
default: default:
if (Number(ledger_spec)) { if (isNaN(ledger)) {
this.message.ledger_index = ledger_spec; this.message.ledger_hash = ledger;
} else { } else if (ledger = Number(ledger)) {
this.message.ledger_hash = ledger_spec; this.message.ledger_index = ledger;
} }
break; break;
} }
@@ -196,8 +204,8 @@ Request.prototype.accountRoot = function(account) {
return this; return this;
}; };
Request.prototype.index = function(hash) { Request.prototype.index = function(index) {
this.message.index = hash; this.message.index = index;
return this; return this;
}; };
@@ -305,7 +313,7 @@ Request.prototype.books = function(books, snapshot) {
Request.prototype.addBook = function (book, snapshot) { Request.prototype.addBook = function (book, snapshot) {
if (!Array.isArray(this.message.books)) { if (!Array.isArray(this.message.books)) {
this.message.books = []; this.message.books = [ ];
} }
var json = { }; var json = { };

View File

@@ -683,8 +683,8 @@ Transaction.prototype.payment = function(src, dst, amount) {
src = options.source || options.from || options.account; src = options.source || options.from || options.account;
} }
if (options.invoiceID) { if (src.invoiceID) {
this.invoiceID(options.invoiceID); this.invoiceID(src.invoiceID);
} }
if (!UInt160.is_valid(src)) { if (!UInt160.is_valid(src)) {