Skip to content

Commit 278df90

Browse files
committed
Switch account requests to use ledgerSelect rather than ledgerChoose
1 parent bc1f9f8 commit 278df90

File tree

5 files changed

+66
-35
lines changed

5 files changed

+66
-35
lines changed

src/js/ripple/remote.js

+13-25
Original file line numberDiff line numberDiff line change
@@ -1200,16 +1200,16 @@ Remote.prototype.requestTx = function(hash, callback) {
12001200
/**
12011201
* Account request abstraction
12021202
*
1203+
* @this Remote
12031204
* @api private
12041205
*/
12051206

1206-
Remote.accountRequest = function(type, account, accountIndex, ledger, peer, callback) {
1207+
Remote.accountRequest = function(type, account, ledger, peer, callback) {
12071208
if (typeof account === 'object') {
12081209
var options = account;
1209-
callback = accountIndex;
1210+
callback = ledger;
12101211
ledger = options.ledger;
1211-
accountIndex = options.account_index || options.accountIndex;
1212-
account = options.accountID || options.account;
1212+
account = options.account || options.accountID;
12131213
peer = options.peer;
12141214
}
12151215

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

1225-
request.message.ident = account; //DEPRECATED;
12261225
request.message.account = account;
1226+
request.ledgerSelect(ledger);
12271227

1228-
if (typeof accountIndex === 'number') {
1229-
request.message.index = accountIndex;
1230-
}
1231-
1232-
if (!/^(undefined|function)$/.test(typeof ledger)) {
1233-
request.ledgerChoose(ledger);
1234-
}
1235-
1236-
if (!/^(undefined|function)$/.test(typeof peer)) {
1228+
if (UInt160.is_valid(peer)) {
12371229
request.message.peer = UInt160.json_rewrite(peer);
12381230
}
12391231

@@ -1246,6 +1238,7 @@ Remote.accountRequest = function(type, account, accountIndex, ledger, peer, call
12461238
* Request account_info
12471239
*
12481240
* @param {String} ripple address
1241+
* @param [String|Number] ledger identifier
12491242
* @param [Function] callback
12501243
* @return {Request}
12511244
*/
@@ -1259,6 +1252,7 @@ Remote.prototype.requestAccountInfo = function(account, callback) {
12591252
* Request account_currencies
12601253
*
12611254
* @param {String} ripple address
1255+
* @param [String|Number] ledger identifier
12621256
* @param [Function] callback
12631257
* @return {Request}
12641258
*/
@@ -1272,14 +1266,13 @@ Remote.prototype.requestAccountCurrencies = function(account, callback) {
12721266
* Request account_lines
12731267
*
12741268
* @param {String} ripple address
1275-
* @param {Number] sub-account index
1276-
* @param [String|Number] ledger
1269+
* @param [String|Number] ledger identifier
12771270
* @param [String] peer
12781271
* @param [Function] callback
12791272
* @return {Request}
12801273
*/
12811274

1282-
Remote.prototype.requestAccountLines = function(account, accountIndex, ledger, peer, callback) {
1275+
Remote.prototype.requestAccountLines = function(account, peer, callback) {
12831276
// XXX Does this require the server to be trusted?
12841277
//utils.assert(this.trusted);
12851278
var args = Array.prototype.concat.apply(['account_lines'], arguments);
@@ -1290,19 +1283,16 @@ Remote.prototype.requestAccountLines = function(account, accountIndex, ledger, p
12901283
* Request account_offers
12911284
*
12921285
* @param {String} ripple address
1293-
* @param {Number] sub-account index
1294-
* @param [String|Number] ledger
1295-
* @param [String] peer
1286+
* @param [String|Number] ledger identifier
12961287
* @param [Function] callback
12971288
* @return {Request}
12981289
*/
12991290

1300-
Remote.prototype.requestAccountOffers = function(account, accountIndex, ledger, callback) {
1291+
Remote.prototype.requestAccountOffers = function(account, callback) {
13011292
var args = Array.prototype.concat.apply(['account_offers'], arguments);
13021293
return Remote.accountRequest.apply(this, args);
13031294
};
13041295

1305-
13061296
/**
13071297
* Request account_tx
13081298
*
@@ -1318,9 +1308,6 @@ Remote.prototype.requestAccountOffers = function(account, accountIndex, ledger,
13181308
* @param [Number] offset, defaults to 0
13191309
* @param [Number] limit
13201310
*
1321-
* @param [Function] filter
1322-
* @param [Function] map
1323-
* @param [Function] reduce
13241311
* @param [Function] callback
13251312
* @return {Request}
13261313
*/
@@ -1606,6 +1593,7 @@ Remote.prototype.requestLedgerAccept = function(callback) {
16061593
/**
16071594
* Account root request abstraction
16081595
*
1596+
* @this Remote
16091597
* @api private
16101598
*/
16111599

src/js/ripple/request.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ Request.prototype.ledgerIndex = function(ledger_index) {
208208
return this;
209209
};
210210

211+
/**
212+
* Set either ledger_index or ledger_hash based on heuristic
213+
*
214+
* @param {Number|String} ledger identifier
215+
*/
216+
217+
Request.prototype.selectLedger =
211218
Request.prototype.ledgerSelect = function(ledger) {
212219
switch (ledger) {
213220
case 'current':
@@ -217,10 +224,10 @@ Request.prototype.ledgerSelect = function(ledger) {
217224
break;
218225

219226
default:
220-
if (isNaN(ledger)) {
221-
this.message.ledger_hash = ledger;
222-
} else if ((ledger = Number(ledger))) {
223-
this.message.ledger_index = ledger;
227+
if (Number(ledger)) {
228+
this.message.ledger_index = Number(ledger);
229+
} if (/^[A-F0-9]+$/.test(ledger)) {
230+
this.message.ledger_hash = ledger;
224231
}
225232
break;
226233
}

test/orderbook-test.js

-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ describe('OrderBook', function() {
276276
assert.deepEqual(request.message, {
277277
command: 'account_info',
278278
id: void(0),
279-
ident: 'rrrrrrrrrrrrrrrrrrrrBZbvji',
280279
account: 'rrrrrrrrrrrrrrrrrrrrBZbvji'
281280
});
282281
request.emit('success', {
@@ -1360,7 +1359,6 @@ describe('OrderBook', function() {
13601359
assert.deepEqual(request.message, {
13611360
command: 'account_info',
13621361
id: undefined,
1363-
ident: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
13641362
account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
13651363
});
13661364

test/remote-test.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,43 @@ describe('Remote', function () {
185185
assert(request instanceof Request);
186186
});
187187

188-
it('request account balance with ledger', function() {
188+
it('request account info with ledger index', function() {
189+
var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 9592219);
190+
assert.strictEqual(request.message.command, 'account_info');
191+
assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
192+
assert.strictEqual(request.message.ledger_index, 9592219);
193+
});
194+
it('request account info with ledger hash', function() {
195+
var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
196+
assert.strictEqual(request.message.command, 'account_info');
197+
assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
198+
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
199+
});
200+
it('request account info with ledger identifier', function() {
201+
var request = remote.requestAccountInfo('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'validated');
202+
assert.strictEqual(request.message.command, 'account_info');
203+
assert.strictEqual(request.message.account, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
204+
assert.strictEqual(request.message.ledger_index, 'validated');
205+
});
206+
207+
it('request account balance with ledger index', function() {
189208
var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 9592219);
190209
assert.strictEqual(request.message.command, 'ledger_entry');
191210
assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
192211
assert.strictEqual(request.message.ledger_index, 9592219);
193212
});
213+
it('request account balance with ledger hash', function() {
214+
var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
215+
assert.strictEqual(request.message.command, 'ledger_entry');
216+
assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
217+
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
218+
});
219+
it('request account balance with ledger identifier', function() {
220+
var request = remote.requestAccountBalance('r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS', 'validated');
221+
assert.strictEqual(request.message.command, 'ledger_entry');
222+
assert.strictEqual(request.message.account_root, 'r4qLSAzv4LZ9TLsR7diphGwKnSEAMQTSjS');
223+
assert.strictEqual(request.message.ledger_index, 'validated');
224+
});
194225
})
195226

196227
it('create remote and get pending transactions', function() {

test/request-test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,20 @@ describe('Request', function() {
355355
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
356356
});
357357

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

362362
var request = new Request(remote, 'server_info');
363-
request.ledgerSelect('B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
364-
assert.strictEqual(request.message.ledger_hash, 'B4FD84A73DBD8F0DA9E320D137176EBFED969691DC0AAC7882B76B595A0841AE');
363+
request.ledgerSelect();
364+
assert.strictEqual(request.message.ledger_hash, void(0));
365+
assert.strictEqual(request.message.ledger_index, void(0));
366+
request.ledgerSelect(null);
367+
assert.strictEqual(request.message.ledger_hash, void(0));
368+
assert.strictEqual(request.message.ledger_index, void(0));
369+
request.ledgerSelect(NaN);
370+
assert.strictEqual(request.message.ledger_hash, void(0));
371+
assert.strictEqual(request.message.ledger_index, void(0));
365372
});
366373

367374
it('Set account_root', function() {

0 commit comments

Comments
 (0)