From beee227824e3c21b05b4b6cab4f88904c9908373 Mon Sep 17 00:00:00 2001 From: Aleks Lozovyuk Date: Thu, 12 Nov 2026 12:07:12 +0200 Subject: [PATCH] utils: tie-break equal output indices instead of throwing vout_before threw when two outputs shared account, subaddress and local index, although std::sort may compare any two elements. The comment already promised a key-image tie-break; implement it, fall back to the transaction hash, and compare equal outputs as equal. --- src/utils/monero_utils.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/utils/monero_utils.cpp b/src/utils/monero_utils.cpp index 17743a56..5e87db10 100644 --- a/src/utils/monero_utils.cpp +++ b/src/utils/monero_utils.cpp @@ -606,7 +606,28 @@ bool monero_utils::vout_before(const std::shared_ptr& o1, const s if (ow1->m_subaddress_index.get() < ow2->m_subaddress_index.get()) return true; if (ow1->m_subaddress_index.get() == ow2->m_subaddress_index.get()) { if (ow1->m_index.get() < ow2->m_index.get()) return true; - if (ow1->m_index.get() == ow2->m_index.get()) throw std::runtime_error("Should never sort outputs with duplicate indices"); + if (ow1->m_index.get() == ow2->m_index.get()) { + // TWO DIFFERENT OUTPUTS CAN CARRY THE SAME LOCAL INDEX. This comparator used to throw here, although + // std::sort is allowed to compare any two elements and a throwing comparator is not a valid ordering. + // Keep the order total with the fields the comment above already promised: the key image, then the + // transaction hash. Two truly identical outputs compare equal (false in both directions). + auto keyImageHex = [](const std::shared_ptr& output) { + if (output->m_key_image == nullptr || output->m_key_image->m_hex == boost::none) return std::string(); + return output->m_key_image->m_hex.get(); + }; + auto txHash = [](const std::shared_ptr& output) { + if (output->m_tx == nullptr || output->m_tx->m_hash == boost::none) return std::string(); + return output->m_tx->m_hash.get(); + }; + const std::string keyImage1 = keyImageHex(ow1); + const std::string keyImage2 = keyImageHex(ow2); + if (keyImage1 != keyImage2) return keyImage1 < keyImage2; + const std::string txHash1 = txHash(ow1); + const std::string txHash2 = txHash(ow2); + if (txHash1 != txHash2) return txHash1 < txHash2; + return false; + } + return false; } } return false;