Skip to content

Commit

Permalink
Switch account requests to use ledgerSelect rather than ledgerChoose
Browse files Browse the repository at this point in the history
  • Loading branch information
wltsmrz committed Oct 27, 2014
1 parent bc1f9f8 commit 278df90
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
38 changes: 13 additions & 25 deletions src/js/ripple/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,16 +1200,16 @@ Remote.prototype.requestTx = function(hash, callback) {
/**
* Account request abstraction
*
* @this Remote
* @api private
*/

Remote.accountRequest = function(type, account, accountIndex, ledger, peer, callback) {
Remote.accountRequest = function(type, account, ledger, peer, callback) {
if (typeof account === 'object') {
var options = account;
callback = accountIndex;
callback = ledger;
ledger = options.ledger;
accountIndex = options.account_index || options.accountIndex;
account = options.accountID || options.account;
account = options.account || options.accountID;
peer = options.peer;
}

Expand All @@ -1222,18 +1222,10 @@ Remote.accountRequest = function(type, account, accountIndex, ledger, peer, call
var request = new Request(this, type);
var account = UInt160.json_rewrite(account);

request.message.ident = account; //DEPRECATED;
request.message.account = account;
request.ledgerSelect(ledger);

if (typeof accountIndex === 'number') {
request.message.index = accountIndex;
}

if (!/^(undefined|function)$/.test(typeof ledger)) {
request.ledgerChoose(ledger);
}

if (!/^(undefined|function)$/.test(typeof peer)) {
if (UInt160.is_valid(peer)) {
request.message.peer = UInt160.json_rewrite(peer);
}

Expand All @@ -1246,6 +1238,7 @@ Remote.accountRequest = function(type, account, accountIndex, ledger, peer, call
* Request account_info
*
* @param {String} ripple address
* @param [String|Number] ledger identifier
* @param [Function] callback
* @return {Request}
*/
Expand All @@ -1259,6 +1252,7 @@ Remote.prototype.requestAccountInfo = function(account, callback) {
* Request account_currencies
*
* @param {String} ripple address
* @param [String|Number] ledger identifier
* @param [Function] callback
* @return {Request}
*/
Expand All @@ -1272,14 +1266,13 @@ Remote.prototype.requestAccountCurrencies = function(account, callback) {
* Request account_lines
*
* @param {String} ripple address
* @param {Number] sub-account index
* @param [String|Number] ledger
* @param [String|Number] ledger identifier
* @param [String] peer
* @param [Function] callback
* @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?
//utils.assert(this.trusted);
var args = Array.prototype.concat.apply(['account_lines'], arguments);
Expand All @@ -1290,19 +1283,16 @@ Remote.prototype.requestAccountLines = function(account, accountIndex, ledger, p
* Request account_offers
*
* @param {String} ripple address
* @param {Number] sub-account index
* @param [String|Number] ledger
* @param [String] peer
* @param [String|Number] ledger identifier
* @param [Function] callback
* @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);
return Remote.accountRequest.apply(this, args);
};


/**
* Request account_tx
*
Expand All @@ -1318,9 +1308,6 @@ Remote.prototype.requestAccountOffers = function(account, accountIndex, ledger,
* @param [Number] offset, defaults to 0
* @param [Number] limit
*
* @param [Function] filter
* @param [Function] map
* @param [Function] reduce
* @param [Function] callback
* @return {Request}
*/
Expand Down Expand Up @@ -1606,6 +1593,7 @@ Remote.prototype.requestLedgerAccept = function(callback) {
/**
* Account root request abstraction
*
* @this Remote
* @api private
*/

Expand Down
15 changes: 11 additions & 4 deletions src/js/ripple/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ Request.prototype.ledgerIndex = function(ledger_index) {
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) {
switch (ledger) {
case 'current':
Expand All @@ -217,10 +224,10 @@ Request.prototype.ledgerSelect = function(ledger) {
break;

default:
if (isNaN(ledger)) {
this.message.ledger_hash = ledger;
} else if ((ledger = Number(ledger))) {
this.message.ledger_index = ledger;
if (Number(ledger)) {
this.message.ledger_index = Number(ledger);
} if (/^[A-F0-9]+$/.test(ledger)) {
this.message.ledger_hash = ledger;
}
break;
}
Expand Down
2 changes: 0 additions & 2 deletions test/orderbook-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ describe('OrderBook', function() {
assert.deepEqual(request.message, {
command: 'account_info',
id: void(0),
ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji',
account: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
});
request.emit('success', {
Expand Down Expand Up @@ -1360,7 +1359,6 @@ describe('OrderBook', function() {
assert.deepEqual(request.message, {
command: 'account_info',
id: undefined,
ident: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
});

Expand Down
33 changes: 32 additions & 1 deletion test/remote-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,43 @@ describe('Remote', function () {
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);
assert.strictEqual(request.message.command, 'ledger_entry');
assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
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() {
Expand Down
13 changes: 10 additions & 3 deletions test/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,20 @@ describe('Request', function() {
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
});

it('Select ledger - hash', function() {
it('Select ledger - undefined', function() {
var remote = new Remote();
remote._connected = true;

var request = new Request(remote, 'server_info');
request.ledgerSelect('B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
request.ledgerSelect();
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() {
Expand Down

0 comments on commit 278df90

Please sign in to comment.