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';
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';

View File

@@ -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)) {

View File

@@ -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) {

View File

@@ -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
};

View File

@@ -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
*

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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