Check for ledger gaps in getTransactions

This commit is contained in:
Chris Clark
2015-07-09 14:28:42 -07:00
parent 6a3eb7b9b9
commit 11ca00bb45
11 changed files with 30 additions and 24 deletions

View File

@@ -52,7 +52,8 @@ NotFoundError.prototype = new RippleError();
NotFoundError.prototype.name = 'NotFoundError'; NotFoundError.prototype.name = 'NotFoundError';
function MissingLedgerHistoryError(message) { function MissingLedgerHistoryError(message) {
this.message = message; this.message = message ||
'Server is missing ledger history in the specified range';
} }
MissingLedgerHistoryError.prototype = new RippleError(); MissingLedgerHistoryError.prototype = new RippleError();
MissingLedgerHistoryError.prototype.name = 'MissingLedgerHistoryError'; MissingLedgerHistoryError.prototype.name = 'MissingLedgerHistoryError';

View File

@@ -5,14 +5,6 @@ const utils = require('./utils');
const parseTransaction = require('./parse/transaction'); const parseTransaction = require('./parse/transaction');
const validate = utils.common.validate; const validate = utils.common.validate;
const errors = utils.common.errors; 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) { function attachTransactionDate(remote, tx, callback) {
if (tx.date) { if (tx.date) {
@@ -50,7 +42,8 @@ function getTransaction(identifier, options, callback) {
function callbackWrapper(error, tx) { function callbackWrapper(error, tx) {
if (error instanceof errors.NotFoundError if (error instanceof errors.NotFoundError
&& !hasCompleteLedgerRange(remote, options)) { && !utils.hasCompleteLedgerRange(remote,
options.minLedgerVersion, options.maxLedgerVersion)) {
callback(new errors.MissingLedgerHistoryError('Transaction not found,' callback(new errors.MissingLedgerHistoryError('Transaction not found,'
+ ' but the server\'s ledger history is incomplete')); + ' but the server\'s ledger history is incomplete'));
} else if (!error && !isTransactionInRange(tx, options)) { } else if (!error && !isTransactionInRange(tx, options)) {

View File

@@ -77,6 +77,11 @@ function getTransactions(address, options, callback) {
validate.getTransactionsOptions(options); validate.getTransactionsOptions(options);
const remote = this.remote; const remote = this.remote;
if (!utils.hasCompleteLedgerRange(remote, options.minLedgerVersion,
options.maxLedgerVersion)) {
callback(new utils.common.errors.MissingLedgerHistoryError());
}
if (options.start) { if (options.start) {
getTransaction.bind(this)(options.start, {}, (error, tx) => { getTransaction.bind(this)(options.start, {}, (error, tx) => {
if (error) { if (error) {

View File

@@ -71,12 +71,20 @@ function compareTransactions(first, second) {
return Number(first.ledgerVersion) < Number(second.ledgerVersion) ? -1 : 1; 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 = { module.exports = {
getXRPBalance: getXRPBalance, getXRPBalance,
compareTransactions: compareTransactions, compareTransactions,
renameCounterpartyToIssuer: renameCounterpartyToIssuer, renameCounterpartyToIssuer,
renameCounterpartyToIssuerInOrder: renameCounterpartyToIssuerInOrder, renameCounterpartyToIssuerInOrder,
getRecursive: getRecursive, getRecursive,
hasCompleteLedgerRange,
wrapCatch: common.wrapCatch, wrapCatch: common.wrapCatch,
common: common common: common
}; };

View File

@@ -513,15 +513,14 @@ Remote.prototype._handleMessage = function(message, server) {
} }
}; };
Remote.prototype.getLedgerSequence = function() { Remote.prototype.getLedgerSequence = function() {
if (!this._ledger_current_index) { if (!this._ledger_current_index) {
throw new Error('Ledger sequence has not yet been initialized'); 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 * Handle server ledger_closed event
* *

View File

@@ -192,6 +192,6 @@ describe('RippleAPI', function() {
}); });
it('getLedgerVersion', function() { it('getLedgerVersion', function() {
assert.strictEqual(this.api.getLedgerVersion(), 8819952); assert.strictEqual(this.api.getLedgerVersion(), 8819951);
}); });
}); });

View File

@@ -2,7 +2,7 @@
"Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", "Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"Fee": "12", "Fee": "12",
"Flags": 131072, "Flags": 131072,
"LastLedgerSequence": 8820052, "LastLedgerSequence": 8820051,
"Sequence": 23, "Sequence": 23,
"TakerGets": "2000000", "TakerGets": "2000000",
"TakerPays": { "TakerPays": {

View File

@@ -3,7 +3,7 @@
"TransactionType": "OfferCancel", "TransactionType": "OfferCancel",
"Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", "Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"OfferSequence": 23, "OfferSequence": 23,
"LastLedgerSequence": 8820052, "LastLedgerSequence": 8820051,
"Fee": "12", "Fee": "12",
"Sequence": 23 "Sequence": 23
} }

View File

@@ -8,7 +8,7 @@
"currency": "USD", "currency": "USD",
"issuer": "rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM" "issuer": "rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM"
}, },
"LastLedgerSequence": 8820052, "LastLedgerSequence": 8820051,
"SendMax": { "SendMax": {
"currency": "USD", "currency": "USD",
"issuer": "rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM", "issuer": "rMH4UxPrbuMa1spCBR98hLLyNJp4d8p4tM",

View File

@@ -4,7 +4,7 @@
"Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", "Account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
"Domain": "726970706C652E636F6D", "Domain": "726970706C652E636F6D",
"Flags": 0, "Flags": 0,
"LastLedgerSequence": 8820052, "LastLedgerSequence": 8820051,
"Fee": "12", "Fee": "12",
"Sequence": 23 "Sequence": 23
} }

View File

@@ -9,7 +9,7 @@
}, },
"QualityIn": 500000000, "QualityIn": 500000000,
"QualityOut": 500000000, "QualityOut": 500000000,
"LastLedgerSequence": 8820052, "LastLedgerSequence": 8820051,
"Fee": "12", "Fee": "12",
"Sequence": 23 "Sequence": 23
} }