mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-23 13:45:48 +00:00
Cleanup
- Deprecate 'save' event - Add TransactionQueue.getMinLedger(), use this as ledger_index_min in account_tx request on reconnect - tx.sign() no longer accepts a callback - Add various setters and jsdoc to transaction.js - Normalize setters, e.g. sourceTag() and destinationTag() - Minor optimization in call to tx.hash() in TransactionManager prior to submit; allow `serialized` argument to tx.hash() such that the transaction is not serialized twice
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,6 @@
|
||||
var util = require('util');
|
||||
var assert = require('assert');
|
||||
var async = require('async');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var Transaction = require('./transaction').Transaction;
|
||||
var RippleError = require('./rippleerror').RippleError;
|
||||
@@ -24,28 +26,115 @@ function TransactionManager(account) {
|
||||
this._submissionTimeout = this._remote._submission_timeout;
|
||||
this._pending = new PendingQueue();
|
||||
|
||||
// Query remote server for next account sequence number
|
||||
this._loadSequence();
|
||||
this._account.on('transaction-outbound', function(res) {
|
||||
self._transactionReceived(res);
|
||||
});
|
||||
|
||||
function transactionReceived(res) {
|
||||
var transaction = TransactionManager.normalizeTransaction(res);
|
||||
var sequence = transaction.tx_json.Sequence;
|
||||
var hash = transaction.tx_json.hash;
|
||||
this._remote.on('load_changed', function(load) {
|
||||
self._adjustFees(load);
|
||||
});
|
||||
|
||||
function updatePendingStatus(ledger) {
|
||||
self._updatePendingStatus(ledger);
|
||||
};
|
||||
|
||||
this._remote.on('ledger_closed', updatePendingStatus);
|
||||
|
||||
this._remote.on('disconnect', function() {
|
||||
self._remote.removeListener('ledger_closed', updatePendingStatus);
|
||||
self._remote.once('connect', function() {
|
||||
self._remote.on('ledger_closed', updatePendingStatus);
|
||||
self._handleReconnect();
|
||||
});
|
||||
});
|
||||
|
||||
// Query server for next account transaction sequence
|
||||
this._loadSequence();
|
||||
};
|
||||
|
||||
util.inherits(TransactionManager, EventEmitter);
|
||||
|
||||
/**
|
||||
* Normalize transactions received from account transaction stream and
|
||||
* account_tx
|
||||
*
|
||||
* @param {Transaction}
|
||||
* @return {Transaction} normalized
|
||||
* @api private
|
||||
*/
|
||||
|
||||
TransactionManager.normalizeTransaction = function(tx) {
|
||||
var transaction = { };
|
||||
var keys = Object.keys(tx);
|
||||
|
||||
for (var i=0; i<keys.length; i++) {
|
||||
var k = keys[i];
|
||||
switch (k) {
|
||||
case 'transaction':
|
||||
// Account transaction stream
|
||||
transaction.tx_json = tx[k];
|
||||
break;
|
||||
case 'tx':
|
||||
// account_tx response
|
||||
transaction.engine_result = tx.meta.TransactionResult;
|
||||
transaction.result = transaction.engine_result;
|
||||
transaction.tx_json = tx[k];
|
||||
transaction.hash = tx[k].hash;
|
||||
transaction.ledger_index = tx[k].ledger_index;
|
||||
transaction.type = 'transaction';
|
||||
transaction.validated = tx.validated;
|
||||
break;
|
||||
case 'meta':
|
||||
case 'metadata':
|
||||
transaction.metadata = tx[k];
|
||||
break;
|
||||
case 'mmeta':
|
||||
// Don't copy mmeta
|
||||
break;
|
||||
default:
|
||||
transaction[k] = tx[k];
|
||||
}
|
||||
}
|
||||
|
||||
return transaction;
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle received transaction from two possible sources
|
||||
*
|
||||
* + Account transaction stream (normal operation)
|
||||
* + account_tx (after reconnect)
|
||||
*
|
||||
* @param {Object} transaction
|
||||
* @api private
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._transactionReceived = function(tx) {
|
||||
var transaction = TransactionManager.normalizeTransaction(tx);
|
||||
|
||||
if (!transaction.validated) {
|
||||
// Transaction has not been validated
|
||||
return;
|
||||
}
|
||||
|
||||
self._pending.addReceivedSequence(sequence);
|
||||
if (transaction.tx_json.Account !== this._accountID) {
|
||||
// Received transaction's account does not match
|
||||
return;
|
||||
}
|
||||
|
||||
// ND: we need to check against all submissions IDs
|
||||
var submission = self._pending.getSubmission(hash);
|
||||
|
||||
if (self._remote.trace) {
|
||||
if (this._remote.trace) {
|
||||
log.info('transaction received:', transaction.tx_json);
|
||||
}
|
||||
|
||||
if (submission instanceof Transaction) {
|
||||
this._pending.addReceivedSequence(transaction.tx_json.Sequence);
|
||||
|
||||
var hash = transaction.tx_json.hash;
|
||||
var submission = this._pending.getSubmission(hash);
|
||||
|
||||
if (!(submission instanceof Transaction)) {
|
||||
// The received transaction does not correlate to one submitted
|
||||
return this._pending.addReceivedId(hash, transaction);
|
||||
}
|
||||
|
||||
// ND: A `success` handler will `finalize` this later
|
||||
switch (transaction.engine_result) {
|
||||
@@ -55,153 +144,80 @@ function TransactionManager(account) {
|
||||
default:
|
||||
submission.emit('error', transaction);
|
||||
}
|
||||
|
||||
} else {
|
||||
self._pending.addReceivedId(hash, transaction);
|
||||
}
|
||||
};
|
||||
|
||||
this._account.on('transaction-outbound', transactionReceived);
|
||||
|
||||
this._remote.on('load_changed', this._adjustFees.bind(this));
|
||||
|
||||
function updatePendingStatus(ledger) {
|
||||
self._pending.forEach(function(pending) {
|
||||
switch (ledger.ledger_index - pending.submitIndex) {
|
||||
case 8:
|
||||
pending.emit('lost', ledger);
|
||||
break;
|
||||
case 4:
|
||||
pending.emit('missing', ledger);
|
||||
break;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
this._remote.on('ledger_closed', updatePendingStatus);
|
||||
|
||||
function remoteReconnected(callback) {
|
||||
var callback = (typeof callback === 'function') ? callback : function(){};
|
||||
|
||||
if (!self._pending.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
//Load account transaction history
|
||||
var options = {
|
||||
account: self._accountID,
|
||||
ledger_index_min: -1,
|
||||
ledger_index_max: -1,
|
||||
binary: true,
|
||||
parseBinary: true,
|
||||
limit: 100,
|
||||
filter: 'outbound'
|
||||
};
|
||||
|
||||
function accountTx(err, transactions) {
|
||||
if (!err && Array.isArray(transactions.transactions)) {
|
||||
transactions.transactions.forEach(transactionReceived);
|
||||
}
|
||||
|
||||
self._remote.on('ledger_closed', updatePendingStatus);
|
||||
|
||||
//Load next transaction sequence
|
||||
self._loadSequence(self._resubmit.bind(self));
|
||||
|
||||
callback();
|
||||
};
|
||||
|
||||
self._remote.requestAccountTx(options, accountTx);
|
||||
|
||||
self.emit('reconnect');
|
||||
};
|
||||
|
||||
function remoteDisconnected() {
|
||||
self._remote.once('connect', remoteReconnected);
|
||||
self._remote.removeListener('ledger_closed', updatePendingStatus);
|
||||
};
|
||||
|
||||
this._remote.on('disconnect', remoteDisconnected);
|
||||
|
||||
function saveTransaction(transaction) {
|
||||
self._remote.storage.saveTransaction(transaction.summary());
|
||||
};
|
||||
|
||||
if (this._remote.storage) {
|
||||
this.on('save', saveTransaction);
|
||||
}
|
||||
};
|
||||
|
||||
util.inherits(TransactionManager, EventEmitter);
|
||||
/**
|
||||
* Adjust pending transactions' fees in real-time. This does not resubmit
|
||||
* pending transactions; they will be resubmitted periodically with an updated
|
||||
* fee (and as a consequence, a new transaction ID) if not already validated
|
||||
*
|
||||
* ND: note, that `Fee` is a component of a transactionID
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
//Normalize transactions received from account
|
||||
//transaction stream and account_tx
|
||||
TransactionManager.normalizeTransaction = function(tx) {
|
||||
var transaction = { };
|
||||
|
||||
Object.keys(tx).forEach(function(key) {
|
||||
transaction[key] = tx[key];
|
||||
});
|
||||
|
||||
if (!tx.engine_result) {
|
||||
// account_tx
|
||||
transaction = {
|
||||
engine_result: tx.meta.TransactionResult,
|
||||
tx_json: tx.tx,
|
||||
hash: tx.tx.hash,
|
||||
ledger_index: tx.tx.ledger_index,
|
||||
meta: tx.meta,
|
||||
type: 'transaction',
|
||||
validated: true
|
||||
};
|
||||
|
||||
transaction.result = transaction.engine_result;
|
||||
transaction.result_message = transaction.engine_result_message;
|
||||
}
|
||||
|
||||
if (!transaction.metadata) {
|
||||
transaction.metadata = transaction.meta;
|
||||
}
|
||||
|
||||
if (!transaction.tx_json) {
|
||||
transaction.tx_json = transaction.transaction;
|
||||
}
|
||||
|
||||
delete transaction.transaction;
|
||||
delete transaction.mmeta;
|
||||
delete transaction.meta;
|
||||
|
||||
return transaction;
|
||||
};
|
||||
|
||||
// Transaction fees are adjusted in real-time
|
||||
TransactionManager.prototype._adjustFees = function(loadData) {
|
||||
// ND: note, that `Fee` is a component of a transactionID
|
||||
TransactionManager.prototype._adjustFees = function() {
|
||||
var self = this;
|
||||
|
||||
if (!this._remote.local_fee) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._pending.forEach(function(pending) {
|
||||
var oldFee = pending.tx_json.Fee;
|
||||
var newFee = pending._computeFee();
|
||||
|
||||
function maxFeeExceeded() {
|
||||
pending.once('presubmit', function() {
|
||||
pending.emit('error', 'tejMaxFeeExceeded');
|
||||
function maxFeeExceeded(transaction) {
|
||||
// Don't err until attempting to resubmit
|
||||
transaction.once('presubmit', function() {
|
||||
transaction.emit('error', 'tejMaxFeeExceeded');
|
||||
});
|
||||
};
|
||||
|
||||
this._pending.forEach(function(transaction) {
|
||||
var oldFee = transaction.tx_json.Fee;
|
||||
var newFee = transaction._computeFee();
|
||||
|
||||
if (Number(newFee) > self._maxFee) {
|
||||
return maxFeeExceeded();
|
||||
// Max transaction fee exceeded, abort submission
|
||||
return maxFeeExceeded(transaction);
|
||||
}
|
||||
|
||||
pending.tx_json.Fee = newFee;
|
||||
pending.emit('fee_adjusted', oldFee, newFee);
|
||||
transaction.tx_json.Fee = newFee;
|
||||
transaction.emit('fee_adjusted', oldFee, newFee);
|
||||
|
||||
if (self._remote.trace) {
|
||||
log.info('fee adjusted:', pending.tx_json, oldFee, newFee);
|
||||
log.info('fee adjusted:', transaction.tx_json, oldFee, newFee);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Get pending transactions
|
||||
*
|
||||
* @return {Array} pending transactions
|
||||
*/
|
||||
|
||||
TransactionManager.prototype.getPending = function() {
|
||||
return this._pending;
|
||||
};
|
||||
|
||||
/**
|
||||
* Legacy code. Update transaction status after excessive ledgers pass. One of
|
||||
* either "missing" or "lost"
|
||||
*
|
||||
* @param {Object} ledger data
|
||||
* @api private
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._updatePendingStatus = function(ledger) {
|
||||
assert.strictEqual(typeof ledger, 'object');
|
||||
assert.strictEqual(typeof ledger.ledger_index, 'number');
|
||||
|
||||
this._pending.forEach(function(transaction) {
|
||||
switch (ledger.ledger_index - transaction.submitIndex) {
|
||||
case 4:
|
||||
transaction.emit('missing', ledger);
|
||||
break;
|
||||
case 8:
|
||||
transaction.emit('lost', ledger);
|
||||
break;
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -250,85 +266,88 @@ TransactionManager.prototype._fillSequence = function(tx, callback) {
|
||||
this._loadSequence(sequenceLoaded);
|
||||
};
|
||||
|
||||
/**
|
||||
* Load account transaction sequence
|
||||
*
|
||||
* @param [Function] callback
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._loadSequence = function(callback) {
|
||||
var self = this;
|
||||
var callback = (typeof callback === 'function') ? callback : function(){};
|
||||
|
||||
function sequenceLoaded(err, sequence) {
|
||||
if (typeof sequence === 'number') {
|
||||
if (err || typeof sequence !== 'number') {
|
||||
if (self._remote.trace) {
|
||||
log.info('error requesting account transaction sequence', err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
self._nextSequence = sequence;
|
||||
self.emit('sequence_loaded', sequence);
|
||||
if (typeof callback === 'function') {
|
||||
callback(err, sequence);
|
||||
}
|
||||
} else {
|
||||
setTimeout(function() {
|
||||
self._loadSequence(callback);
|
||||
}, 1000 * 3);
|
||||
}
|
||||
};
|
||||
|
||||
this._account.getNextSequence(sequenceLoaded);
|
||||
};
|
||||
|
||||
TransactionManager.prototype._resubmit = function(ledgers, pending) {
|
||||
/**
|
||||
* On reconnect, load account_tx in case a pending transaction succeeded while
|
||||
* disconnected
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._handleReconnect = function() {
|
||||
var self = this;
|
||||
var pending = pending ? [ pending ] : this._pending;
|
||||
var ledgers = Number(ledgers) || 0;
|
||||
|
||||
function resubmitTransaction(pending) {
|
||||
if (!pending || pending.finalized) {
|
||||
// Transaction has been finalized, nothing to do
|
||||
if (!this._pending.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
function handleTransactions(err, transactions) {
|
||||
if (err || typeof transactions !== 'object') {
|
||||
if (self._remote.trace) {
|
||||
log.info('error requesting account_tx', err);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var hashCached = pending.findId(self._pending._idCache);
|
||||
|
||||
if (self._remote.trace) {
|
||||
log.info('resubmit:', pending.tx_json);
|
||||
if (Array.isArray(transactions.transactions)) {
|
||||
// Treat each transaction in account transaction history as received
|
||||
transactions.transactions.forEach(self._transactionReceived, self);
|
||||
}
|
||||
|
||||
if (hashCached) {
|
||||
return pending.emit('success', hashCached);
|
||||
}
|
||||
|
||||
while (self._pending.hasSequence(pending.tx_json.Sequence)) {
|
||||
//Sequence number has been consumed by another transaction
|
||||
pending.tx_json.Sequence += 1;
|
||||
|
||||
if (self._remote.trace) {
|
||||
log.info('incrementing sequence:', pending.tx_json);
|
||||
}
|
||||
}
|
||||
|
||||
self._request(pending);
|
||||
};
|
||||
|
||||
function resubmitTransactions() {
|
||||
;(function nextTransaction(i) {
|
||||
var transaction = pending[i];
|
||||
|
||||
if (!(transaction instanceof Transaction)) {
|
||||
return;
|
||||
}
|
||||
|
||||
transaction.once('submitted', function(m) {
|
||||
transaction.emit('resubmitted', m);
|
||||
|
||||
self._loadSequence();
|
||||
|
||||
if (++i < pending.length) {
|
||||
nextTransaction(i);
|
||||
}
|
||||
self._loadSequence(function() {
|
||||
// Resubmit pending transactions after sequence is loaded
|
||||
self._resubmit();
|
||||
});
|
||||
|
||||
resubmitTransaction(transaction);
|
||||
})(0);
|
||||
};
|
||||
|
||||
this._waitLedgers(ledgers, resubmitTransactions);
|
||||
var options = {
|
||||
account: this._accountID,
|
||||
ledger_index_min: this._pending.getMinLedger(),
|
||||
ledger_index_max: -1,
|
||||
binary: true,
|
||||
parseBinary: true,
|
||||
limit: 20
|
||||
};
|
||||
|
||||
this._remote.requestAccountTx(options, handleTransactions);
|
||||
};
|
||||
|
||||
/**
|
||||
* Wait for specified number of ledgers to pass
|
||||
*
|
||||
* @param {Number} ledgers
|
||||
* @param {Function} callback
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._waitLedgers = function(ledgers, callback) {
|
||||
assert.strictEqual(typeof ledgers, 'number');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
if (ledgers < 1) {
|
||||
return callback();
|
||||
}
|
||||
@@ -337,35 +356,121 @@ TransactionManager.prototype._waitLedgers = function(ledgers, callback) {
|
||||
var closes = 0;
|
||||
|
||||
function ledgerClosed() {
|
||||
if (++closes < ledgers) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (++closes === ledgers) {
|
||||
self._remote.removeListener('ledger_closed', ledgerClosed);
|
||||
|
||||
callback();
|
||||
}
|
||||
};
|
||||
|
||||
this._remote.on('ledger_closed', ledgerClosed);
|
||||
};
|
||||
|
||||
/**
|
||||
* Resubmit pending transactions. If a transaction is specified, it will be
|
||||
* resubmitted. Otherwise, all pending transactions will be resubmitted
|
||||
*
|
||||
* @param [Number] ledgers to wait before resubmitting
|
||||
* @param [Transaction] pending transactions to resubmit
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._resubmit = function(ledgers, pending) {
|
||||
var self = this;
|
||||
var ledgers = ledgers || 0;
|
||||
var pending = pending ? [ pending ] : this._pending;
|
||||
|
||||
function resubmitTransaction(transaction, next) {
|
||||
if (!transaction || transaction.finalized) {
|
||||
// Transaction has been finalized, nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
// Find ID within cache of received (validated) transaction IDs
|
||||
var received = transaction.findId(self._pending._idCache);
|
||||
|
||||
if (received) {
|
||||
return transaction.emit('success', received);
|
||||
}
|
||||
|
||||
while (self._pending.hasSequence(transaction.tx_json.Sequence)) {
|
||||
//Sequence number has been consumed by another transaction
|
||||
transaction.tx_json.Sequence += 1;
|
||||
|
||||
if (self._remote.trace) {
|
||||
log.info('incrementing sequence:', transaction.tx_json);
|
||||
}
|
||||
}
|
||||
|
||||
if (self._remote.trace) {
|
||||
log.info('resubmit:', transaction.tx_json);
|
||||
}
|
||||
|
||||
transaction.once('submitted', function(m) {
|
||||
transaction.emit('resubmitted', m);
|
||||
next();
|
||||
});
|
||||
|
||||
self._request(transaction);
|
||||
};
|
||||
|
||||
this._waitLedgers(ledgers, function() {
|
||||
async.eachSeries(pending, resubmitTransaction);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Prepare submit request
|
||||
*
|
||||
* @param {Transaction} transaction to submit
|
||||
* @return {Request} submit request
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._prepareRequest = function(tx) {
|
||||
var submitRequest = this._remote.requestSubmit();
|
||||
|
||||
if (this._remote.local_signing) {
|
||||
tx.sign();
|
||||
|
||||
var serialized = tx.serialize();
|
||||
submitRequest.tx_blob(serialized.to_hex());
|
||||
|
||||
var hash = tx.hash(null, null, serialized);
|
||||
tx.addId(hash);
|
||||
} else {
|
||||
// ND: `build_path` is completely ignored when doing local signing as
|
||||
// `Paths` is a component of the signed blob, the `tx_blob` is signed,
|
||||
// sealed and delivered, and the txn unmodified.
|
||||
// TODO: perhaps an exception should be raised if build_path is attempted
|
||||
// while local signing
|
||||
submitRequest.build_path(tx._build_path);
|
||||
submitRequest.secret(tx._secret);
|
||||
submitRequest.tx_json(tx.tx_json);
|
||||
}
|
||||
|
||||
return submitRequest;
|
||||
};
|
||||
|
||||
/**
|
||||
* Send `submit` request, handle response
|
||||
*
|
||||
* @param {Transaction} transaction to submit
|
||||
*/
|
||||
|
||||
TransactionManager.prototype._request = function(tx) {
|
||||
var self = this;
|
||||
var remote = this._remote;
|
||||
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tx.attempts > this._maxAttempts) {
|
||||
return tx.emit('error', new RippleError('tejAttemptsExceeded'));
|
||||
tx.emit('error', new RippleError('tejAttemptsExceeded'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (tx.attempts > 0 && !remote.local_signing) {
|
||||
var message = ''
|
||||
+ 'It is not possible to resubmit transactions automatically safely without '
|
||||
+ 'synthesizing the transactionID locally. See `local_signing` config option';
|
||||
|
||||
return tx.emit('error', new RippleError('tejLocalSigningRequired', message));
|
||||
}
|
||||
|
||||
if (tx.finalized) {
|
||||
var message = 'Automatic resubmission requires local signing';
|
||||
tx.emit('error', new RippleError('tejLocalSigningRequired', message));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -373,22 +478,7 @@ TransactionManager.prototype._request = function(tx) {
|
||||
log.info('submit transaction:', tx.tx_json);
|
||||
}
|
||||
|
||||
function transactionProposed(message) {
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If server is honest, don't expect a final if rejected.
|
||||
message.rejected = tx.isRejected(message.engine_result_code);
|
||||
|
||||
tx.emit('proposed', message);
|
||||
};
|
||||
|
||||
function transactionFailed(message) {
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (message.engine_result) {
|
||||
case 'tefPAST_SEQ':
|
||||
self._resubmit(1, tx);
|
||||
@@ -406,39 +496,20 @@ TransactionManager.prototype._request = function(tx) {
|
||||
};
|
||||
|
||||
function transactionRetry(message) {
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
self._fillSequence(tx, function() {
|
||||
self._resubmit(1, tx);
|
||||
});
|
||||
};
|
||||
|
||||
function transactionFeeClaimed(message) {
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
function transactionFailedLocal(message) {
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (self._remote.local_fee && (message.engine_result === 'telINSUF_FEE_P')) {
|
||||
self._resubmit(2, tx);
|
||||
} else {
|
||||
if (!self._remote.local_fee) {
|
||||
submissionError(message);
|
||||
} else if (message.engine_result === 'telINSUF_FEE_P') {
|
||||
self._resubmit(2, tx);
|
||||
}
|
||||
};
|
||||
|
||||
function submissionError(error) {
|
||||
// Finalized (e.g. aborted) transactions must stop all activity
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (TransactionManager._isTooBusy(error)) {
|
||||
self._resubmit(1, tx);
|
||||
} else {
|
||||
@@ -448,13 +519,12 @@ TransactionManager.prototype._request = function(tx) {
|
||||
};
|
||||
|
||||
function submitted(message) {
|
||||
// Finalized (e.g. aborted) transactions must stop all activity
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ND: If for some unknown reason our hash wasn't computed correctly this is
|
||||
// an extra measure.
|
||||
// ND: If for some unknown reason our hash wasn't computed correctly this
|
||||
// is an extra measure.
|
||||
if (message.tx_json && message.tx_json.hash) {
|
||||
tx.addId(message.tx_json.hash);
|
||||
}
|
||||
@@ -472,10 +542,9 @@ TransactionManager.prototype._request = function(tx) {
|
||||
|
||||
switch (message.result.slice(0, 3)) {
|
||||
case 'tes':
|
||||
transactionProposed(message);
|
||||
tx.emit('proposed', message);
|
||||
break;
|
||||
case 'tec':
|
||||
transactionFeeClaimed(message);
|
||||
break;
|
||||
case 'ter':
|
||||
transactionRetry(message);
|
||||
@@ -492,57 +561,18 @@ TransactionManager.prototype._request = function(tx) {
|
||||
}
|
||||
};
|
||||
|
||||
var submitRequest = remote.requestSubmit();
|
||||
|
||||
submitRequest.once('error', submitted);
|
||||
submitRequest.once('success', submitted);
|
||||
|
||||
function prepareSubmit() {
|
||||
if (remote.local_signing) {
|
||||
// TODO: We are serializing twice, when we could/should be feeding the
|
||||
// tx_blob to `tx.hash()` which rebuilds it to sign it.
|
||||
submitRequest.tx_blob(tx.serialize().to_hex());
|
||||
|
||||
// ND: ecdsa produces a random `TxnSignature` field value, a component of
|
||||
// the hash. Attempting to identify a transaction via a hash synthesized
|
||||
// locally while using remote signing is inherently flawed.
|
||||
tx.addId(tx.hash());
|
||||
} else {
|
||||
// ND: `build_path` is completely ignored when doing local signing as
|
||||
// `Paths` is a component of the signed blob, the `tx_blob` is signed,
|
||||
// sealed and delivered, and the txn unmodified.
|
||||
// TODO: perhaps an exception should be raised if build_path is attempted
|
||||
// while local signing
|
||||
submitRequest.build_path(tx._build_path);
|
||||
submitRequest.secret(tx._secret);
|
||||
submitRequest.tx_json(tx.tx_json);
|
||||
}
|
||||
|
||||
if (tx._server) {
|
||||
submitRequest.server = tx._server;
|
||||
}
|
||||
|
||||
submitTransaction();
|
||||
};
|
||||
|
||||
function requestTimeout() {
|
||||
// ND: What if the response is just slow and we get a response that
|
||||
// `submitted` above will cause to have concurrent resubmit logic streams?
|
||||
// It's simpler to just mute handlers and look out for finalized
|
||||
// `transaction` messages.
|
||||
|
||||
// ND: We should audit the code for other potential multiple resubmit
|
||||
// streams. Connection/reconnection could be one? That's why it's imperative
|
||||
// that ALL transactionIDs sent over network are tracked.
|
||||
|
||||
// Finalized (e.g. aborted) transactions must stop all activity
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
tx.emit('timeout');
|
||||
|
||||
if (remote._connected) {
|
||||
if (remote.isConnected()) {
|
||||
if (remote.trace) {
|
||||
log.info('timeout:', tx.tx_json);
|
||||
}
|
||||
@@ -550,20 +580,6 @@ TransactionManager.prototype._request = function(tx) {
|
||||
}
|
||||
};
|
||||
|
||||
function submitTransaction() {
|
||||
if (tx.finalized) {
|
||||
return;
|
||||
}
|
||||
|
||||
tx.emit('presubmit');
|
||||
|
||||
submitRequest.timeout(self._submissionTimeout, requestTimeout);
|
||||
|
||||
tx.submissions = submitRequest.broadcast();
|
||||
tx.attempts++;
|
||||
tx.emit('postsubmit');
|
||||
};
|
||||
|
||||
tx.submitIndex = this._remote._ledger_current_index;
|
||||
|
||||
if (tx.attempts === 0) {
|
||||
@@ -571,19 +587,31 @@ TransactionManager.prototype._request = function(tx) {
|
||||
}
|
||||
|
||||
if (!tx._setLastLedger) {
|
||||
// Honor LastLedgerSequence set by user of API. If
|
||||
// left unset by API, bump LastLedgerSequence
|
||||
// Honor LastLedgerSequence set by user of API. If left unset by API, bump
|
||||
// LastLedgerSequence
|
||||
tx.tx_json.LastLedgerSequence = tx.submitIndex + 8;
|
||||
}
|
||||
|
||||
tx.lastLedgerSequence = tx.tx_json.LastLedgerSequence;
|
||||
|
||||
if (remote.local_signing) {
|
||||
tx.sign(prepareSubmit);
|
||||
} else {
|
||||
prepareSubmit();
|
||||
tx.sign();
|
||||
}
|
||||
|
||||
var submitRequest = this._prepareRequest(tx);
|
||||
submitRequest.once('error', submitted);
|
||||
submitRequest.once('success', submitted);
|
||||
|
||||
tx.emit('presubmit');
|
||||
|
||||
tx.submissions = submitRequest.broadcast();
|
||||
tx.attempts++;
|
||||
|
||||
tx.emit('postsubmit');
|
||||
|
||||
//XXX
|
||||
submitRequest.timeout(self._submissionTimeout, requestTimeout);
|
||||
|
||||
return submitRequest;
|
||||
};
|
||||
|
||||
@@ -620,60 +648,33 @@ TransactionManager.prototype.submit = function(tx) {
|
||||
var self = this;
|
||||
var remote = this._remote;
|
||||
|
||||
// If sequence number is not yet known, defer until it is.
|
||||
if (typeof this._nextSequence !== 'number') {
|
||||
this.once('sequence_loaded', this.submit.bind(this, tx));
|
||||
// If sequence number is not yet known, defer until it is.
|
||||
this.once('sequence_loaded', function() {
|
||||
self.submit(tx);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Finalized (e.g. aborted) transactions must stop all activity
|
||||
if (tx.finalized) {
|
||||
// Finalized transactions must stop all activity
|
||||
return;
|
||||
}
|
||||
|
||||
function cleanup(message) {
|
||||
// ND: We can just remove this `tx` by identity
|
||||
self._pending.remove(tx);
|
||||
tx.emit('final', message);
|
||||
if (remote.trace) {
|
||||
log.info('transaction finalized:', tx.tx_json, self._pending.getLength());
|
||||
}
|
||||
};
|
||||
|
||||
tx.once('cleanup', cleanup);
|
||||
|
||||
tx.on('save', function() {
|
||||
self.emit('save', tx);
|
||||
});
|
||||
|
||||
tx.once('error', function(message) {
|
||||
tx._errorHandler(message);
|
||||
});
|
||||
|
||||
tx.once('success', function(message) {
|
||||
tx._successHandler(message);
|
||||
});
|
||||
|
||||
tx.once('abort', function() {
|
||||
tx.emit('error', new RippleError('tejAbort', 'Transaction aborted'));
|
||||
});
|
||||
|
||||
if (typeof tx.tx_json.Sequence !== 'number') {
|
||||
// Honor manually-set sequences
|
||||
tx.tx_json.Sequence = this._nextSequence++;
|
||||
}
|
||||
|
||||
// Attach secret, associate transaction with a server, attach fee.
|
||||
// If the transaction can't complete, decrement sequence so that
|
||||
// subsequent transactions
|
||||
tx.once('cleanup', function() {
|
||||
self.getPending().remove(tx);
|
||||
});
|
||||
|
||||
if (!tx.complete()) {
|
||||
this._nextSequence--;
|
||||
return;
|
||||
}
|
||||
|
||||
tx.attempts = 0;
|
||||
tx.submissions = 0;
|
||||
tx.responses = 0;
|
||||
|
||||
// ND: this is the ONLY place we put the tx into the queue. The
|
||||
// TransactionQueue queue is merely a list, so any mutations to tx._hash
|
||||
// will cause subsequent look ups (eg. inside 'transaction-outbound'
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
var LRU = require('lru-cache');
|
||||
var Transaction = require('./transaction').Transaction;
|
||||
|
||||
/**
|
||||
* Manager for pending transactions
|
||||
*/
|
||||
|
||||
var LRU = require('lru-cache');
|
||||
var Transaction = require('./transaction').Transaction;
|
||||
|
||||
function TransactionQueue() {
|
||||
this._queue = [ ];
|
||||
this._idCache = LRU();
|
||||
this._sequenceCache = LRU();
|
||||
this._idCache = LRU({ max: 200 });
|
||||
this._sequenceCache = LRU({ max: 200 });
|
||||
};
|
||||
|
||||
/**
|
||||
* Store received (validated) sequence
|
||||
*
|
||||
* @param {Number} sequence
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.addReceivedSequence = function(sequence) {
|
||||
@@ -23,6 +24,9 @@ TransactionQueue.prototype.addReceivedSequence = function(sequence) {
|
||||
/**
|
||||
* Check that sequence number has been consumed by a validated
|
||||
* transaction
|
||||
*
|
||||
* @param {Number} sequence
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.hasSequence = function(sequence) {
|
||||
@@ -31,6 +35,9 @@ TransactionQueue.prototype.hasSequence = function(sequence) {
|
||||
|
||||
/**
|
||||
* Store received (validated) ID transaction
|
||||
*
|
||||
* @param {String} transaction id
|
||||
* @param {Transaction} transaction
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.addReceivedId = function(id, transaction) {
|
||||
@@ -39,6 +46,9 @@ TransactionQueue.prototype.addReceivedId = function(id, transaction) {
|
||||
|
||||
/**
|
||||
* Get received (validated) transaction by ID
|
||||
*
|
||||
* @param {String} transaction id
|
||||
* @return {Object}
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.getReceived = function(id) {
|
||||
@@ -48,6 +58,9 @@ TransactionQueue.prototype.getReceived = function(id) {
|
||||
/**
|
||||
* Get a submitted transaction by ID. Transactions
|
||||
* may have multiple associated IDs.
|
||||
*
|
||||
* @param {String} transaction id
|
||||
* @return {Transaction}
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.getSubmission = function(id) {
|
||||
@@ -63,8 +76,32 @@ TransactionQueue.prototype.getSubmission = function(id) {
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get earliest ledger in the pending queue
|
||||
*
|
||||
* @return {Number} ledger
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.getMinLedger = function() {
|
||||
var result = Infinity;
|
||||
|
||||
for (var i=0, tx; (tx=this._queue[i]); i++) {
|
||||
if (tx.initialSubmitIndex < result) {
|
||||
result = tx.initialSubmitIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isFinite(result)) {
|
||||
result = -1;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove a transaction from the queue
|
||||
*
|
||||
* @param {String|Transaction} transaction or id
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.remove = function(tx) {
|
||||
@@ -87,14 +124,30 @@ TransactionQueue.prototype.remove = function(tx) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Add a transaction to pending queue
|
||||
*
|
||||
* @param {Transaction} transaction
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.push = function(tx) {
|
||||
this._queue.push(tx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Iterate over pending transactions
|
||||
*
|
||||
* @param {Function} iterator
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.forEach = function(fn) {
|
||||
this._queue.forEach(fn);
|
||||
};
|
||||
|
||||
/**
|
||||
* @return {Number} length of pending queue
|
||||
*/
|
||||
|
||||
TransactionQueue.prototype.length =
|
||||
TransactionQueue.prototype.getLength = function() {
|
||||
return this._queue.length;
|
||||
|
||||
@@ -2,6 +2,7 @@ var utils = require('./testutils');
|
||||
var assert = require('assert');
|
||||
var Amount = utils.load_module('amount').Amount;
|
||||
var Transaction = utils.load_module('transaction').Transaction;
|
||||
var TransactionQueue = utils.load_module('transactionqueue').TransactionQueue;
|
||||
var Remote = utils.load_module('remote').Remote;
|
||||
var Server = utils.load_module('server').Server;
|
||||
|
||||
@@ -38,7 +39,7 @@ describe('Transaction', function() {
|
||||
it('Success listener', function(done) {
|
||||
var transaction = new Transaction();
|
||||
|
||||
transaction.once('cleanup', function(message) {
|
||||
transaction.once('final', function(message) {
|
||||
assert.deepEqual(message, transactionResult);
|
||||
assert(transaction.finalized);
|
||||
assert.strictEqual(transaction.state, 'validated');
|
||||
@@ -51,7 +52,7 @@ describe('Transaction', function() {
|
||||
it('Error listener', function(done) {
|
||||
var transaction = new Transaction();
|
||||
|
||||
transaction.once('cleanup', function(message) {
|
||||
transaction.once('final', function(message) {
|
||||
assert.deepEqual(message, transactionResult);
|
||||
assert(transaction.finalized);
|
||||
assert.strictEqual(transaction.state, 'failed');
|
||||
@@ -130,25 +131,29 @@ describe('Transaction', function() {
|
||||
|
||||
it('Set state', function(done) {
|
||||
var transaction = new Transaction();
|
||||
transaction.state = 'pending';
|
||||
|
||||
assert.strictEqual(transaction.state, 'unsubmitted');
|
||||
|
||||
var receivedEvents = 0;
|
||||
var events = 2;
|
||||
var events = [
|
||||
'submitted',
|
||||
'pending',
|
||||
'validated'
|
||||
];
|
||||
|
||||
transaction.once('state', function(state) {
|
||||
assert.strictEqual(state, 'validated');
|
||||
if (++receivedEvents === events) {
|
||||
done();
|
||||
}
|
||||
transaction.on('state', function(state) {
|
||||
receivedEvents++;
|
||||
assert(events.indexOf(state) > -1, 'Invalid state: ' + state);
|
||||
});
|
||||
|
||||
transaction.once('save', function() {
|
||||
if (++receivedEvents === events) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
transaction.setState(events[0]);
|
||||
transaction.setState(events[1]);
|
||||
transaction.setState(events[1]);
|
||||
transaction.setState(events[2]);
|
||||
|
||||
transaction.setState('validated');
|
||||
assert.strictEqual(receivedEvents, 3);
|
||||
assert.strictEqual(transaction.state, events[2]);
|
||||
done();
|
||||
});
|
||||
|
||||
it('Finalize submission', function() {
|
||||
@@ -720,47 +725,20 @@ describe('Transaction', function() {
|
||||
done();
|
||||
});
|
||||
|
||||
it('Sign transaction - with callback', function(done) {
|
||||
var transaction = new Transaction();
|
||||
transaction._secret = 'sh2pTicynUEG46jjR4EoexHcQEoij';
|
||||
transaction.tx_json.SigningPubKey = '021FED5FD081CE5C4356431267D04C6E2167E4112C897D5E10335D4E22B4DA49ED';
|
||||
transaction.tx_json.Account = 'rMWwx3Ma16HnqSd4H6saPisihX9aKpXxHJ';
|
||||
transaction.tx_json.Flags = 0;
|
||||
transaction.tx_json.Fee = 10;
|
||||
transaction.tx_json.Sequence = 1;
|
||||
transaction.tx_json.TransactionType = 'AccountSet';
|
||||
|
||||
transaction.sign(function() {
|
||||
var signature = transaction.tx_json.TxnSignature;
|
||||
assert.strictEqual(transaction.previousSigningHash, 'D1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
|
||||
assert(/^[A-Z0-9]+$/.test(signature));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Add transaction ID', function(done) {
|
||||
var transaction = new Transaction();
|
||||
var saved = 0;
|
||||
|
||||
transaction.on('save', function() {
|
||||
++saved;
|
||||
});
|
||||
transaction.addId('D1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
|
||||
transaction.addId('F1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
|
||||
transaction.addId('F1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
|
||||
|
||||
transaction.once('save', function() {
|
||||
setImmediate(function() {
|
||||
assert.strictEqual(saved, 2);
|
||||
assert.deepEqual(
|
||||
transaction.submittedIDs,
|
||||
[ 'F1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE',
|
||||
'D1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE' ]
|
||||
);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
transaction.addId('D1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
|
||||
transaction.addId('F1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
|
||||
transaction.addId('F1C15200CF532175F1890B6440AD223D3676140522BC11D2784E56760AE3B4FE');
|
||||
done();
|
||||
});
|
||||
|
||||
it('Find transaction IDs in cache', function(done) {
|
||||
@@ -791,9 +769,10 @@ describe('Transaction', function() {
|
||||
|
||||
it('Set DestinationTag', function() {
|
||||
var transaction = new Transaction();
|
||||
transaction.destinationTag();
|
||||
transaction.destinationTag('tag');
|
||||
assert.strictEqual(transaction.tx_json.DestinationTag, 'tag');
|
||||
assert.strictEqual(transaction.tx_json.DestinationTag, void(0));
|
||||
transaction.destinationTag(1);
|
||||
assert.strictEqual(transaction.tx_json.DestinationTag, 1);
|
||||
});
|
||||
|
||||
it('Set InvoiceID', function() {
|
||||
@@ -872,7 +851,7 @@ describe('Transaction', function() {
|
||||
}
|
||||
];
|
||||
|
||||
assert.deepEqual(Transaction._pathRewrite(path), [
|
||||
assert.deepEqual(Transaction._rewritePath(path), [
|
||||
{
|
||||
account: 'rP51ycDJw5ZhgvdKiRjBYZKYjsyoCcHmnY',
|
||||
issuer: 'rsLEU1TPdCJPPysqhWYw9jD97xtG5WqSJm',
|
||||
@@ -892,7 +871,9 @@ describe('Transaction', function() {
|
||||
});
|
||||
|
||||
it('Rewrite transaction path - invalid path', function() {
|
||||
assert.strictEqual(Transaction._pathRewrite(1), void(0));
|
||||
assert.throws(function() {
|
||||
assert.strictEqual(Transaction._rewritePath(1), void(0));
|
||||
});
|
||||
});
|
||||
|
||||
it('Add transaction path', function() {
|
||||
@@ -1001,51 +982,52 @@ describe('Transaction', function() {
|
||||
it('Set SourceTag', function() {
|
||||
var transaction = new Transaction();
|
||||
transaction.sourceTag('tag');
|
||||
assert.strictEqual(transaction.tx_json.SourceTag, 'tag');
|
||||
assert.strictEqual(transaction.tx_json.SourceTag, void(0));
|
||||
transaction.sourceTag(1);
|
||||
assert.strictEqual(transaction.tx_json.SourceTag, 1);
|
||||
});
|
||||
|
||||
it('Set TransferRate', function() {
|
||||
var transaction = new Transaction();
|
||||
|
||||
assert.throws(function() {
|
||||
transaction.transferRate(1);
|
||||
});
|
||||
|
||||
assert.throws(function() {
|
||||
transaction.transferRate('1');
|
||||
});
|
||||
|
||||
assert.strictEqual(transaction.tx_json.TransferRate, void(0));
|
||||
transaction.transferRate(1.5 * 1e9);
|
||||
|
||||
assert.strictEqual(transaction.tx_json.TransferRate, 1.5 * 1e9);
|
||||
});
|
||||
|
||||
it('Set Flags', function(done) {
|
||||
var transaction = new Transaction();
|
||||
transaction.tx_json.TransactionType = 'Payment';
|
||||
|
||||
transaction.setFlags();
|
||||
|
||||
assert.strictEqual(transaction.tx_json.Flags, 0);
|
||||
|
||||
transaction.setFlags(1);
|
||||
var transaction = new Transaction();
|
||||
transaction.tx_json.TransactionType = 'Payment';
|
||||
transaction.setFlags(Transaction.flags.Payment.PartialPayment);
|
||||
assert.strictEqual(transaction.tx_json.Flags, 131072);
|
||||
|
||||
assert.strictEqual(transaction.tx_json.Flags, 1);
|
||||
var transaction = new Transaction();
|
||||
transaction.tx_json.TransactionType = 'Payment';
|
||||
transaction.setFlags('NoRippleDirect');
|
||||
assert.strictEqual(transaction.tx_json.Flags, 65536);
|
||||
|
||||
transaction.setFlags('PartialPayment');
|
||||
|
||||
assert.strictEqual(transaction.tx_json.Flags, 131073);
|
||||
var transaction = new Transaction();
|
||||
transaction.tx_json.TransactionType = 'Payment';
|
||||
transaction.setFlags('PartialPayment', 'NoRippleDirect');
|
||||
assert.strictEqual(transaction.tx_json.Flags, 196608);
|
||||
|
||||
var transaction = new Transaction();
|
||||
transaction.tx_json.TransactionType = 'Payment';
|
||||
transaction.setFlags([ 'LimitQuality', 'PartialPayment' ]);
|
||||
assert.strictEqual(transaction.tx_json.Flags, 393216);
|
||||
|
||||
assert.strictEqual(transaction.tx_json.Flags, 524289);
|
||||
|
||||
var transaction = new Transaction();
|
||||
transaction.tx_json.TransactionType = 'Payment';
|
||||
transaction.once('error', function(err) {
|
||||
assert.strictEqual(err.result, 'tejInvalidFlag');
|
||||
done();
|
||||
});
|
||||
|
||||
transaction.setFlags('test');
|
||||
transaction.setFlags('asdf');
|
||||
});
|
||||
|
||||
it('Add Memo', function() {
|
||||
@@ -1548,16 +1530,10 @@ describe('Transaction', function() {
|
||||
|
||||
it('Submit transaction - invalid account', function(done) {
|
||||
var remote = new Remote();
|
||||
var transaction = new Transaction(remote).accountSet('r36xtKNKR43SeXnGn7kN4r4JdQzcrkqpWe');
|
||||
|
||||
transaction.tx_json.Account += 'z';
|
||||
|
||||
transaction.once('error', function(err) {
|
||||
assert.strictEqual(err.result, 'tejInvalidAccount');
|
||||
done();
|
||||
assert.throws(function() {
|
||||
var transaction = new Transaction(remote).accountSet('r36xtKNKR43SeXnGn7kN4r4JdQzcrkqpWeZ');
|
||||
});
|
||||
|
||||
transaction.submit();
|
||||
done();
|
||||
});
|
||||
|
||||
it('Abort submission on presubmit', function(done) {
|
||||
@@ -1589,6 +1565,39 @@ describe('Transaction', function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Get min ledger', function() {
|
||||
var queue = new TransactionQueue();
|
||||
|
||||
// Randomized submit indexes
|
||||
[
|
||||
28093,
|
||||
456944,
|
||||
347213,
|
||||
165662,
|
||||
729760,
|
||||
808990,
|
||||
927393,
|
||||
925550,
|
||||
872298,
|
||||
543305
|
||||
]
|
||||
.forEach(function(index){
|
||||
var tx = new Transaction();
|
||||
tx.initialSubmitIndex = index;
|
||||
queue.push(tx);
|
||||
});
|
||||
|
||||
// Pending queue sorted by submit index
|
||||
var sorted = queue._queue.slice().sort(function(a, b) {
|
||||
return a.initialSubmitIndex - b.initialSubmitIndex;
|
||||
});
|
||||
|
||||
sorted.forEach(function(tx){
|
||||
assert.strictEqual(queue.getMinLedger(), tx.initialSubmitIndex);
|
||||
queue.remove(tx);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// vim:sw=2:sts=2:ts=8:et
|
||||
//vim:sw=2:sts=2:ts=8:et
|
||||
|
||||
Reference in New Issue
Block a user