wallet2: fix infinite loop in fake out selection

The gamma picker and the caller code did not quite agree on the
number of rct outputs available for use - by one block - which
caused an infinite loop if the picker could never pick outputs
from that block but already had picked all other outputs from
previous blocks.

Also change the range to select from using code from UkoeHB.
This commit is contained in:
Crypto City 2023-03-21 09:02:38 +00:00
parent 2c2030a039
commit f4b9eeef1d
2 changed files with 18 additions and 3 deletions

View File

@ -1091,9 +1091,23 @@ gamma_picker::gamma_picker(const std::vector<uint64_t> &rct_offsets, double shap
const size_t blocks_in_a_year = 86400 * 365 / DIFFICULTY_TARGET_V2;
const size_t blocks_to_consider = std::min<size_t>(rct_offsets.size(), blocks_in_a_year);
const size_t outputs_to_consider = rct_offsets.back() - (blocks_to_consider < rct_offsets.size() ? rct_offsets[rct_offsets.size() - blocks_to_consider - 1] : 0);
begin = rct_offsets.data();
end = rct_offsets.data() + rct_offsets.size() - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE;
num_rct_outputs = *(end - 1);
end = rct_offsets.data() + rct_offsets.size() - (std::max(1, CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE) - 1);
auto top_spendable_it =
std::next(
rct_offsets.rbegin(),
std::min<size_t>(
rct_offsets.size(),
std::max(1, CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE) - 1
)
);
if (top_spendable_it == rct_offsets.rend())
num_rct_outputs = 0;
else
num_rct_outputs = *top_spendable_it;
THROW_WALLET_EXCEPTION_IF(num_rct_outputs == 0, error::wallet_internal_error, "No rct outputs");
average_output_time = DIFFICULTY_TARGET_V2 * blocks_to_consider / static_cast<double>(outputs_to_consider); // this assumes constant target over the whole rct range
};
@ -9147,7 +9161,7 @@ void wallet2::get_outs(std::vector<std::vector<tools::wallet2::get_outs_entry>>
else
{
// the base offset of the first rct output in the first unlocked block (or the one to be if there's none)
num_outs = m_rct_offsets[m_rct_offsets.size() - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE];
num_outs = gamma->get_num_rct_outs();
LOG_PRINT_L1("" << num_outs << " unlocked rct outputs");
THROW_WALLET_EXCEPTION_IF(num_outs == 0, error::wallet_internal_error,
"histogram reports no unlocked rct outputs, not even ours");

View File

@ -117,6 +117,7 @@ namespace tools
uint64_t pick();
gamma_picker(const std::vector<uint64_t> &rct_offsets);
gamma_picker(const std::vector<uint64_t> &rct_offsets, double shape, double scale);
uint64_t get_num_rct_outs() const { return num_rct_outputs; }
private:
struct gamma_engine