From e12a3fc8feed4030847adf283645456229e4bdff Mon Sep 17 00:00:00 2001 From: woodser Date: Tue, 30 Jul 2019 16:06:54 -0400 Subject: [PATCH] getOutputs() sorts txs by height; comparators use common comparator for height comparison --- src/wallet/MoneroWallet.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index 4d936c6..229d326 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -393,7 +393,7 @@ namespace monero { /** * Returns true iff tx1's height is known to be less than tx2's height for sorting. */ - bool txHeightLessThan(const shared_ptr& tx1, const shared_ptr& tx2) { + bool txHeightLessThan(const shared_ptr& tx1, const shared_ptr& tx2) { if (tx1->block != boost::none && tx2->block != boost::none) return tx1->getHeight() < tx2->getHeight(); else if (tx1->block == boost::none) return false; else return true; @@ -403,6 +403,11 @@ namespace monero { * Returns true iff transfer1 is ordered before transfer2 by ascending account and subaddress indices. */ bool incomingTransferBefore(const shared_ptr& transfer1, const shared_ptr& transfer2) { + + // compare by height + if (txHeightLessThan(transfer1->tx, transfer2->tx)) return true; + + // compare by account and subaddress index if (transfer1->accountIndex.get() < transfer2->accountIndex.get()) return true; else if (transfer1->accountIndex.get() == transfer2->accountIndex.get()) return transfer1->subaddressIndex.get() < transfer2->subaddressIndex.get(); else return false; @@ -414,6 +419,11 @@ namespace monero { bool voutBefore(const shared_ptr& o1, const shared_ptr& o2) { shared_ptr ow1 = static_pointer_cast(o1); shared_ptr ow2 = static_pointer_cast(o2); + + // compare by height + if (txHeightLessThan(ow1->tx, ow2->tx)) return true; + + // compare by account index, subaddress index, and output if (ow1->accountIndex.get() < ow2->accountIndex.get()) return true; else if (ow1->accountIndex.get() == ow2->accountIndex.get()) { if (ow1->subaddressIndex.get() < ow2->subaddressIndex.get()) return true; @@ -1566,10 +1576,16 @@ namespace monero { mergeTx(tx, txMap, blockMap, false); } - // collect requested vouts - vector> vouts; + // sort txs by block height + vector> txs ; for (map>::const_iterator txIter = txMap.begin(); txIter != txMap.end(); txIter++) { - shared_ptr tx = txIter->second; + txs.push_back(txIter->second); + } + sort(txs.begin(), txs.end(), txHeightLessThan); + + // filter and return outputs + vector> vouts; + for (const shared_ptr& tx : txs) { // sort outputs sort(tx->vouts.begin(), tx->vouts.end(), voutBefore);