diff --git a/src/core/autobridgecalculator.js b/src/core/autobridgecalculator.js index 53541505..d3848337 100644 --- a/src/core/autobridgecalculator.js +++ b/src/core/autobridgecalculator.js @@ -3,6 +3,7 @@ const _ = require('lodash'); const assert = require('assert'); const Amount = require('./amount').Amount; +const UInt160 = require('./uint160').UInt160; const Utils = require('./orderbookutils'); function assertValidNumber(number, message) { @@ -42,8 +43,6 @@ const NULL_AMOUNT = Utils.normalizeAmount('0'); */ AutobridgeCalculator.prototype.calculate = function(callback) { - this._oldMode = Amount.strict_mode; - Amount.strict_mode = false; const legOnePointer = 0; const legTwoPointer = 0; @@ -60,6 +59,12 @@ AutobridgeCalculator.prototype._calculateInternal = function( legOnePointer_, legTwoPointer_, offersAutobridged, callback ) { + // Amount class is calling _check_limits after each operation in strict mode, + // and _check_limits is very computationally expensive, so we turning it off + // whle doing calculations + this._oldMode = Amount.strict_mode; + Amount.strict_mode = false; + let legOnePointer = legOnePointer_; let legTwoPointer = legTwoPointer_; @@ -70,12 +75,8 @@ AutobridgeCalculator.prototype._calculateInternal = function( // of execution so user's browser stays responsive const lasted = (Date.now() - startTime); if (lasted > 30) { - setTimeout(() => { - this._oldMode = Amount.strict_mode; - Amount.strict_mode = false; - this._calculateInternal(legOnePointer, legTwoPointer, offersAutobridged, - callback); - }, 0); + setTimeout(this._calculateInternal.bind(this, legOnePointer, + legTwoPointer, offersAutobridged, callback), 0); Amount.strict_mode = this._oldMode; return; diff --git a/src/core/orderbook.js b/src/core/orderbook.js index 399f80d2..ed080f78 100644 --- a/src/core/orderbook.js +++ b/src/core/orderbook.js @@ -88,7 +88,7 @@ function OrderBook(remote, this._gotOffersFromLegTwo = false; this._waitingForOffers = false; - this._offersModifiedAt = 0; + this._lastUpdateLedgerSequence = 0; this._transactionsLeft = 0; this._calculatorRunning = false; @@ -839,29 +839,29 @@ OrderBook.prototype.isBalanceChangeNode = function(node) { OrderBook.prototype._canRunAutobridgeCalc = function(): boolean { return !this._calculatorRunning; -} +}; OrderBook.prototype.onTransaction = function(transaction) { this.updateFundedAmounts(transaction); if (--this._transactionsLeft === 0 && !this._waitingForOffers) { - const lastClosedLedger = this._remote.getLedgerSequence() - 1; + const lastClosedLedger = this._remote.getLedgerSequence(); if (this._isAutobridgeable) { if (this._canRunAutobridgeCalc()) { - if (this._legOneBook._offersModifiedAt === lastClosedLedger || - this._legTwoBook._offersModifiedAt === lastClosedLedger + if (this._legOneBook._lastUpdateLedgerSequence === lastClosedLedger || + this._legTwoBook._lastUpdateLedgerSequence === lastClosedLedger ) { this.computeAutobridgedOffersWrapper(); - } else if (this._offersModifiedAt === lastClosedLedger) { + } else if (this._lastUpdateLedgerSequence === lastClosedLedger) { this.mergeDirectAndAutobridgedBooks(); } } - } else if (this._offersModifiedAt === lastClosedLedger) { + } else if (this._lastUpdateLedgerSequence === lastClosedLedger) { this.emit('model', this._offers); } } -} +}; /** * Updates funded amounts/balances using modified balance nodes @@ -978,7 +978,7 @@ OrderBook.prototype.onLedgerClosed = function(message) { return; } this._transactionsLeft = message.txn_count; -} +}; /** * Notify orderbook of a relevant transaction @@ -1080,7 +1080,7 @@ OrderBook.prototype.notify = function(transaction) { this.emit('transaction', transaction); - this._offersModifiedAt = this._remote.getLedgerSequence() - 1; + this._lastUpdateLedgerSequence = this._remote.getLedgerSequence(); if (!takerGetsTotal.is_zero()) { this.emit('trade', takerPaysTotal, takerGetsTotal); @@ -1370,7 +1370,7 @@ OrderBook.prototype.computeAutobridgedOffers = function(callback = function() {} assert(!this._currencyGets.is_native() && !this._currencyPays.is_native(), 'Autobridging is only for IOU:IOU orderbooks'); - + if (this._destroyed) { return; } @@ -1384,8 +1384,6 @@ OrderBook.prototype.computeAutobridgedOffers = function(callback = function() {} this._issuerPays ); - const lastClosedLedger = this._remote.getLedgerSequence() - 1; - autobridgeCalculator.calculate((autobridgedOffers) => { this._offersAutobridged = autobridgedOffers; callback(); diff --git a/test/orderbook-autobridge-test.js b/test/orderbook-autobridge-test.js index 271829fa..bb4bb51a 100644 --- a/test/orderbook-autobridge-test.js +++ b/test/orderbook-autobridge-test.js @@ -589,7 +589,7 @@ describe('OrderBook Autobridging', function() { assert(book._offersAutobridged[2].autobridged); - done(); + done(); }); });