Add PendingLedgerVersionError

MissingLedgerHistoryError - no minLedgerVersion or maxLedgerVersion
There is a ledger gap, but a range should be provided to narrow down the range
of the gap.

MissingLedgerHistoryError
When requesting a tx, if maxLedgerVersion and minLedgerVersion provided, this
means there is a ledger gap in the provided range.

PendingLedgerVersionError:
If maxLedgerVersion provided, check if ledger is ahead of the server's last
validated ledger.
This commit is contained in:
Alan Cohen
2015-09-15 12:06:42 -07:00
parent b43c4a7ad4
commit 60f2419b5c
4 changed files with 193 additions and 144 deletions

View File

@@ -58,6 +58,13 @@ function MissingLedgerHistoryError(message) {
MissingLedgerHistoryError.prototype = new RippleError();
MissingLedgerHistoryError.prototype.name = 'MissingLedgerHistoryError';
function PendingLedgerVersionError(message) {
this.message = message ||
'maxLedgerVersion is greater than server\'s most recent validated ledger';
}
PendingLedgerVersionError.prototype = new RippleError();
PendingLedgerVersionError.prototype.name = 'PendingLedgerVersionError';
/**
* Request timed out
*/
@@ -82,6 +89,7 @@ module.exports = {
TransactionError,
RippledNetworkError,
NotFoundError,
PendingLedgerVersionError,
MissingLedgerHistoryError,
TimeOutError,
ApiError,

View File

@@ -52,8 +52,8 @@ function getTransactionAsync(identifier: string, options: TransactionOptions,
validate.getTransactionOptions(options);
const remote = this.remote;
const maxLedgerVersion = Math.min(options.maxLedgerVersion || Infinity,
remote.getLedgerSequence());
const maxLedgerVersion =
options.maxLedgerVersion || remote.getLedgerSequence();
function callbackWrapper(error_?: Error, tx?: Object) {
let error = error_;
@@ -67,13 +67,19 @@ function getTransactionAsync(identifier: string, options: TransactionOptions,
error = new errors.NotFoundError('Transaction not found');
}
// Missing complete ledger range
if (error instanceof errors.NotFoundError
&& !utils.hasCompleteLedgerRange(remote,
options.minLedgerVersion, maxLedgerVersion)) {
callback(new errors.MissingLedgerHistoryError('Transaction not found,'
+ ' but the server\'s ledger history is incomplete'));
&& !utils.hasCompleteLedgerRange(remote, options.minLedgerVersion,
maxLedgerVersion)) {
if (utils.isPendingLedgerVersion(remote, maxLedgerVersion)) {
callback(new errors.PendingLedgerVersionError());
} else {
callback(new errors.MissingLedgerHistoryError());
}
// Transaction is found, but not in specified range
} else if (!error && tx && !isTransactionInRange(tx, options)) {
callback(new errors.NotFoundError('Transaction not found'));
// Transaction is not found
} else if (error) {
convertErrors(callback)(error);
} else if (!tx) {

View File

@@ -99,12 +99,19 @@ function compareTransactions(first: Outcome, second: Outcome): number {
function hasCompleteLedgerRange(remote: Remote, minLedgerVersion?: number,
maxLedgerVersion?: number
): boolean {
const firstLedgerVersion = 32570; // earlier versions have been lost
return remote.getServer().hasLedgerRange(
minLedgerVersion || firstLedgerVersion,
maxLedgerVersion || remote.getLedgerSequence());
}
function isPendingLedgerVersion(remote: Remote, maxLedgerVersion: ?number
): boolean {
const currentLedger = remote.getLedgerSequence();
return currentLedger < (maxLedgerVersion || 0);
}
module.exports = {
getXRPBalance,
compareTransactions,
@@ -112,6 +119,7 @@ module.exports = {
renameCounterpartyToIssuerInOrder,
getRecursive,
hasCompleteLedgerRange,
isPendingLedgerVersion,
promisify: common.promisify,
clamp: clamp,
common: common

View File

@@ -160,6 +160,7 @@ describe('RippleAPI', function() {
_.partial(checkResult, responses.getBalances, 'getBalances'));
});
describe('getTransaction', () => {
it('getTransaction - payment', function() {
return this.api.getTransaction(hashes.VALID_TRANSACTION_HASH).then(
_.partial(checkResult, responses.getTransaction.payment,
@@ -282,6 +283,31 @@ describe('RippleAPI', function() {
});
});
it('getTransaction - missing ledger history with ledger range', function() {
const hash = hashes.NOTFOUND_TRANSACTION_HASH;
const options = {
minLedgerVersion: 32569,
maxLedgerVersion: 32571
};
return this.api.getTransaction(hash, options).then(() => {
assert(false, 'Should throw MissingLedgerHistoryError');
}).catch(error => {
assert(error instanceof this.api.errors.MissingLedgerHistoryError);
});
});
it('getTransaction - not found - future maxLedgerVersion', function() {
const hash = hashes.NOTFOUND_TRANSACTION_HASH;
const options = {
maxLedgerVersion: 99999999999
};
return this.api.getTransaction(hash, options).then(() => {
assert(false, 'Should throw PendingLedgerVersionError');
}).catch(error => {
assert(error instanceof this.api.errors.PendingLedgerVersionError);
});
});
it('getTransaction - ledger_index not found', function() {
const hash =
'4FB3ADF22F3C605E23FAEFAA185F3BD763C4692CAC490D9819D117CD33BFAA11';
@@ -313,6 +339,7 @@ describe('RippleAPI', function() {
assert(error instanceof this.api.errors.ApiError);
});
});
});
it('getTransactions', function() {
const options = {types: ['payment', 'order'], initiated: true, limit: 2};