diff --git a/src/api/common/errors.js b/src/api/common/errors.js index 53c07d97..e5194c07 100644 --- a/src/api/common/errors.js +++ b/src/api/common/errors.js @@ -52,7 +52,8 @@ NotFoundError.prototype = new RippleError(); NotFoundError.prototype.name = 'NotFoundError'; function MissingLedgerHistoryError(message) { - this.message = message; + this.message = message || + 'Server is missing ledger history in the specified range'; } MissingLedgerHistoryError.prototype = new RippleError(); MissingLedgerHistoryError.prototype.name = 'MissingLedgerHistoryError'; diff --git a/src/api/ledger/transaction.js b/src/api/ledger/transaction.js index 9088cc8d..4a89a75c 100644 --- a/src/api/ledger/transaction.js +++ b/src/api/ledger/transaction.js @@ -5,14 +5,6 @@ const utils = require('./utils'); const parseTransaction = require('./parse/transaction'); const validate = utils.common.validate; const errors = utils.common.errors; -const MIN_LEDGER_VERSION = 32570; // earlier versions have been completely lost - -function hasCompleteLedgerRange(remote, options) { - const minLedgerVersion = options.minLedgerVersion || MIN_LEDGER_VERSION; - const maxLedgerVersion = options.maxLedgerVersion - || remote.getLedgerSequence(); - return remote.getServer().hasLedgerRange(minLedgerVersion, maxLedgerVersion); -} function attachTransactionDate(remote, tx, callback) { if (tx.date) { @@ -50,7 +42,8 @@ function getTransaction(identifier, options, callback) { function callbackWrapper(error, tx) { if (error instanceof errors.NotFoundError - && !hasCompleteLedgerRange(remote, options)) { + && !utils.hasCompleteLedgerRange(remote, + options.minLedgerVersion, options.maxLedgerVersion)) { callback(new errors.MissingLedgerHistoryError('Transaction not found,' + ' but the server\'s ledger history is incomplete')); } else if (!error && !isTransactionInRange(tx, options)) { diff --git a/src/api/ledger/transactions.js b/src/api/ledger/transactions.js index 59cb477a..e2eea955 100644 --- a/src/api/ledger/transactions.js +++ b/src/api/ledger/transactions.js @@ -77,6 +77,11 @@ function getTransactions(address, options, callback) { validate.getTransactionsOptions(options); const remote = this.remote; + if (!utils.hasCompleteLedgerRange(remote, options.minLedgerVersion, + options.maxLedgerVersion)) { + callback(new utils.common.errors.MissingLedgerHistoryError()); + } + if (options.start) { getTransaction.bind(this)(options.start, {}, (error, tx) => { if (error) { diff --git a/src/api/ledger/utils.js b/src/api/ledger/utils.js index e196336d..efed8d22 100644 --- a/src/api/ledger/utils.js +++ b/src/api/ledger/utils.js @@ -71,12 +71,20 @@ function compareTransactions(first, second) { return Number(first.ledgerVersion) < Number(second.ledgerVersion) ? -1 : 1; } +function hasCompleteLedgerRange(remote, minLedgerVersion, maxLedgerVersion) { + const firstLedgerVersion = 32570; // earlier versions have been lost + return remote.getServer().hasLedgerRange( + minLedgerVersion || firstLedgerVersion, + maxLedgerVersion || remote.getLedgerSequence()); +} + module.exports = { - getXRPBalance: getXRPBalance, - compareTransactions: compareTransactions, - renameCounterpartyToIssuer: renameCounterpartyToIssuer, - renameCounterpartyToIssuerInOrder: renameCounterpartyToIssuerInOrder, - getRecursive: getRecursive, + getXRPBalance, + compareTransactions, + renameCounterpartyToIssuer, + renameCounterpartyToIssuerInOrder, + getRecursive, + hasCompleteLedgerRange, wrapCatch: common.wrapCatch, common: common }; diff --git a/src/core/remote.js b/src/core/remote.js index 7b9c2f7b..c66d330f 100644 --- a/src/core/remote.js +++ b/src/core/remote.js @@ -513,15 +513,14 @@ Remote.prototype._handleMessage = function(message, server) { } }; - Remote.prototype.getLedgerSequence = function() { if (!this._ledger_current_index) { throw new Error('Ledger sequence has not yet been initialized'); } - return this._ledger_current_index; + // the "current" ledger is the one after the most recently closed ledger + return this._ledger_current_index - 1; }; - /** * Handle server ledger_closed event * diff --git a/test/api-test.js b/test/api-test.js index bde6436d..62473f17 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -192,6 +192,6 @@ describe('RippleAPI', function() { }); it('getLedgerVersion', function() { - assert.strictEqual(this.api.getLedgerVersion(), 8819952); + assert.strictEqual(this.api.getLedgerVersion(), 8819951); }); }); diff --git a/test/fixtures/order-response.json b/test/fixtures/order-response.json index 5ac368b4..ffe3f6a4 100644 --- a/test/fixtures/order-response.json +++ b/test/fixtures/order-response.json @@ -2,7 +2,7 @@ "Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", "Fee": "12", "Flags": 131072, - "LastLedgerSequence": 8820052, + "LastLedgerSequence": 8820051, "Sequence": 23, "TakerGets": "2000000", "TakerPays": { diff --git a/test/fixtures/ordercancellation-response.json b/test/fixtures/ordercancellation-response.json index d48e3ac9..49940b9f 100644 --- a/test/fixtures/ordercancellation-response.json +++ b/test/fixtures/ordercancellation-response.json @@ -3,7 +3,7 @@ "TransactionType": "OfferCancel", "Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", "OfferSequence": 23, - "LastLedgerSequence": 8820052, + "LastLedgerSequence": 8820051, "Fee": "12", "Sequence": 23 } diff --git a/test/fixtures/payment-response.json b/test/fixtures/payment-response.json index a187c254..dbb748f9 100644 --- a/test/fixtures/payment-response.json +++ b/test/fixtures/payment-response.json @@ -8,7 +8,7 @@ "currency": "USD", "issuer": "rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM" }, - "LastLedgerSequence": 8820052, + "LastLedgerSequence": 8820051, "SendMax": { "currency": "USD", "issuer": "rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM", diff --git a/test/fixtures/settings-response.json b/test/fixtures/settings-response.json index 2433e8ac..97949b71 100644 --- a/test/fixtures/settings-response.json +++ b/test/fixtures/settings-response.json @@ -4,7 +4,7 @@ "Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", "Domain": "726970706C652E636F6D", "Flags": 0, - "LastLedgerSequence": 8820052, + "LastLedgerSequence": 8820051, "Fee": "12", "Sequence": 23 } diff --git a/test/fixtures/trustline-response.json b/test/fixtures/trustline-response.json index e6c5414f..6187bd84 100644 --- a/test/fixtures/trustline-response.json +++ b/test/fixtures/trustline-response.json @@ -9,7 +9,7 @@ }, "QualityIn": 500000000, "QualityOut": 500000000, - "LastLedgerSequence": 8820052, + "LastLedgerSequence": 8820051, "Fee": "12", "Sequence": 23 }