Merge pull request #195 from ripple/use-ledgerselect

Switch account requests to use ledgerSelect rather than ledgerChoose
This commit is contained in:
Geert Weening
2014-10-27 16:07:40 -07:00
5 changed files with 66 additions and 35 deletions

View File

@@ -1200,16 +1200,16 @@ Remote.prototype.requestTx = function(hash, callback) {
/** /**
* Account request abstraction * Account request abstraction
* *
* @this Remote
* @api private * @api private
*/ */
Remote.accountRequest = function(type, account, accountIndex, ledger, peer, callback) { Remote.accountRequest = function(type, account, ledger, peer, callback) {
if (typeof account === 'object') { if (typeof account === 'object') {
var options = account; var options = account;
callback = accountIndex; callback = ledger;
ledger = options.ledger; ledger = options.ledger;
accountIndex = options.account_index || options.accountIndex; account = options.account || options.accountID;
account = options.accountID || options.account;
peer = options.peer; peer = options.peer;
} }
@@ -1222,18 +1222,10 @@ Remote.accountRequest = function(type, account, accountIndex, ledger, peer, call
var request = new Request(this, type); var request = new Request(this, type);
var account = UInt160.json_rewrite(account); var account = UInt160.json_rewrite(account);
request.message.ident = account; //DEPRECATED;
request.message.account = account; request.message.account = account;
request.ledgerSelect(ledger);
if (typeof accountIndex === 'number') { if (UInt160.is_valid(peer)) {
request.message.index = accountIndex;
}
if (!/^(undefined|function)$/.test(typeof ledger)) {
request.ledgerChoose(ledger);
}
if (!/^(undefined|function)$/.test(typeof peer)) {
request.message.peer = UInt160.json_rewrite(peer); request.message.peer = UInt160.json_rewrite(peer);
} }
@@ -1246,6 +1238,7 @@ Remote.accountRequest = function(type, account, accountIndex, ledger, peer, call
* Request account_info * Request account_info
* *
* @param {String} ripple address * @param {String} ripple address
* @param [String|Number] ledger identifier
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
@@ -1259,6 +1252,7 @@ Remote.prototype.requestAccountInfo = function(account, callback) {
* Request account_currencies * Request account_currencies
* *
* @param {String} ripple address * @param {String} ripple address
* @param [String|Number] ledger identifier
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
@@ -1272,14 +1266,13 @@ Remote.prototype.requestAccountCurrencies = function(account, callback) {
* Request account_lines * Request account_lines
* *
* @param {String} ripple address * @param {String} ripple address
* @param {Number] sub-account index * @param [String|Number] ledger identifier
* @param [String|Number] ledger
* @param [String] peer * @param [String] peer
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
Remote.prototype.requestAccountLines = function(account, accountIndex, ledger, peer, callback) { Remote.prototype.requestAccountLines = function(account, peer, callback) {
// XXX Does this require the server to be trusted? // XXX Does this require the server to be trusted?
//utils.assert(this.trusted); //utils.assert(this.trusted);
var args = Array.prototype.concat.apply(['account_lines'], arguments); var args = Array.prototype.concat.apply(['account_lines'], arguments);
@@ -1290,19 +1283,16 @@ Remote.prototype.requestAccountLines = function(account, accountIndex, ledger, p
* Request account_offers * Request account_offers
* *
* @param {String} ripple address * @param {String} ripple address
* @param {Number] sub-account index * @param [String|Number] ledger identifier
* @param [String|Number] ledger
* @param [String] peer
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
Remote.prototype.requestAccountOffers = function(account, accountIndex, ledger, callback) { Remote.prototype.requestAccountOffers = function(account, callback) {
var args = Array.prototype.concat.apply(['account_offers'], arguments); var args = Array.prototype.concat.apply(['account_offers'], arguments);
return Remote.accountRequest.apply(this, args); return Remote.accountRequest.apply(this, args);
}; };
/** /**
* Request account_tx * Request account_tx
* *
@@ -1318,9 +1308,6 @@ Remote.prototype.requestAccountOffers = function(account, accountIndex, ledger,
* @param [Number] offset, defaults to 0 * @param [Number] offset, defaults to 0
* @param [Number] limit * @param [Number] limit
* *
* @param [Function] filter
* @param [Function] map
* @param [Function] reduce
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
@@ -1606,6 +1593,7 @@ Remote.prototype.requestLedgerAccept = function(callback) {
/** /**
* Account root request abstraction * Account root request abstraction
* *
* @this Remote
* @api private * @api private
*/ */

View File

@@ -208,6 +208,13 @@ Request.prototype.ledgerIndex = function(ledger_index) {
return this; return this;
}; };
/**
* Set either ledger_index or ledger_hash based on heuristic
*
* @param {Number|String} ledger identifier
*/
Request.prototype.selectLedger =
Request.prototype.ledgerSelect = function(ledger) { Request.prototype.ledgerSelect = function(ledger) {
switch (ledger) { switch (ledger) {
case 'current': case 'current':
@@ -217,10 +224,10 @@ Request.prototype.ledgerSelect = function(ledger) {
break; break;
default: default:
if (isNaN(ledger)) { if (Number(ledger)) {
this.message.ledger_hash = ledger; this.message.ledger_index = Number(ledger);
} else if ((ledger = Number(ledger))) { } if (/^[A-F0-9]+$/.test(ledger)) {
this.message.ledger_index = ledger; this.message.ledger_hash = ledger;
} }
break; break;
} }

View File

@@ -276,7 +276,6 @@ describe('OrderBook', function() {
assert.deepEqual(request.message, { assert.deepEqual(request.message, {
command: 'account_info', command: 'account_info',
id: void(0), id: void(0),
ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji',
account: 'rrrrrrrrrrrrrrrrrrrrBZbvji' account: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
}); });
request.emit('success', { request.emit('success', {
@@ -1360,7 +1359,6 @@ describe('OrderBook', function() {
assert.deepEqual(request.message, { assert.deepEqual(request.message, {
command: 'account_info', command: 'account_info',
id: undefined, id: undefined,
ident: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B' account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
}); });

View File

@@ -185,12 +185,43 @@ describe('Remote', function () {
assert(request instanceof Request); assert(request instanceof Request);
}); });
it('request account balance with ledger', function() { it('request account info with ledger index', function() {
var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 9592219);
assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
assert.strictEqual(request.message.ledger_index, 9592219);
});
it('request account info with ledger hash', function() {
var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
});
it('request account info with ledger identifier', function() {
var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'validated');
assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
assert.strictEqual(request.message.ledger_index, 'validated');
});
it('request account balance with ledger index', function() {
var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 9592219); var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 9592219);
assert.strictEqual(request.message.command, 'ledger_entry'); assert.strictEqual(request.message.command, 'ledger_entry');
assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS'); assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
assert.strictEqual(request.message.ledger_index, 9592219); assert.strictEqual(request.message.ledger_index, 9592219);
}); });
it('request account balance with ledger hash', function() {
var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
assert.strictEqual(request.message.command, 'ledger_entry');
assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
});
it('request account balance with ledger identifier', function() {
var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'validated');
assert.strictEqual(request.message.command, 'ledger_entry');
assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
assert.strictEqual(request.message.ledger_index, 'validated');
});
}) })
it('create remote and get pending transactions', function() { it('create remote and get pending transactions', function() {

View File

@@ -355,13 +355,20 @@ describe('Request', function() {
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
}); });
it('Select ledger - hash', function() { it('Select ledger - undefined', function() {
var remote = new Remote(); var remote = new Remote();
remote._connected = true; remote._connected = true;
var request = new Request(remote, 'server_info'); var request = new Request(remote, 'server_info');
request.ledgerSelect('B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); request.ledgerSelect();
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE'); assert.strictEqual(request.message.ledger_hash, void(0));
assert.strictEqual(request.message.ledger_index, void(0));
request.ledgerSelect(null);
assert.strictEqual(request.message.ledger_hash, void(0));
assert.strictEqual(request.message.ledger_index, void(0));
request.ledgerSelect(NaN);
assert.strictEqual(request.message.ledger_hash, void(0));
assert.strictEqual(request.message.ledger_index, void(0));
}); });
it('Set account_root', function() { it('Set account_root', function() {