Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
[FIX] Notifications: Sort transcations on meta.TransactionIndex
Browse files Browse the repository at this point in the history
(RLJS-253)

- Previous fix was incorrectly sorting on Sequence, but that can only
  sort transactions submitted by the same account
  • Loading branch information
Alan Cohen committed Mar 2, 2015
1 parent b8874dd commit 2361a4e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
9 changes: 1 addition & 8 deletions api/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,7 @@ function attachPreviousAndNextTransactionIdentifiers(response, notificationDetai
return tx.hash;
});

// Sort transactions in ascending order (earliestFirst) by ledger_index
transactions.sort(function(a, b) {
if (a.ledger_index === b.ledger_index) {
return a.date <= b.date ? -1 : 1;
} else {
return a.ledger_index < b.ledger_index ? -1 : 1;
}
});
transactions.sort(utils.compareTransactions);

async_callback(null, transactions);
}
Expand Down
26 changes: 25 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = {
getUrlBase: getUrlBase,
parseLedger: parseLedger,
parseCurrencyAmount: parseCurrencyAmount,
parseCurrencyQuery: parseCurrencyQuery
parseCurrencyQuery: parseCurrencyQuery,
compareTransactions: compareTransactions
};

function dropsToXrp(drops) {
Expand Down Expand Up @@ -91,3 +92,26 @@ function parseCurrencyQuery(query) {
};
}
}

function signum(num) {
return (num === 0) ? 0 : (num > 0 ? 1 : -1);
}

/**
* Order two rippled transactions based on their ledger_index.
* If two transactions took place in the same ledger, sort
* them based on TransactionIndex
* See: https://ripple.com/build/transactions/
*
* @param {Object} first
* @param {Object} second
* @returns {Number} [-1, 0, 1]
*/
function compareTransactions(first, second) {
if (first.ledger_index === second.ledger_index) {
return signum(first.meta.TransactionIndex - second.meta.TransactionIndex);
} else {
return first.ledger_index < second.ledger_index ? -1 : 1;
}
}

45 changes: 45 additions & 0 deletions test/unit/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,48 @@ suite('unit - utils.parseCurrencyQuery()', function() {
});
});
});


suite('unit - utils.compareTransactions()', function() {
test('compareTransactions() -- different ledgers', function() {
var tx1 = {
ledger_index: 1
};

var tx2 = {
ledger_index: 2
};

assert.strictEqual(utils.compareTransactions(tx1,tx2), -1);
});

test('compareTransactions() -- same ledger', function() {
var tx1 = {
ledger_index: 1,
meta: {
TransactionIndex: 2
}
};

var tx2 = {
ledger_index: 1,
meta: {
TransactionIndex: 1
}
};

assert.strictEqual(utils.compareTransactions(tx1,tx2), 1);
});

test('compareTransactions() -- same transaction', function() {
var tx1 = {
ledger_index: 1,
meta: {
TransactionIndex: 2
}
};

assert.strictEqual(utils.compareTransactions(tx1,tx1), 0);
});

});

0 comments on commit 2361a4e

Please sign in to comment.