mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 12:45:50 +00:00
fix bugs in orderbook subscription
This commit is contained in:
@@ -23,6 +23,7 @@ const AutobridgeCalculator = require('./autobridgecalculator');
|
|||||||
const OrderBookUtils = require('./orderbookutils');
|
const OrderBookUtils = require('./orderbookutils');
|
||||||
const log = require('./log').internal.sub('orderbook');
|
const log = require('./log').internal.sub('orderbook');
|
||||||
const {IOUValue} = require('ripple-lib-value');
|
const {IOUValue} = require('ripple-lib-value');
|
||||||
|
const RippleError = require('./rippleerror').RippleError;
|
||||||
|
|
||||||
function _sortOffers(a, b) {
|
function _sortOffers(a, b) {
|
||||||
const aQuality = OrderBookUtils.getOfferQuality(a, this._currencyGets);
|
const aQuality = OrderBookUtils.getOfferQuality(a, this._currencyGets);
|
||||||
@@ -256,6 +257,9 @@ OrderBook.offerRewrite = function(offer) {
|
|||||||
/**
|
/**
|
||||||
* Initialize orderbook. Get orderbook offers and subscribe to transactions
|
* Initialize orderbook. Get orderbook offers and subscribe to transactions
|
||||||
* @api private
|
* @api private
|
||||||
|
* NOTE: this method is not meant to be publicly used
|
||||||
|
* and it does not work for autobridged books since
|
||||||
|
* it does not add listeners for them
|
||||||
*/
|
*/
|
||||||
|
|
||||||
OrderBook.prototype.subscribe = function() {
|
OrderBook.prototype.subscribe = function() {
|
||||||
@@ -333,21 +337,23 @@ OrderBook.prototype.destroy = function() {
|
|||||||
* Request orderbook entries from server
|
* Request orderbook entries from server
|
||||||
*
|
*
|
||||||
* @param {Function} callback
|
* @param {Function} callback
|
||||||
|
* @param {boolean} internal - internal request made on 'subscribe'
|
||||||
*/
|
*/
|
||||||
|
|
||||||
OrderBook.prototype.requestOffers = function(callback = function() {},
|
OrderBook.prototype.requestOffers = function(callback = function() {},
|
||||||
internal = false) {
|
internal = false) {
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
if (!this._remote.isConnected()) {
|
if (!this._remote.isConnected() && !internal) {
|
||||||
// do not make request if not online.
|
// do not make request if not online.
|
||||||
// that requests will be queued and
|
// that requests will be queued and
|
||||||
// eventually all of them will fire back
|
// eventually all of them will fire back
|
||||||
|
callback(new RippleError('remote is offline'));
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this._shouldSubscribe) {
|
if (!this._shouldSubscribe) {
|
||||||
callback(new Error('Should not request offers'));
|
callback(new RippleError('Should not request offers'));
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,7 +361,7 @@ OrderBook.prototype.requestOffers = function(callback = function() {},
|
|||||||
log.info('requesting offers', this._key);
|
log.info('requesting offers', this._key);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._synchronized = false;
|
this._synced = false;
|
||||||
|
|
||||||
if (this._isAutobridgeable && !internal) {
|
if (this._isAutobridgeable && !internal) {
|
||||||
this._gotOffersFromLegOne = false;
|
this._gotOffersFromLegOne = false;
|
||||||
@@ -375,7 +381,7 @@ OrderBook.prototype.requestOffers = function(callback = function() {},
|
|||||||
|
|
||||||
if (!Array.isArray(res.offers)) {
|
if (!Array.isArray(res.offers)) {
|
||||||
// XXX What now?
|
// XXX What now?
|
||||||
callback(new Error('Invalid response'));
|
callback(new RippleError('Invalid response'));
|
||||||
self.emit('model', []);
|
self.emit('model', []);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1390,7 +1396,7 @@ OrderBook.prototype.computeAutobridgedOffers = function(callback = function() {}
|
|||||||
|
|
||||||
OrderBook.prototype.computeAutobridgedOffersWrapper = function() {
|
OrderBook.prototype.computeAutobridgedOffersWrapper = function() {
|
||||||
if (!this._gotOffersFromLegOne || !this._gotOffersFromLegTwo ||
|
if (!this._gotOffersFromLegOne || !this._gotOffersFromLegTwo ||
|
||||||
!this._synchronized || this._destroyed || this._calculatorRunning
|
!this._synced || this._destroyed || this._calculatorRunning
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ const fixtures = require('./fixtures/orderbook');
|
|||||||
const IOUValue = require('ripple-lib-value').IOUValue;
|
const IOUValue = require('ripple-lib-value').IOUValue;
|
||||||
|
|
||||||
describe('OrderBook', function() {
|
describe('OrderBook', function() {
|
||||||
this.timeout(0);
|
|
||||||
|
|
||||||
function createRemote() {
|
function createRemote() {
|
||||||
const remote = new Remote();
|
const remote = new Remote();
|
||||||
@@ -68,40 +67,155 @@ describe('OrderBook', function() {
|
|||||||
assert(book.isValid());
|
assert(book.isValid());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Automatic subscription (based on listeners)', function(done) {
|
|
||||||
const book = createRemote().createOrderBook({
|
|
||||||
currency_gets: 'XRP',
|
|
||||||
issuer_pays: addresses.ISSUER,
|
|
||||||
currency_pays: 'BTC'
|
|
||||||
});
|
|
||||||
|
|
||||||
book.subscribe = function() {
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
|
|
||||||
book.on('model', function() {});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Subscribe', function(done) {
|
it('Subscribe', function(done) {
|
||||||
const book = createRemote().createOrderBook({
|
const remote = createRemote();
|
||||||
currency_gets: 'XRP',
|
const book = remote.createOrderBook({
|
||||||
issuer_pays: addresses.ISSUER,
|
currency_pays: 'XRP',
|
||||||
currency_pays: 'BTC'
|
issuer_gets: addresses.ISSUER,
|
||||||
|
currency_gets: 'BTC'
|
||||||
});
|
});
|
||||||
|
|
||||||
let requestedOffers = false;
|
remote.request = function(request) {
|
||||||
|
const response = {};
|
||||||
|
|
||||||
book.subscribeTransactions = function() {
|
if (request.message.command === 'account_info') {
|
||||||
assert(requestedOffers);
|
response.account_data = {
|
||||||
done();
|
TransferRate: 1002000000
|
||||||
};
|
};
|
||||||
|
|
||||||
book.requestOffers = function(callback) {
|
} else if (request.message.command === 'book_offers') {
|
||||||
requestedOffers = true;
|
response.offers = [];
|
||||||
callback();
|
}
|
||||||
|
|
||||||
|
request.emit('success', response);
|
||||||
};
|
};
|
||||||
|
|
||||||
book.subscribe();
|
book.subscribe();
|
||||||
|
assert.strictEqual(book._subscribed, true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Subscribe - gets XRP', function(done) {
|
||||||
|
const remote = createRemote();
|
||||||
|
const book = remote.createOrderBook({
|
||||||
|
currency_gets: 'XRP',
|
||||||
|
issuer_pays: addresses.ISSUER,
|
||||||
|
currency_pays: 'BTC'
|
||||||
|
});
|
||||||
|
|
||||||
|
remote.isConnected = function() {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
remote.request = function(request) {
|
||||||
|
const response = {};
|
||||||
|
if (request.message.command === 'book_offers') {
|
||||||
|
response.offers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
request.emit('success', response);
|
||||||
|
};
|
||||||
|
|
||||||
|
book.subscribe();
|
||||||
|
assert.strictEqual(book._subscribed, true, '_subscribed');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Subscribe - autobridged', function(done) {
|
||||||
|
|
||||||
|
const remote = createRemote();
|
||||||
|
const book = remote.createOrderBook({
|
||||||
|
currency_pays: 'USD',
|
||||||
|
issuer_pays: addresses.ISSUER,
|
||||||
|
currency_gets: 'BTC',
|
||||||
|
issuer_gets: addresses.ISSUER
|
||||||
|
});
|
||||||
|
|
||||||
|
remote.request = function(request) {
|
||||||
|
const response = {};
|
||||||
|
|
||||||
|
if (request.message.command === 'account_info') {
|
||||||
|
response.account_data = {
|
||||||
|
TransferRate: 1002000000
|
||||||
|
};
|
||||||
|
|
||||||
|
} else if (request.message.command === 'book_offers') {
|
||||||
|
response.offers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
request.emit('success', response);
|
||||||
|
};
|
||||||
|
|
||||||
|
book.subscribe();
|
||||||
|
assert.strictEqual(book._subscribed, true, '_subscribed');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Automatic subscription (based on listeners)', function(done) {
|
||||||
|
this.timeout(100);
|
||||||
|
|
||||||
|
const remote = createRemote();
|
||||||
|
const book = remote.createOrderBook({
|
||||||
|
currency_gets: 'XRP',
|
||||||
|
issuer_pays: addresses.ISSUER,
|
||||||
|
currency_pays: 'BTC'
|
||||||
|
});
|
||||||
|
|
||||||
|
remote.request = function(request) {
|
||||||
|
const response = {};
|
||||||
|
|
||||||
|
if (request.message.command === 'account_info') {
|
||||||
|
response.account_data = {
|
||||||
|
TransferRate: 1002000000
|
||||||
|
};
|
||||||
|
|
||||||
|
} else if (request.message.command === 'book_offers') {
|
||||||
|
response.offers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
setImmediate(function() {
|
||||||
|
request.emit('success', response);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
book.on('model', function(offers) {
|
||||||
|
assert.strictEqual(offers.length, 0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Automatic subscription (based on listeners) - autobridged', function(done) {
|
||||||
|
this.timeout(100);
|
||||||
|
|
||||||
|
const remote = createRemote();
|
||||||
|
const book = remote.createOrderBook({
|
||||||
|
currency_pays: 'USD',
|
||||||
|
issuer_pays: addresses.ISSUER,
|
||||||
|
currency_gets: 'BTC',
|
||||||
|
issuer_gets: addresses.ISSUER
|
||||||
|
});
|
||||||
|
|
||||||
|
remote.request = function(request) {
|
||||||
|
const response = {};
|
||||||
|
|
||||||
|
if (request.message.command === 'account_info') {
|
||||||
|
response.account_data = {
|
||||||
|
TransferRate: 1002000000
|
||||||
|
};
|
||||||
|
|
||||||
|
} else if (request.message.command === 'book_offers') {
|
||||||
|
response.offers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
setImmediate(function() {
|
||||||
|
request.emit('success', response);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
book.on('model', function(offers) {
|
||||||
|
assert.strictEqual(offers.length, 0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Unsubscribe', function(done) {
|
it('Unsubscribe', function(done) {
|
||||||
|
|||||||
Reference in New Issue
Block a user