Skip to content

Commit

Permalink
Use streaming pathfinding: rippled path_find instead of ripple_path_find
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Clark committed Aug 11, 2015
1 parent fcbe7d3 commit 5cce850
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 30 deletions.
8 changes: 6 additions & 2 deletions src/api/ledger/pathfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ function requestPathFind(remote, pathfind: PathFind, callback) {
_.omit(utils.common.toRippledAmount(amount), 'value'));
}

remote.requestRipplePathFind(params,
composeAsync(_.partial(addParams, params), callback));
remote.createPathFind(params, function(data) {
if (data.full_reply) {
this.close();
callback(null, addParams(params, data));
}
});
}

function addDirectXrpPath(paths, xrpBalance) {
Expand Down
5 changes: 3 additions & 2 deletions src/core/pathfind.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ PathFind.prototype.create = function() {
};

PathFind.prototype.close = function() {
this.remote.request_path_find_close().broadcast().request();
this.removeAllListeners('update');
this.remote.requestPathFindClose().broadcast().request();
this.emit('end');
this.emit('close');
};
Expand All @@ -73,7 +74,7 @@ PathFind.prototype.notify_update = function(message) {
// looking for.
if (this.src_account === src_account &&
this.dst_account === dst_account &&
this.dst_amount.equals(dst_amount)) {
dst_amount.equals(this.dst_amount)) {
this.emit('update', message);
}
};
Expand Down
19 changes: 16 additions & 3 deletions src/core/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function Remote(options = {}) {
this._transaction_listeners = 0;
this._received_tx = new LRU({max: 100});
this._cur_path_find = null;
this._queued_path_finds = [];

if (this.local_signing) {
// Local signing implies local fees and sequences
Expand Down Expand Up @@ -1934,9 +1935,14 @@ Remote.prototype.findAccount = function(accountID) {
* @return {PathFind}
*/

function createPathFind(options_) {
function createPathFind(options_, onUpdate) {
const options = {};

if (this._cur_path_find !== null) {
this._queued_path_finds.push({options: options_, onUpdate: onUpdate});
return null;
}

if (_.isPlainObject(options_)) {
_.merge(options, options_);
} else {
Expand All @@ -1955,10 +1961,12 @@ function createPathFind(options_) {
this._cur_path_find.notify_superceded();
}

pathFind.create();
if (onUpdate) {
pathFind.on('update', onUpdate);
}

this._cur_path_find = pathFind;

pathFind.create();
return pathFind;
}

Expand Down Expand Up @@ -2369,6 +2377,11 @@ Remote.prototype.requestPathFindClose = function(callback) {

request.message.subcommand = 'close';
request.callback(callback);
this._cur_path_find = null; // TODO: wait for confirmation (RIPD-1013)
if (this._queued_path_finds.length > 0) {
const pathfind = this._queued_path_finds.shift();
this.createPathFind(pathfind.options, pathfind.onUpdate);
}

return request;
};
Expand Down
24 changes: 12 additions & 12 deletions test/fixtures/api/requests/getpaths/xrp2xrp-not-enough.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J"
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "1000002",
"currency": "XRP"
}
}
}
{
"source": {
"address": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J"
},
"destination": {
"address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"amount": {
"value": "1000002",
"currency": "XRP"
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/api/requests/getpaths/xrp2xrp.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"currency": "XRP"
}
}
}
}
8 changes: 4 additions & 4 deletions test/fixtures/api/rippled/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ module.exports = {
book_offers: require('./book-offers'),
server_info: require('./server-info'),
server_info_error: require('./server-info-error'),
ripple_path_find: {
generate: require('./ripple-path-find'),
sendUSD: require('./ripple-path-find-send-usd'),
XrpToXrp: require('./ripple-path-find-xrp-to-xrp')
path_find: {
generate: require('./path-find'),
sendUSD: require('./path-find-send-usd'),
XrpToXrp: require('./path-find-xrp-to-xrp')
},
tx: {
Payment: require('./tx/payment.json'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"id": 0,
"result": {
"full_reply": true,
"alternatives": [
{
"paths_canonical": [],
Expand Down Expand Up @@ -72,6 +73,12 @@
}
}
],
"source_account": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo",
"destination_amount": {
"currency": "USD",
"value": "0.000001",
"issuer": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"
},
"destination_account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"destination_currencies": [
"JOE",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"id": 1,
"result": {
"full_reply": true,
"alternatives": [],
"source_account": "rwBYyfufTzk77zUSKEu4MvixfarC35av1J",
"destination_amount": "2",
"destination_account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"destination_currencies": [
"JOE",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ function(request_id, sendingAccount, destinationAccount, destinationAmount) {
'status': 'success',
'type': 'response',
'result': {
'full_reply': true,
'source_account': sendingAccount,
'destination_amount': destinationAmount,
'alternatives': [
{
'paths_canonical': [],
Expand Down Expand Up @@ -308,6 +311,7 @@ function(request_id, sendingAccount, destinationAccount) {
'status': 'success',
'type': 'response',
'result': {
'full_reply': true,
'alternatives': [
{
'paths_canonical': [],
Expand Down
21 changes: 15 additions & 6 deletions test/mock-rippled.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ function isBTC(json) {
return json === 'BTC' || json === '0000000000000000000000004254430000000000';
}

function createResponse(request, response) {
return JSON.stringify(_.assign({}, response, {id: request.id}));
function createResponse(request, response, overrides={}) {
const result = _.assign({}, response.result, overrides);
const change = response.result && !_.isEmpty(overrides) ?
{id: request.id, result: result} : {id: request.id};
return JSON.stringify(_.assign({}, response, change));
}

module.exports = function(port) {
Expand Down Expand Up @@ -216,14 +219,20 @@ module.exports = function(port) {
}
});

mock.on('request_ripple_path_find', function(request, conn) {
mock.on('request_path_find', function(request, conn) {
let response = null;
if (request.subcommand === 'close') {
return;
}
if (request.source_account === addresses.OTHER_ACCOUNT) {
response = createResponse(request, fixtures.ripple_path_find.sendUSD);
response = createResponse(request, fixtures.path_find.sendUSD);
} else if (request.source_account === addresses.THIRD_ACCOUNT) {
response = createResponse(request, fixtures.ripple_path_find.XrpToXrp);
response = createResponse(request, fixtures.path_find.XrpToXrp, {
destination_amount: request.destination_amount,
destination_address: request.destination_address
});
} else {
response = fixtures.ripple_path_find.generate.generateIOUPaymentPaths(
response = fixtures.path_find.generate.generateIOUPaymentPaths(
request.id, request.source_account, request.destination_account,
request.destination_amount);
}
Expand Down

0 comments on commit 5cce850

Please sign in to comment.