diff options
author | Riccardo Spagni <ric@spagni.net> | 2015-08-10 20:04:30 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2015-08-10 20:04:32 +0200 |
commit | 09cada45c4e02b5f5dc6c8878dfe90035fe491d4 (patch) | |
tree | 62bab4f94f9fb6b934f79172f6cc1b684a88edb6 /src/cryptonote_core | |
parent | Merge pull request #362 (diff) | |
parent | blockchain: factor get_num_outpouts(amount) calls (diff) | |
download | monero-09cada45c4e02b5f5dc6c8878dfe90035fe491d4.tar.xz |
Merge pull request #363
4f19e68 blockchain: factor get_num_outpouts(amount) calls (moneromooo-monero)
275894c blockchain: always select random outs using triangular distribution (moneromooo-monero)
Diffstat (limited to 'src/cryptonote_core')
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index cc2d6f821..648b77b2f 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -1473,6 +1473,7 @@ bool Blockchain::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUT // from BlockchainDB where <n> is req.outs_count (number of mixins). for (uint64_t amount : req.amounts) { + auto num_outs = m_db->get_num_outputs(amount); // create outs_for_amount struct and populate amount field COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount& result_outs = *res.outs.insert(res.outs.end(), COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS::outs_for_amount()); result_outs.amount = amount; @@ -1481,9 +1482,9 @@ bool Blockchain::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUT // if there aren't enough outputs to mix with (or just enough), // use all of them. Eventually this should become impossible. - if (m_db->get_num_outputs(amount) <= req.outs_count) + if (num_outs <= req.outs_count) { - for (uint64_t i = 0; i < m_db->get_num_outputs(amount); i++) + for (uint64_t i = 0; i < num_outs; i++) { // get tx_hash, tx_out_index from DB tx_out_index toi = m_db->get_output_tx_and_index(amount, i); @@ -1499,7 +1500,6 @@ bool Blockchain::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUT else { // while we still need more mixins - auto num_outs = m_db->get_num_outputs(amount); while (result_outs.outs.size() < req.outs_count) { // if we've gone through every possible output, we've gotten all we can @@ -1511,7 +1511,15 @@ bool Blockchain::get_random_outs_for_amounts(const COMMAND_RPC_GET_RANDOM_OUTPUT // get a random output index from the DB. If we've already seen it, // return to the top of the loop and try again, otherwise add it to the // list of output indices we've seen. - uint64_t i = m_db->get_random_output(amount); + + // triangular distribution over [a,b) with a=0, mode c=b=up_index_limit + uint64_t r = crypto::rand<uint64_t>() % ((uint64_t)1 << 53); + double frac = std::sqrt((double)r / ((uint64_t)1 << 53)); + uint64_t i = (uint64_t)(frac*num_outs); + // just in case rounding up to 1 occurs after sqrt + if (i == num_outs) + --i; + if (seen_indices.count(i)) { continue; |